From 9bbe0799bd149b8599585d7676f4839f4a3be489 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 8 Dec 2014 16:20:08 +0000 Subject: [PATCH] Allow adminAuth setting to provide functions --- red/api/auth/strategies.js | 19 ++++----- red/api/auth/users.js | 80 +++++++++++++++++++++++++------------- 2 files changed, 62 insertions(+), 37 deletions(-) diff --git a/red/api/auth/strategies.js b/red/api/auth/strategies.js index 8300b47a6..f128e87ed 100644 --- a/red/api/auth/strategies.js +++ b/red/api/auth/strategies.js @@ -19,15 +19,16 @@ var ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy var crypto = require("crypto"); -var tokens = require("./tokens"); -var users = require("./users"); -var clients = require("./clients"); +var Tokens = require("./tokens"); +var Users = require("./users"); +var Clients = require("./clients"); var bearerStrategy = function (accessToken, done) { // is this a valid token? - tokens.get(accessToken).then(function(token) { + Tokens.get(accessToken).then(function(token) { if (token) { - users.get(token.user).then(function(user) { + Users.get(token.user).then(function(user) { + console.log(user); if (user) { done(null,{username:user.username},{scope:token.scope}); } else { @@ -42,7 +43,7 @@ var bearerStrategy = function (accessToken, done) { bearerStrategy.BearerStrategy = new BearerStrategy(bearerStrategy); var clientPasswordStrategy = function(clientId, clientSecret, done) { - clients.get(clientId).then(function(client) { + Clients.get(clientId).then(function(client) { if (client && client.secret == clientSecret) { done(null,client); } else { @@ -53,9 +54,9 @@ var clientPasswordStrategy = function(clientId, clientSecret, done) { clientPasswordStrategy.ClientPasswordStrategy = new ClientPasswordStrategy(clientPasswordStrategy); var passwordTokenExchange = function(client, username, password, scope, done) { - users.get(username).then(function(user) { - if (user && user.password == crypto.createHash('md5').update(password,'utf8').digest('hex')) { - tokens.create(username,client.id,scope).then(function(token) { + Users.get(username,password).then(function(user) { + if (user) { + Tokens.create(username,client.id,scope).then(function(token) { done(null,token); }); } else { diff --git a/red/api/auth/users.js b/red/api/auth/users.js index 5eb688b66..2e6a8980c 100644 --- a/red/api/auth/users.js +++ b/red/api/auth/users.js @@ -1,40 +1,64 @@ /** - * Copyright 2014 IBM Corp. - * - * 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. - **/ - +* Copyright 2014 IBM Corp. +* +* 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 when = require("when"); var crypto = require("crypto"); +var util = require("util"); var settings = require("../../settings"); //{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.user && settings.adminAuth.pass) { - users.push({username:settings.adminAuth.user, password:settings.adminAuth.pass}); + if (settings.adminAuth.type == "credentials") { + if (settings.adminAuth.users) { + if (util.isArray(settings.adminAuth.users)) { + for (var i=0;i