-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 Simper, leaner version of superlogin
No more session caching
- Loading branch information
Showing
15 changed files
with
154 additions
and
574 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ on: | |
- master | ||
- release | ||
- dev | ||
- minimal | ||
|
||
jobs: | ||
test: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,21 @@ | ||
'use strict'; | ||
|
||
import { hashPassword, verifyPassword } from './util'; | ||
import { FileAdapter } from './sessionAdapters/FileAdapter'; | ||
import { MemoryAdapter } from './sessionAdapters/MemoryAdapter'; | ||
import { RedisAdapter } from './sessionAdapters/RedisAdapter'; | ||
|
||
const extend = require('util')._extend; | ||
|
||
const tokenPrefix = 'token'; | ||
import { LocalHashObj } from './types/typings'; | ||
import { verifyPassword } from './util'; | ||
|
||
export class Session { | ||
#adapter; | ||
static invalidMsg = 'invalid token'; | ||
constructor(config) { | ||
let adapter; | ||
const sessionAdapter = config.getItem('session.adapter'); | ||
if (sessionAdapter === 'redis') { | ||
adapter = new RedisAdapter(config); | ||
} else if (sessionAdapter === 'file') { | ||
adapter = new FileAdapter(config); | ||
} else { | ||
adapter = new MemoryAdapter(); | ||
} | ||
this.#adapter = adapter; | ||
} | ||
constructor(config?) {} | ||
|
||
storeToken(token) { | ||
token = extend({}, token); | ||
if (!token.password && token.salt && token.derived_key) { | ||
return this.#adapter | ||
.storeKey( | ||
tokenPrefix + ':' + token.key, | ||
token.expires - Date.now(), | ||
JSON.stringify(token) | ||
) | ||
.then(() => { | ||
delete token.salt; | ||
delete token.derived_key; | ||
return Promise.resolve(token); | ||
}); | ||
} | ||
return hashPassword(token.password) | ||
.then(hash => { | ||
token.salt = hash.salt; | ||
token.derived_key = hash.derived_key; | ||
delete token.password; | ||
return this.#adapter.storeKey( | ||
tokenPrefix + ':' + token.key, | ||
token.expires - Date.now(), | ||
JSON.stringify(token) | ||
); | ||
}) | ||
.then(() => { | ||
delete token.salt; | ||
delete token.derived_key; | ||
return Promise.resolve(token); | ||
}); | ||
} | ||
|
||
deleteTokens(keys) { | ||
const entries = []; | ||
if (!(keys instanceof Array)) { | ||
keys = [keys]; | ||
} | ||
keys.forEach(key => { | ||
entries.push(tokenPrefix + ':' + key); | ||
}); | ||
return this.#adapter.deleteKeys(entries); | ||
} | ||
|
||
async confirmToken(key: string, password: string) { | ||
/** Confirms the token and removes the information that should not be sent to the client */ | ||
async confirmToken(token: LocalHashObj, password: string) { | ||
try { | ||
const result = await this.#adapter.getKey(tokenPrefix + ':' + key); | ||
if (!result) { | ||
throw Session.invalidMsg; | ||
} | ||
const token = JSON.parse(result); | ||
await verifyPassword(token, password); | ||
delete token.salt; | ||
delete token.derived_key; | ||
delete token.iterations; | ||
return token; | ||
} catch (error) { | ||
throw Session.invalidMsg; | ||
} | ||
} | ||
/** | ||
* retrieved the key from the session adapter | ||
*/ | ||
fetchToken(key: string) { | ||
return this.#adapter.getKey(tokenPrefix + ':' + key).then(result => { | ||
return Promise.resolve(JSON.parse(result)); | ||
}); | ||
} | ||
quit() { | ||
return this.#adapter.quit(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.