Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
702 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict' | ||
|
||
/* | ||
* adonis-auth | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
const debug = require('debug')('adonis:auth') | ||
|
||
class Auth { | ||
constructor (Config) { | ||
const authenticator = Config.get('auth.authenticator') | ||
this.scheme = Config.get(`auth.${authenticator}.scheme`, null) | ||
} | ||
|
||
/** | ||
* Authenticate the user using one of the defined | ||
* schemes or the default scheme | ||
* | ||
* @method handle | ||
* | ||
* @param {Object} options.auth | ||
* @param {Function} next | ||
* | ||
* @return {void} | ||
*/ | ||
async handle ({ auth }, next, schemes) { | ||
let lastError = null | ||
let authenticatedScheme = null | ||
|
||
schemes = schemes instanceof Array === true ? schemes : [this.scheme] | ||
debug('attempting to authenticate via %j scheme(s)', schemes) | ||
|
||
/** | ||
* Loop over all the defined schemes and wait until use is logged | ||
* via anyone | ||
*/ | ||
for (const scheme of schemes) { | ||
try { | ||
await auth.authenticator(scheme).check() | ||
debug('authenticated using %s scheme', scheme) | ||
authenticatedScheme = scheme | ||
lastError = null | ||
break | ||
} catch (error) { | ||
debug('authentication failed using %s scheme', scheme) | ||
lastError = error | ||
} | ||
} | ||
|
||
/** | ||
* If there is an error from all the schemes | ||
* then throw it back | ||
*/ | ||
if (lastError) { | ||
throw lastError | ||
} | ||
|
||
/** | ||
* If user got logged then set the `current` property | ||
* on auth, which is reference to the scheme via | ||
* which user got authenticated. | ||
*/ | ||
if (authenticatedScheme) { | ||
/** | ||
* If logged in scheme is same as the default scheme, the reference | ||
* the actual authenticator instance, otherwise create a new | ||
* one for the scheme via which user got authenticated | ||
*/ | ||
auth.current = authenticatedScheme === this.scheme | ||
? auth.authenticatorInstance | ||
: auth.authenticator(authenticatedScheme) | ||
} | ||
|
||
await next() | ||
} | ||
} | ||
|
||
module.exports = Auth |
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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
'use strict' | ||
|
||
/* | ||
* adonis-auth | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
const uuid = require('uuid') | ||
const BaseScheme = require('./Base') | ||
const GE = require('@adonisjs/generic-exceptions') | ||
const CE = require('../Exceptions') | ||
|
||
class ApiScheme extends BaseScheme { | ||
/** | ||
* Validate user credentials | ||
* | ||
* @method validate | ||
* | ||
* @param {String} uid | ||
* @param {String} password | ||
* @param {Boolean} returnUser | ||
* | ||
* @return {Object} | ||
* | ||
* @throws {UserNotFoundException} If unable to find user with uid | ||
* @throws {PasswordMisMatchException} If password mismatches | ||
*/ | ||
async validate (uid, password, returnUser) { | ||
const user = await this._serializerInstance.findByUid(uid) | ||
if (!user) { | ||
throw CE.UserNotFoundException.invoke(`Cannot find user with ${this._config.uid} as ${uid}`) | ||
} | ||
|
||
const validated = await this._serializerInstance.validateCredentails(user, password) | ||
if (!validated) { | ||
throw CE.PasswordMisMatchException.invoke('Cannot verify user password') | ||
} | ||
|
||
return returnUser ? user : !!user | ||
} | ||
|
||
/** | ||
* Attempt to valid the user credentials and then | ||
* generates a new token for it. | ||
* | ||
* @method attempt | ||
* | ||
* @param {String} uid | ||
* @param {String} password | ||
* | ||
* @return {String} | ||
*/ | ||
async attempt (uid, password) { | ||
const user = await this.validate(uid, password, true) | ||
return this.generate(user) | ||
} | ||
|
||
/** | ||
* Generates a personal API token for a user | ||
* | ||
* @method generate | ||
* @async | ||
* | ||
* @param {Object} user | ||
* | ||
* @return {Object} | ||
*/ | ||
async generate (user) { | ||
/** | ||
* Throw exception when user is not persisted to | ||
* database | ||
*/ | ||
const userId = user[this.primaryKey] | ||
if (!userId) { | ||
throw GE.RuntimeException.invoke('Primary key value is missing for user') | ||
} | ||
|
||
const token = uuid.v4().replace(/-/g, '') | ||
await this._serializerInstance.saveToken(user, token, 'api_token') | ||
return { type: 'bearer', token } | ||
} | ||
|
||
/** | ||
* Check whether the api token has been passed | ||
* in the request header and is it valid or | ||
* not. | ||
* | ||
* @method check | ||
* | ||
* @return {Boolean} | ||
*/ | ||
async check () { | ||
if (this.user) { | ||
return true | ||
} | ||
|
||
const token = this.getAuthHeader() | ||
if (!token) { | ||
throw CE.InvalidApiToken.invoke() | ||
} | ||
|
||
this.user = await this._serializerInstance.findByToken(token, 'api_token') | ||
|
||
/** | ||
* Throw exception when user is not found | ||
*/ | ||
if (!this.user) { | ||
throw CE.InvalidApiToken.invoke() | ||
} | ||
return true | ||
} | ||
|
||
/** | ||
* Makes sure user is loggedin and then | ||
* returns the user back | ||
* | ||
* @method getUser | ||
* | ||
* @return {Object} | ||
*/ | ||
async getUser () { | ||
await this.check() | ||
return this.user | ||
} | ||
} | ||
|
||
module.exports = ApiScheme |
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
Oops, something went wrong.