2015-08-22 00:46:42 +03:00
|
|
|
var models = require('../models'),
|
2015-08-09 13:50:05 +03:00
|
|
|
strategies;
|
2014-06-30 16:58:10 +04:00
|
|
|
|
2015-08-09 13:50:05 +03:00
|
|
|
strategies = {
|
2014-06-30 16:58:10 +04:00
|
|
|
|
2015-08-09 13:50:05 +03:00
|
|
|
/**
|
|
|
|
* ClientPasswordStrategy
|
|
|
|
*
|
|
|
|
* This strategy is used to authenticate registered OAuth clients. It is
|
|
|
|
* employed to protect the `token` endpoint, which consumers use to obtain
|
|
|
|
* access tokens. The OAuth 2.0 specification suggests that clients use the
|
|
|
|
* HTTP Basic scheme to authenticate (not implemented yet).
|
|
|
|
* Use of the client password strategy is implemented to support ember-simple-auth.
|
|
|
|
*/
|
2015-08-22 00:46:42 +03:00
|
|
|
clientPasswordStrategy: function clientPasswordStrategy(clientId, clientSecret, done) {
|
2015-10-22 16:28:47 +03:00
|
|
|
return models.Client.findOne({slug: clientId}, {withRelated: ['trustedDomains']})
|
2015-08-09 13:50:05 +03:00
|
|
|
.then(function then(model) {
|
|
|
|
if (model) {
|
2015-10-22 16:28:47 +03:00
|
|
|
var client = model.toJSON({include: ['trustedDomains']});
|
2015-11-04 19:59:56 +03:00
|
|
|
if (client.status === 'enabled' && client.secret === clientSecret) {
|
2015-08-09 13:50:05 +03:00
|
|
|
return done(null, client);
|
|
|
|
}
|
2014-06-30 16:58:10 +04:00
|
|
|
}
|
2015-08-09 13:50:05 +03:00
|
|
|
return done(null, false);
|
|
|
|
});
|
2015-08-22 00:46:42 +03:00
|
|
|
},
|
2014-06-30 16:58:10 +04:00
|
|
|
|
2015-08-09 13:50:05 +03:00
|
|
|
/**
|
|
|
|
* BearerStrategy
|
|
|
|
*
|
|
|
|
* This strategy is used to authenticate users based on an access token (aka a
|
|
|
|
* bearer token). The user must have previously authorized a client
|
|
|
|
* application, which is issued an access token to make requests on behalf of
|
|
|
|
* the authorizing user.
|
|
|
|
*/
|
2015-08-22 00:46:42 +03:00
|
|
|
bearerStrategy: function bearerStrategy(accessToken, done) {
|
2015-10-16 21:23:18 +03:00
|
|
|
return models.Accesstoken.findOne({token: accessToken})
|
2015-08-09 13:50:05 +03:00
|
|
|
.then(function then(model) {
|
|
|
|
if (model) {
|
|
|
|
var token = model.toJSON();
|
|
|
|
if (token.expires > Date.now()) {
|
2015-10-16 21:23:18 +03:00
|
|
|
return models.User.findOne({id: token.user_id})
|
2015-08-22 00:46:42 +03:00
|
|
|
.then(function then(model) {
|
|
|
|
if (model) {
|
|
|
|
var user = model.toJSON(),
|
|
|
|
info = {scope: '*'};
|
|
|
|
return done(null, {id: user.id}, info);
|
|
|
|
}
|
|
|
|
return done(null, false);
|
|
|
|
});
|
2015-08-09 13:50:05 +03:00
|
|
|
} else {
|
2014-06-30 16:58:10 +04:00
|
|
|
return done(null, false);
|
2015-08-09 13:50:05 +03:00
|
|
|
}
|
2014-06-30 16:58:10 +04:00
|
|
|
} else {
|
|
|
|
return done(null, false);
|
|
|
|
}
|
2015-08-09 13:50:05 +03:00
|
|
|
});
|
2015-08-22 00:46:42 +03:00
|
|
|
}
|
2015-08-09 13:50:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = strategies;
|