Allow adminAuth setting to provide functions

This commit is contained in:
Nick O'Leary 2014-12-08 16:20:08 +00:00
parent f3eb85c449
commit 9bbe0799bd
2 changed files with 62 additions and 37 deletions

View File

@ -19,15 +19,16 @@ var ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy
var crypto = require("crypto"); var crypto = require("crypto");
var tokens = require("./tokens"); var Tokens = require("./tokens");
var users = require("./users"); var Users = require("./users");
var clients = require("./clients"); var Clients = require("./clients");
var bearerStrategy = function (accessToken, done) { var bearerStrategy = function (accessToken, done) {
// is this a valid token? // is this a valid token?
tokens.get(accessToken).then(function(token) { Tokens.get(accessToken).then(function(token) {
if (token) { if (token) {
users.get(token.user).then(function(user) { Users.get(token.user).then(function(user) {
console.log(user);
if (user) { if (user) {
done(null,{username:user.username},{scope:token.scope}); done(null,{username:user.username},{scope:token.scope});
} else { } else {
@ -42,7 +43,7 @@ var bearerStrategy = function (accessToken, done) {
bearerStrategy.BearerStrategy = new BearerStrategy(bearerStrategy); bearerStrategy.BearerStrategy = new BearerStrategy(bearerStrategy);
var clientPasswordStrategy = function(clientId, clientSecret, done) { var clientPasswordStrategy = function(clientId, clientSecret, done) {
clients.get(clientId).then(function(client) { Clients.get(clientId).then(function(client) {
if (client && client.secret == clientSecret) { if (client && client.secret == clientSecret) {
done(null,client); done(null,client);
} else { } else {
@ -53,9 +54,9 @@ var clientPasswordStrategy = function(clientId, clientSecret, done) {
clientPasswordStrategy.ClientPasswordStrategy = new ClientPasswordStrategy(clientPasswordStrategy); clientPasswordStrategy.ClientPasswordStrategy = new ClientPasswordStrategy(clientPasswordStrategy);
var passwordTokenExchange = function(client, username, password, scope, done) { var passwordTokenExchange = function(client, username, password, scope, done) {
users.get(username).then(function(user) { Users.get(username,password).then(function(user) {
if (user && user.password == crypto.createHash('md5').update(password,'utf8').digest('hex')) { if (user) {
tokens.create(username,client.id,scope).then(function(token) { Tokens.create(username,client.id,scope).then(function(token) {
done(null,token); done(null,token);
}); });
} else { } else {

View File

@ -1,40 +1,64 @@
/** /**
* Copyright 2014 IBM Corp. * Copyright 2014 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
**/ **/
var when = require("when"); var when = require("when");
var crypto = require("crypto"); var crypto = require("crypto");
var util = require("util");
var settings = require("../../settings"); var settings = require("../../settings");
//{username:"nick",password:crypto.createHash('md5').update("foo",'utf8').digest('hex')} //{username:"nick",password:crypto.createHash('md5').update("foo",'utf8').digest('hex')}
var users = []; var users = {};
var passwords = {};
var api = {};
if (settings.adminAuth) { if (settings.adminAuth) {
if (settings.adminAuth.user && settings.adminAuth.pass) { if (settings.adminAuth.type == "credentials") {
users.push({username:settings.adminAuth.user, password:settings.adminAuth.pass}); if (settings.adminAuth.users) {
if (util.isArray(settings.adminAuth.users)) {
for (var i=0;i<settings.adminAuth.users.length;i++) {
var u = settings.adminAuth.users[i];
users[u.username] = {
"username":u.username
};
passwords[u.username] = u.password;
}
var api = {
get: function(username) {
return when.resolve(users[username]);
},
authenticate: function(username,password) {
return api.get(username).then(function(user) {
if (user) {
var pass = crypto.createHash('md5').update(password,'utf8').digest('hex');
if (pass == passwords[username]) {
return when.resolve(user);
}
}
return when.resolve(null);
});
}
}
} else {
api = settings.adminAuth.users;
}
}
} }
} }
module.exports = { module.exports = api;
get: function(username) {
for (var i=0;i<users.length;i++) {
if (users[i].username == username) {
return when.resolve(users[i]);
}
}
return when.resolve(null);
}
}