forked from openhab/openhab-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves auth to one pleace, cleans up auth straragies (openhab#60)
Signed-off-by: Dan Cunningham <[email protected]>
- Loading branch information
1 parent
1764721
commit d276743
Showing
3 changed files
with
198 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,108 @@ | ||
var passport = require('passport') | ||
, LocalStrategy = require('passport-local').Strategy | ||
, BasicStrategy = require('passport-http').BasicStrategy | ||
, ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy | ||
, BearerStrategy = require('passport-http-bearer').Strategy | ||
, User = require('./models/user'); | ||
var passport = require('passport'), | ||
LocalStrategy = require('passport-local').Strategy, | ||
BasicStrategy = require('passport-http').BasicStrategy, | ||
ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy, | ||
BearerStrategy = require('passport-http-bearer').Strategy, | ||
OAuth2Client = require('./models/oauth2client'), | ||
OAuth2Token = require('./models/oauth2token'), | ||
User = require('./models/user'); | ||
|
||
|
||
// Local authentication strategy for passportjs | ||
// Local authentication strategy for passportjs, used for web logins | ||
passport.use(new LocalStrategy({ | ||
usernameField: 'username'}, | ||
function(username, password, done){ | ||
User.authenticate(username, password, function(err, user, params) { | ||
usernameField: 'username' | ||
}, | ||
function (username, password, done) { | ||
User.authenticate(username, password, function (err, user, params) { | ||
return done(err, user, params); | ||
}); | ||
})); | ||
|
||
passport.serializeUser(function(user, done) { | ||
done(null, user._id); | ||
}); | ||
|
||
passport.deserializeUser(function(id, done) { | ||
User.findById(id, function (err, user) { | ||
done(err, user); | ||
}); | ||
}); | ||
|
||
passport.use(new BasicStrategy( | ||
function(username, password, done) { | ||
// standard basic authentication strategy, used for REST based logins | ||
passport.use(new BasicStrategy( | ||
function (username, password, done) { | ||
User.authenticate(username, password, function (err, user, params) { | ||
return done(err, user, params); | ||
}); | ||
} | ||
)); | ||
|
||
// authentication strategy used by oauth clients, usess a custom name 'oAuthBasic' | ||
passport.use('oAuthBasic' , new BasicStrategy( | ||
function (username, password, done) { | ||
OAuth2Client.findOne({ | ||
clientId: username | ||
}, function (error, client) { | ||
if (error) { | ||
return done(error); | ||
} | ||
if (!client) { | ||
return done(null, false); | ||
} | ||
if (client.clientSecret != password) { | ||
return done(null, false); | ||
} | ||
return done(null, client); | ||
}); | ||
} | ||
)); | ||
|
||
// A client-password strategy for authorizing requests for tokens | ||
passport.use(new ClientPasswordStrategy( | ||
function(clientId, clientSecret, done) { | ||
|
||
function (clientId, clientSecret, done) { | ||
OAuth2Client.findOne({ | ||
clientId: clientId | ||
}, function (error, client) { | ||
if (error) { | ||
return done(error); | ||
} | ||
if (!client) { | ||
return done(null, false); | ||
} | ||
if (client.clientSecret != clientSecret) { | ||
return done(null, false); | ||
} | ||
return done(null, client); | ||
}); | ||
} | ||
)); | ||
|
||
// A bearer strategy to authorize API requests by oauth2code | ||
passport.use(new BearerStrategy( | ||
function(accessToken, done) { | ||
|
||
function (accessToken, done) { | ||
OAuth2Token.findOne({ | ||
token: accessToken | ||
}, function (error, oauth2token) { | ||
if (error) { | ||
return done(error); | ||
} | ||
if (!oauth2token) { | ||
return done(null, false); | ||
} | ||
User.findOne({ | ||
_id: oauth2token.user | ||
}, function (error, openhabUser) { | ||
if (error) { | ||
return done(error); | ||
} | ||
if (!openhabUser) { | ||
return done(null, false); | ||
} | ||
var info = { | ||
scope: oauth2token.scope | ||
}; | ||
done(null, openhabUser, info); | ||
}); | ||
}); | ||
} | ||
)); | ||
|
||
passport.serializeUser(function (user, done) { | ||
done(null, user._id); | ||
}); | ||
|
||
passport.deserializeUser(function (id, done) { | ||
User.findById(id, function (err, user) { | ||
done(err, user); | ||
}); | ||
}); |
Oops, something went wrong.