Moved out Auth cache to authenticator class

This commit is contained in:
Kasra Bigdeli
2019-02-26 23:13:35 -08:00
parent 1c51fc21ed
commit 0213fe8bca
5 changed files with 46 additions and 34 deletions
+5 -5
View File
@@ -51,7 +51,7 @@ export function injectUser() {
const namespace = res.locals.namespace
CaptainManager.getAuthenticator(namespace)
Authenticator.getAuthenticator(namespace)
.decodeAuthToken(req.header(CaptainConstants.headerAuth) || '')
.then(function(userDecoded) {
if (userDecoded) {
@@ -59,7 +59,7 @@ export function injectUser() {
const serviceManager = ServiceManager.get(
namespace,
CaptainManager.getAuthenticator(namespace),
Authenticator.getAuthenticator(namespace),
datastore,
dockerApi,
CaptainManager.get().getLoadBalanceManager(),
@@ -109,7 +109,7 @@ export function injectUserForWebhook() {
let decodedInfo: UserModel.IAppWebHookToken
CaptainManager.getAuthenticator(namespace)
Authenticator.getAuthenticator(namespace)
.decodeAppPushWebhookToken(token)
.then(function(data) {
decodedInfo = data
@@ -132,7 +132,7 @@ export function injectUserForWebhook() {
const serviceManager = ServiceManager.get(
namespace,
CaptainManager.getAuthenticator(namespace),
Authenticator.getAuthenticator(namespace),
datastore,
dockerApi,
CaptainManager.get().getLoadBalanceManager(),
@@ -166,7 +166,7 @@ export function injectUserForWebhook() {
*/
export function injectUserUsingCookieDataOnly() {
return function(req: Request, res: Response, next: NextFunction) {
CaptainManager.getAuthenticator(CaptainConstants.rootNameSpace)
Authenticator.getAuthenticator(CaptainConstants.rootNameSpace)
.decodeAuthTokenFromCookies(
req.cookies[CaptainConstants.headerCookieAuth]
)
+3 -2
View File
@@ -5,6 +5,7 @@ import CaptainConstants = require('../utils/CaptainConstants')
import InjectionExtractor = require('../injection/InjectionExtractor')
import DataStoreProvider = require('../datastore/DataStoreProvider')
import CaptainManager = require('../user/system/CaptainManager')
import Authenticator = require('../user/Authenticator')
const router = express.Router()
@@ -33,14 +34,14 @@ router.post('/', function(req, res, next) {
})
.then(function(savedHashedPassword) {
loadedHashedPassword = savedHashedPassword
return CaptainManager.getAuthenticator(namespace).getAuthToken(
return Authenticator.getAuthenticator(namespace).getAuthToken(
password,
loadedHashedPassword
)
})
.then(function(token) {
authToken = token
return CaptainManager.getAuthenticator(
return Authenticator.getAuthenticator(
namespace
).getAuthTokenForCookies(password, loadedHashedPassword)
})
+2 -1
View File
@@ -11,6 +11,7 @@ import InjectionExtractor = require('../injection/InjectionExtractor')
import CaptainManager = require('../user/system/CaptainManager')
import Utils from '../utils/Utils'
import EnvVars = require('../utils/EnvVars')
import Authenticator = require('../user/Authenticator')
const router = express.Router()
@@ -106,7 +107,7 @@ router.post('/changepassword/', function(req, res, next) {
return dataStore.getHashedPassword()
})
.then(function(savedHashedPassword) {
return CaptainManager.getAuthenticator(namespace).changepass(
return Authenticator.getAuthenticator(namespace).changepass(
req.body.oldPassword,
req.body.newPassword,
savedHashedPassword
+31
View File
@@ -245,6 +245,37 @@ class Authenticator {
)
})
}
static authenticatorCache: IHashMapGeneric<Authenticator> = {}
private static mainSalt: string
static setMainSalt(salt: string) {
if (Authenticator.mainSalt) throw new Error('Salt is already set!!')
Authenticator.mainSalt = salt
}
static getAuthenticator(namespace: string): Authenticator {
const authenticatorCache = Authenticator.authenticatorCache
if (!namespace) {
throw ApiStatusCodes.createError(
ApiStatusCodes.STATUS_ERROR_NOT_AUTHORIZED,
'Empty namespace'
)
}
if (!authenticatorCache[namespace]) {
const captainSalt = Authenticator.mainSalt
if (captainSalt) {
authenticatorCache[namespace] = new Authenticator(
captainSalt,
namespace
)
}
}
return authenticatorCache[namespace]
}
}
export = Authenticator
+5 -26
View File
@@ -196,6 +196,9 @@ class CaptainManager {
return true
})
.then(function() {
return Authenticator.setMainSalt(self.getCaptainSalt())
})
.then(function() {
return dataStore.setEncryptionSalt(self.getCaptainSalt())
})
@@ -205,7 +208,7 @@ class CaptainManager {
.then(function() {
return new MigrateCaptainDuckDuck(
dataStore,
CaptainManager.getAuthenticator(dataStore.getNameSpace())
Authenticator.getAuthenticator(dataStore.getNameSpace())
)
.migrateIfNeeded()
.then(function(migrationPerformed) {
@@ -453,7 +456,7 @@ class CaptainManager {
const promises: (() => Promise<void>)[] = []
const serviceManager = ServiceManager.get(
self.dataStore.getNameSpace(),
CaptainManager.getAuthenticator(
Authenticator.getAuthenticator(
self.dataStore.getNameSpace()
),
self.dataStore,
@@ -884,30 +887,6 @@ class CaptainManager {
})
}
static authenticatorCache: IHashMapGeneric<Authenticator> = {}
static getAuthenticator(namespace: string): Authenticator {
const authenticatorCache = CaptainManager.authenticatorCache
if (!namespace) {
throw ApiStatusCodes.createError(
ApiStatusCodes.STATUS_ERROR_NOT_AUTHORIZED,
'Empty namespace'
)
}
if (!authenticatorCache[namespace]) {
const captainSalt = CaptainManager.get().getCaptainSalt()
if (captainSalt) {
authenticatorCache[namespace] = new Authenticator(
captainSalt,
namespace
)
}
}
return authenticatorCache[namespace]
}
private static captainManagerInstance: CaptainManager | undefined
static get(): CaptainManager {