Branch Master nur für Readme. Siehe andere Branches
This commit is contained in:
		
							
								
								
									
										9
									
								
								LICENSE
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								LICENSE
									
									
									
									
									
								
							@@ -1,9 +0,0 @@
 | 
				
			|||||||
MIT License
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Copyright (c) <year> <copyright holders>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 | 
					 | 
				
			||||||
@@ -1,5 +1,11 @@
 | 
				
			|||||||
# LoginDemo
 | 
					# LoginDemo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Es gibt 2 Versionen, eine lässt CSRF zu die andere nicht
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					* [NO_CSRF](https://git.dergeorg.at/dergeorg/LoginDemo/src/branch/NO_CSRF)
 | 
				
			||||||
 | 
					* [CSRF_erlaubt](https://git.dergeorg.at/dergeorg/LoginDemo/src/branch/CSRF_erlaubt)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Google Auth +
 | 
					Google Auth +
 | 
				
			||||||
Digest +
 | 
					Digest +
 | 
				
			||||||
Formular +
 | 
					Formular +
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1 +0,0 @@
 | 
				
			|||||||
exports.users = require('./users');
 | 
					 | 
				
			||||||
							
								
								
									
										64
									
								
								db/users.js
									
									
									
									
									
								
							
							
						
						
									
										64
									
								
								db/users.js
									
									
									
									
									
								
							@@ -1,64 +0,0 @@
 | 
				
			|||||||
exports.createUser = function(userobj, cb) {
 | 
					 | 
				
			||||||
  process.nextTick(function (){
 | 
					 | 
				
			||||||
    var lastId = records[records.length - 1].id
 | 
					 | 
				
			||||||
    userobj.id = lastId+1;
 | 
					 | 
				
			||||||
    records.push(userobj)
 | 
					 | 
				
			||||||
    return cb(null, userobj)
 | 
					 | 
				
			||||||
  })
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
exports.findByUsernameGoogle = function(userobj, cb) {
 | 
					 | 
				
			||||||
  process.nextTick(function (){
 | 
					 | 
				
			||||||
    console.log(userobj)
 | 
					 | 
				
			||||||
    for (var i = 0, len = records.length; i < len; i++) {
 | 
					 | 
				
			||||||
      var record = records[i];
 | 
					 | 
				
			||||||
      if (record.googleId === userobj.googleId) {
 | 
					 | 
				
			||||||
        return cb(null, record);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return cb(null, null);
 | 
					 | 
				
			||||||
  })
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
exports.findByToken = function(userobj, cb) {
 | 
					 | 
				
			||||||
  process.nextTick(function (){
 | 
					 | 
				
			||||||
    console.log(userobj)
 | 
					 | 
				
			||||||
    for (var i = 0, len = records.length; i < len; i++) {
 | 
					 | 
				
			||||||
      var record = records[i];
 | 
					 | 
				
			||||||
      if (record.token === userobj.token) {
 | 
					 | 
				
			||||||
        return cb(null, record);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return cb(null, null);
 | 
					 | 
				
			||||||
  })
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var records = [
 | 
					 | 
				
			||||||
    { id: 1, username: 'jack', password: 'secret', displayName: 'Jack', emails: [ { value: 'jack@example.com' } ], googleId: 1 }
 | 
					 | 
				
			||||||
  , { id: 2, username: 'jill', password: 'birthday', displayName: 'Jill', emails: [ { value: 'jill@example.com' } ], googleId: 2 }
 | 
					 | 
				
			||||||
];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
exports.findById = function(id, cb) {
 | 
					 | 
				
			||||||
  process.nextTick(function() {
 | 
					 | 
				
			||||||
    var idx = id - 1;
 | 
					 | 
				
			||||||
    if (records[idx]) {
 | 
					 | 
				
			||||||
      cb(null, records[idx]);
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      cb(new Error('User ' + id + ' does not exist'));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
exports.findByUsername = function(username, cb) {
 | 
					 | 
				
			||||||
  process.nextTick(function() {
 | 
					 | 
				
			||||||
    for (var i = 0, len = records.length; i < len; i++) {
 | 
					 | 
				
			||||||
      var record = records[i];
 | 
					 | 
				
			||||||
      console.log("Username")
 | 
					 | 
				
			||||||
      console.log(username)
 | 
					 | 
				
			||||||
      if (record.username === username) {
 | 
					 | 
				
			||||||
        return cb(null, record);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return cb(null, null);
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										1441
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1441
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										37
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								package.json
									
									
									
									
									
								
							@@ -1,37 +0,0 @@
 | 
				
			|||||||
{
 | 
					 | 
				
			||||||
  "name": "express-4.x-local-example",
 | 
					 | 
				
			||||||
  "version": "0.0.0",
 | 
					 | 
				
			||||||
  "description": "Express 4.x app using Passport for authentication with username and password.",
 | 
					 | 
				
			||||||
  "keywords": [
 | 
					 | 
				
			||||||
    "example",
 | 
					 | 
				
			||||||
    "express",
 | 
					 | 
				
			||||||
    "passport"
 | 
					 | 
				
			||||||
  ],
 | 
					 | 
				
			||||||
  "author": "Jared Hanson",
 | 
					 | 
				
			||||||
  "license": "Unlicense",
 | 
					 | 
				
			||||||
  "homepage": "https://github.com/passport/express-4.x-local-example",
 | 
					 | 
				
			||||||
  "repository": {
 | 
					 | 
				
			||||||
    "type": "git",
 | 
					 | 
				
			||||||
    "url": "git://github.com/passport/express-4.x-local-example.git"
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  "bugs": {
 | 
					 | 
				
			||||||
    "url": "https://github.com/passport/express-4.x-local-example/issues"
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  "dependencies": {
 | 
					 | 
				
			||||||
    "body-parser": "^1.19.0",
 | 
					 | 
				
			||||||
    "connect-ensure-login": "^0.1.1",
 | 
					 | 
				
			||||||
    "cookie-session": "^1.4.0",
 | 
					 | 
				
			||||||
    "ejs": "^2.6.2",
 | 
					 | 
				
			||||||
    "express": "^4.17.1",
 | 
					 | 
				
			||||||
    "express-session": "^1.16.1",
 | 
					 | 
				
			||||||
    "morgan": "^1.9.1",
 | 
					 | 
				
			||||||
    "passport": "^0.4.0",
 | 
					 | 
				
			||||||
    "passport-cookie": "^1.0.9",
 | 
					 | 
				
			||||||
    "passport-google-oauth": "^2.0.0",
 | 
					 | 
				
			||||||
    "passport-http": "^0.3.0",
 | 
					 | 
				
			||||||
    "passport-local": "^1.0.0"
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  "devDependencies": {
 | 
					 | 
				
			||||||
    "cookie-parser": "^1.4.5"
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										167
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										167
									
								
								server.js
									
									
									
									
									
								
							@@ -1,167 +0,0 @@
 | 
				
			|||||||
var express = require('express');
 | 
					 | 
				
			||||||
var passport = require('passport');
 | 
					 | 
				
			||||||
var Strategy = require('passport-local').Strategy;
 | 
					 | 
				
			||||||
var db = require('./db');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Google
 | 
					 | 
				
			||||||
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//Digest
 | 
					 | 
				
			||||||
var DigestStrategy = require('passport-http').DigestStrategy
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
passport.use(new DigestStrategy({ qop: 'auth' },
 | 
					 | 
				
			||||||
    function(username, done) {
 | 
					 | 
				
			||||||
        db.users.findByUsername(username, function (err, user) {
 | 
					 | 
				
			||||||
            console.log("User")
 | 
					 | 
				
			||||||
            console.log(user)
 | 
					 | 
				
			||||||
            if (err) { return done(err); }
 | 
					 | 
				
			||||||
            if (!user) { return done(null, false); }
 | 
					 | 
				
			||||||
            return done(null, user, user.password);
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Configure the local strategy for use by Passport.
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// The local strategy requires a `verify` function which receives the credentials
 | 
					 | 
				
			||||||
// (`username` and `password`) submitted by the user.  The function must verify
 | 
					 | 
				
			||||||
// that the password is correct and then invoke `cb` with a user object, which
 | 
					 | 
				
			||||||
// will be set at `req.user` in route handlers after authentication.
 | 
					 | 
				
			||||||
passport.use(new Strategy(
 | 
					 | 
				
			||||||
  function(username, password, cb) {
 | 
					 | 
				
			||||||
    db.users.findByUsername(username, function(err, user) {
 | 
					 | 
				
			||||||
      if (err) { return cb(err); }
 | 
					 | 
				
			||||||
      if (!user) { return cb(null, false); }
 | 
					 | 
				
			||||||
      if (user.password != password) { return cb(null, false); }
 | 
					 | 
				
			||||||
      return cb(null, user);
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
  }));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Use the GoogleStrategy within Passport.
 | 
					 | 
				
			||||||
//   Strategies in passport require a `verify` function, which accept
 | 
					 | 
				
			||||||
//   credentials (in this case, a token, tokenSecret, and Google profile), and
 | 
					 | 
				
			||||||
//   invoke a callback with a user object.
 | 
					 | 
				
			||||||
passport.use(new GoogleStrategy({
 | 
					 | 
				
			||||||
        clientSecret: 'YEMhoekMa89lFFGesgx7o23L',
 | 
					 | 
				
			||||||
        clientID: '796153694464-k1qqko2fgqtqg3n5teiq2pape1cjkaud.apps.googleusercontent.com',
 | 
					 | 
				
			||||||
        callbackURL: "http://localhost:4000/auth/google/callback"
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    function(token, tokenSecret, profile, done) {
 | 
					 | 
				
			||||||
        console.log("pro")
 | 
					 | 
				
			||||||
        console.log(profile)
 | 
					 | 
				
			||||||
        db.users.findByUsernameGoogle({ googleId: profile.id }, function (err, user) {
 | 
					 | 
				
			||||||
            console.log("user")
 | 
					 | 
				
			||||||
            console.log(user)
 | 
					 | 
				
			||||||
            if (err) { return done(err); }
 | 
					 | 
				
			||||||
            if(user == null || user == {}){
 | 
					 | 
				
			||||||
                var newUser = {username: profile.name.givenName, passwort: '', displayName: profile.displayName, emails: [{value: 'ONLY Google Login'}], googleId: '108136196294121041031'}
 | 
					 | 
				
			||||||
                db.users.createUser(newUser, function (err, user){
 | 
					 | 
				
			||||||
                    return done(err, user)
 | 
					 | 
				
			||||||
                })
 | 
					 | 
				
			||||||
            }else {
 | 
					 | 
				
			||||||
                return done(err, user);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Configure Passport authenticated session persistence.
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// In order to restore authentication state across HTTP requests, Passport needs
 | 
					 | 
				
			||||||
// to serialize users into and deserialize users out of the session.  The
 | 
					 | 
				
			||||||
// typical implementation of this is as simple as supplying the user ID when
 | 
					 | 
				
			||||||
// serializing, and querying the user record by ID from the database when
 | 
					 | 
				
			||||||
// deserializing.
 | 
					 | 
				
			||||||
passport.serializeUser(function(user, cb) {
 | 
					 | 
				
			||||||
  cb(null, user.id);
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
passport.deserializeUser(function(id, cb) {
 | 
					 | 
				
			||||||
  db.users.findById(id, function (err, user) {
 | 
					 | 
				
			||||||
    if (err) { return cb(err); }
 | 
					 | 
				
			||||||
    cb(null, user);
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Create a new Express application.
 | 
					 | 
				
			||||||
var app = express();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Configure view engine to render EJS templates.
 | 
					 | 
				
			||||||
app.set('views', __dirname + '/views');
 | 
					 | 
				
			||||||
app.set('view engine', 'ejs');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Use application-level middleware for common functionality, including
 | 
					 | 
				
			||||||
// logging, parsing, and session handling.
 | 
					 | 
				
			||||||
app.use(require('morgan')('combined'));
 | 
					 | 
				
			||||||
app.use(require('body-parser').urlencoded({ extended: true }));
 | 
					 | 
				
			||||||
app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Initialize Passport and restore authentication state, if any, from the
 | 
					 | 
				
			||||||
// session.
 | 
					 | 
				
			||||||
app.use(passport.initialize());
 | 
					 | 
				
			||||||
app.use(passport.session());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Define routes.
 | 
					 | 
				
			||||||
app.get('/',
 | 
					 | 
				
			||||||
  function(req, res) {
 | 
					 | 
				
			||||||
    res.render('home', { user: req.user });
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
app.get('/login',
 | 
					 | 
				
			||||||
  function(req, res){
 | 
					 | 
				
			||||||
    res.render('login');
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
  
 | 
					 | 
				
			||||||
app.post('/login', 
 | 
					 | 
				
			||||||
  passport.authenticate('local', { failureRedirect: '/login' }),
 | 
					 | 
				
			||||||
  function(req, res) {
 | 
					 | 
				
			||||||
    res.redirect('/');
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
  
 | 
					 | 
				
			||||||
app.get('/logout',
 | 
					 | 
				
			||||||
  function(req, res){
 | 
					 | 
				
			||||||
    req.logout();
 | 
					 | 
				
			||||||
    res.redirect('/');
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
app.get('/profile',
 | 
					 | 
				
			||||||
  require('connect-ensure-login').ensureLoggedIn(),
 | 
					 | 
				
			||||||
  function(req, res){
 | 
					 | 
				
			||||||
    res.render('profile', { user: req.user });
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
app.get('/digest',
 | 
					 | 
				
			||||||
    passport.authenticate('digest', { session: false }),
 | 
					 | 
				
			||||||
    function(req, res) {
 | 
					 | 
				
			||||||
        res.send("<a href=\"/logout\"><button>Logout</button></a> Hallo: " + req.user.displayName);
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // GET /auth/google
 | 
					 | 
				
			||||||
    //   Use passport.authenticate() as route middleware to authenticate the
 | 
					 | 
				
			||||||
    //   request.  The first step in Google authentication will involve redirecting
 | 
					 | 
				
			||||||
    //   the user to google.com.  After authorization, Google will redirect the user
 | 
					 | 
				
			||||||
    //   back to this application at /auth/google/callback
 | 
					 | 
				
			||||||
    app.get('/auth/google',
 | 
					 | 
				
			||||||
        passport.authenticate('google', { scope: 'https://www.googleapis.com/auth/plus.login' }));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // GET /auth/google/callback
 | 
					 | 
				
			||||||
    //   Use passport.authenticate() as route middleware to authenticate the
 | 
					 | 
				
			||||||
    //   request.  If authentication fails, the user will be redirected back to the
 | 
					 | 
				
			||||||
    //   login page.  Otherwise, the primary route function function will be called,
 | 
					 | 
				
			||||||
    //   which, in this example, will redirect the user to the home page.
 | 
					 | 
				
			||||||
    app.get('/auth/google/callback',
 | 
					 | 
				
			||||||
        passport.authenticate('google', { failureRedirect: '/' }),
 | 
					 | 
				
			||||||
        function(req, res) {
 | 
					 | 
				
			||||||
            res.redirect('/');
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
app.listen(4000);
 | 
					 | 
				
			||||||
@@ -1,9 +0,0 @@
 | 
				
			|||||||
<% if (!user) { %>
 | 
					 | 
				
			||||||
	<p>Willkommen! <br></p>
 | 
					 | 
				
			||||||
	<p><a href="/login"><button>Email Login per Formular</button></a></p>
 | 
					 | 
				
			||||||
	<p><br><a href="/auth/google"><button>Google Login</button></a></p>
 | 
					 | 
				
			||||||
	<p><br><a href="/digest"><button>Digest Login</button></a></p>
 | 
					 | 
				
			||||||
<% } else { %>
 | 
					 | 
				
			||||||
	<p>Hello, <%= user.username %>. View your <a href="/profile"><button>Profile</button></a>.</p>
 | 
					 | 
				
			||||||
	<p><a href="/logout"><button>Log out</button></a></p>
 | 
					 | 
				
			||||||
<% } %>
 | 
					 | 
				
			||||||
@@ -1,15 +0,0 @@
 | 
				
			|||||||
<form action="/login" method="post">
 | 
					 | 
				
			||||||
	<div>
 | 
					 | 
				
			||||||
	<label>Username:</label>
 | 
					 | 
				
			||||||
	<input type="text" name="username"/><br/>
 | 
					 | 
				
			||||||
	</div>
 | 
					 | 
				
			||||||
	<div>
 | 
					 | 
				
			||||||
	<label>Password:</label>
 | 
					 | 
				
			||||||
	<input type="password" name="password"/>
 | 
					 | 
				
			||||||
	</div>
 | 
					 | 
				
			||||||
	<div>
 | 
					 | 
				
			||||||
	<input type="submit" value="Submit"/>
 | 
					 | 
				
			||||||
	</div>
 | 
					 | 
				
			||||||
</form>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
<a href="/auth/google"><button>Zum Google Login</button></a>
 | 
					 | 
				
			||||||
@@ -1,12 +0,0 @@
 | 
				
			|||||||
<p>
 | 
					 | 
				
			||||||
ID: <%= user.id %><br/>
 | 
					 | 
				
			||||||
Username: <%= user.username %><br/>
 | 
					 | 
				
			||||||
Name: <%= user.displayName %><br/>
 | 
					 | 
				
			||||||
GoogleId: <%= user.googleId %><br/>
 | 
					 | 
				
			||||||
<% if (user.emails) { %>
 | 
					 | 
				
			||||||
Email: <%= user.emails[0].value %><br/>
 | 
					 | 
				
			||||||
<% } %>
 | 
					 | 
				
			||||||
</p>
 | 
					 | 
				
			||||||
<p><a href="/logout"><button>Log out</button></a></p>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
<p><a href="/"><button>Home</button></a></p>
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user