2014-11-06 23:59:48 +01:00
|
|
|
/**
|
2014-12-10 15:58:53 +01:00
|
|
|
* Copyright 2015 IBM Corp.
|
2014-11-06 23:59:48 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var passport = require("passport");
|
|
|
|
var oauth2orize = require("oauth2orize");
|
|
|
|
|
|
|
|
var strategies = require("./strategies");
|
2014-12-10 15:16:07 +01:00
|
|
|
var Tokens = require("./tokens");
|
|
|
|
var Users = require("./users");
|
2015-02-06 00:43:35 +01:00
|
|
|
var permissions = require("./permissions");
|
2014-11-06 23:59:48 +01:00
|
|
|
|
2015-02-06 00:43:35 +01:00
|
|
|
var settings = null;
|
2015-01-28 23:41:13 +01:00
|
|
|
var log = require("../../log");
|
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
|
|
|
|
passport.use(strategies.bearerStrategy.BearerStrategy);
|
|
|
|
passport.use(strategies.clientPasswordStrategy.ClientPasswordStrategy);
|
2014-12-10 15:16:07 +01:00
|
|
|
passport.use(strategies.anonymousStrategy);
|
2014-11-06 23:59:48 +01:00
|
|
|
|
|
|
|
var server = oauth2orize.createServer();
|
|
|
|
|
|
|
|
server.exchange(oauth2orize.exchange.password(strategies.passwordTokenExchange));
|
|
|
|
|
2015-03-14 00:37:59 +01:00
|
|
|
function init(_settings,storage) {
|
2015-02-06 00:43:35 +01:00
|
|
|
settings = _settings;
|
2015-01-28 23:41:13 +01:00
|
|
|
if (settings.adminAuth) {
|
|
|
|
Users.init(settings.adminAuth);
|
2015-03-30 15:14:32 +02:00
|
|
|
Tokens.init(settings.adminAuth,storage);
|
2015-01-28 23:41:13 +01:00
|
|
|
}
|
2014-12-10 15:16:07 +01:00
|
|
|
}
|
|
|
|
|
2015-02-06 00:43:35 +01:00
|
|
|
function needsPermission(permission) {
|
|
|
|
return function(req,res,next) {
|
2015-03-16 14:58:01 +01:00
|
|
|
if (settings && settings.adminAuth) {
|
2015-02-06 00:43:35 +01:00
|
|
|
return passport.authenticate(['bearer','anon'],{ session: false })(req,res,function() {
|
|
|
|
if (!req.user) {
|
|
|
|
return next();
|
|
|
|
}
|
2015-03-29 22:59:48 +02:00
|
|
|
if (permissions.hasPermission(req.authInfo.scope,permission)) {
|
2015-02-06 00:43:35 +01:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return res.send(401);
|
|
|
|
});
|
2014-11-06 23:59:48 +01:00
|
|
|
} else {
|
2015-02-06 00:43:35 +01:00
|
|
|
next();
|
2014-11-06 23:59:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureClientSecret(req,res,next) {
|
|
|
|
if (!req.body.client_secret) {
|
|
|
|
req.body.client_secret = 'not_available';
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
function authenticateClient(req,res,next) {
|
|
|
|
return passport.authenticate(['oauth2-client-password'], {session: false})(req,res,next);
|
|
|
|
}
|
|
|
|
function getToken(req,res,next) {
|
|
|
|
return server.token()(req,res,next);
|
|
|
|
}
|
|
|
|
|
2014-11-11 11:15:02 +01:00
|
|
|
function login(req,res) {
|
2015-03-25 21:32:40 +01:00
|
|
|
var response = {};
|
|
|
|
if (settings.adminAuth) {
|
|
|
|
response = {
|
|
|
|
"type":"credentials",
|
|
|
|
"prompts":[{id:"username",type:"text",label:"Username"},{id:"password",type:"password",label:"Password"}]
|
|
|
|
}
|
2014-11-11 11:15:02 +01:00
|
|
|
}
|
|
|
|
res.json(response);
|
|
|
|
}
|
2014-11-06 23:59:48 +01:00
|
|
|
|
2014-11-12 14:21:39 +01:00
|
|
|
function revoke(req,res) {
|
|
|
|
var token = req.body.token;
|
2015-01-28 23:41:13 +01:00
|
|
|
// TODO: audit log
|
2014-12-10 15:16:07 +01:00
|
|
|
Tokens.revoke(token).then(function() {
|
2014-11-12 14:21:39 +01:00
|
|
|
res.send(200);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-06 23:59:48 +01:00
|
|
|
module.exports = {
|
2014-12-10 15:16:07 +01:00
|
|
|
init: init,
|
2015-02-06 00:43:35 +01:00
|
|
|
needsPermission: needsPermission,
|
2014-11-06 23:59:48 +01:00
|
|
|
ensureClientSecret: ensureClientSecret,
|
|
|
|
authenticateClient: authenticateClient,
|
|
|
|
getToken: getToken,
|
2015-01-28 23:41:13 +01:00
|
|
|
errorHandler: function(err,req,res,next) {
|
|
|
|
//TODO: audit log statment
|
2015-03-29 23:27:07 +02:00
|
|
|
//console.log(err.stack);
|
2015-01-28 23:41:13 +01:00
|
|
|
//log.log({level:"audit",type:"auth",msg:err.toString()});
|
|
|
|
return server.errorHandler()(err,req,res,next);
|
|
|
|
},
|
2014-11-12 14:21:39 +01:00
|
|
|
login: login,
|
|
|
|
revoke: revoke
|
2014-11-06 23:59:48 +01:00
|
|
|
}
|