@adonisjs/auth
Advanced tools
Comparing version 2.0.10 to 2.0.11
@@ -0,1 +1,19 @@ | ||
<a name="2.0.11"></a> | ||
## [2.0.11](https://github.com/adonisjs/adonis-auth/compare/v2.0.10...v2.0.11) (2018-01-12) | ||
### Bug Fixes | ||
* **auth middleware:** main instance of auth should point the valid user ([c0a0138](https://github.com/adonisjs/adonis-auth/commit/c0a0138)) | ||
* **auth middleware:** use default scheme in case of no runtime scheme ([54a5de1](https://github.com/adonisjs/adonis-auth/commit/54a5de1)) | ||
* **exceptions:** exceptions should use fields names of current authenticator ([9e53837](https://github.com/adonisjs/adonis-auth/commit/9e53837)), closes [#75](https://github.com/adonisjs/adonis-auth/issues/75) | ||
* **tokenschema:** generated tokens are bigger after encryption ([f85a4d2](https://github.com/adonisjs/adonis-auth/commit/f85a4d2)), closes [#77](https://github.com/adonisjs/adonis-auth/issues/77) | ||
### Features | ||
* **jwt:** add login() method to jwt scheme ([#72](https://github.com/adonisjs/adonis-auth/issues/72)) ([0466109](https://github.com/adonisjs/adonis-auth/commit/0466109)) | ||
<a name="2.0.10"></a> | ||
@@ -2,0 +20,0 @@ ## [2.0.10](https://github.com/adonisjs/adonis-auth/compare/v2.0.9...v2.0.10) (2017-10-31) |
{ | ||
"name": "@adonisjs/auth", | ||
"version": "2.0.10", | ||
"version": "2.0.11", | ||
"description": "Offical authentication provider for Adonis framework", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -89,4 +89,4 @@ 'use strict' | ||
return ({ Request, traits }) => { | ||
const sessionIndex = _.findIndex(traits, (triat) => triat.action === 'Session/Client') | ||
const authIndex = _.findIndex(traits, (triat) => triat.action === 'Auth/Client') | ||
const sessionIndex = _.findIndex(traits, (trait) => trait.action === 'Session/Client') | ||
const authIndex = _.findIndex(traits, (trait) => trait.action === 'Auth/Client') | ||
@@ -93,0 +93,0 @@ /** |
@@ -22,4 +22,12 @@ 'use strict' | ||
class UserNotFoundException extends GE.LogicalException { | ||
static invoke (message) { | ||
return new this(message, 401, 'E_USER_NOT_FOUND') | ||
static invoke (message, uidField, passwordField, authScheme) { | ||
if (!uidField || !passwordField || !authScheme) { | ||
throw new Error('Cannot invoke exception without uidField, passwordField or authScheme') | ||
} | ||
const error = new this(message, 401, 'E_USER_NOT_FOUND') | ||
error.uidField = uidField | ||
error.passwordField = passwordField | ||
error.authScheme = authScheme | ||
return error | ||
} | ||
@@ -43,5 +51,5 @@ | ||
*/ | ||
async handle ({ status }, { request, response, session, auth }) { | ||
async handle ({ status, uidField, passwordField, authScheme }, { request, response, session }) { | ||
const isJSON = request.accepts(['html', 'json']) === 'json' | ||
const errorMessages = [{ field: auth.uidField, message: `Cannot find user with provided ${auth.uidField}` }] | ||
const errorMessages = [{ field: uidField, message: `Cannot find user with provided ${uidField}` }] | ||
@@ -60,4 +68,4 @@ /** | ||
*/ | ||
if (auth.scheme === 'session') { | ||
session.withErrors(errorMessages).flashExcept([auth.passwordField]) | ||
if (authScheme === 'session') { | ||
session.withErrors(errorMessages).flashExcept([passwordField]) | ||
await session.commit() | ||
@@ -72,3 +80,3 @@ response.redirect('back') | ||
*/ | ||
if (auth.scheme === 'basic') { | ||
if (authScheme === 'basic') { | ||
response.header('WWW-Authenticate', 'Basic realm="example"') | ||
@@ -93,4 +101,11 @@ response.status(status).send('Access denied') | ||
class PasswordMisMatchException extends GE.LogicalException { | ||
static invoke (message) { | ||
return new this(message, 401, 'E_PASSWORD_MISMATCH') | ||
static invoke (message, passwordField, authScheme) { | ||
if (!passwordField || !authScheme) { | ||
throw new Error('Cannot invoke exception without passwordField or authScheme') | ||
} | ||
const error = new this(message, 401, 'E_PASSWORD_MISMATCH') | ||
error.passwordField = passwordField | ||
error.authScheme = authScheme | ||
return error | ||
} | ||
@@ -114,5 +129,5 @@ | ||
*/ | ||
async handle ({ status }, { request, response, session, auth }) { | ||
async handle ({ status, passwordField, authScheme }, { request, response, session }) { | ||
const isJSON = request.accepts(['html', 'json']) === 'json' | ||
const errorMessages = [{ field: auth.passwordField, message: 'Invalid user password' }] | ||
const errorMessages = [{ field: passwordField, message: 'Invalid user password' }] | ||
@@ -131,4 +146,4 @@ /** | ||
*/ | ||
if (auth.scheme === 'session') { | ||
session.withErrors(errorMessages).flashExcept([auth.passwordField]) | ||
if (authScheme === 'session') { | ||
session.withErrors(errorMessages).flashExcept([passwordField]) | ||
await session.commit() | ||
@@ -143,3 +158,3 @@ response.redirect('back') | ||
*/ | ||
if (auth.scheme === 'basic') { | ||
if (authScheme === 'basic') { | ||
response.header('WWW-Authenticate', 'Basic realm="example"') | ||
@@ -146,0 +161,0 @@ response.status(status).send('Access denied') |
@@ -34,5 +34,4 @@ 'use strict' | ||
let lastError = null | ||
let authenticatedScheme = null | ||
schemes = _.castArray(Array.isArray(schemes) && schemes.length ? schemes : this.scheme) | ||
schemes = _.castArray(schemes || this.scheme) | ||
debug('attempting to authenticate via %j scheme(s)', schemes) | ||
@@ -46,5 +45,13 @@ | ||
try { | ||
await auth.authenticator(scheme).check() | ||
const authenticator = auth.authenticator(scheme) | ||
await authenticator.check() | ||
debug('authenticated using %s scheme', scheme) | ||
authenticatedScheme = scheme | ||
/** | ||
* Swapping the main authentication instance with the one using which user | ||
* logged in. | ||
*/ | ||
auth.authenticatorInstance = authenticator | ||
lastError = null | ||
@@ -67,16 +74,5 @@ break | ||
/** | ||
* If user got logged then set the `current` property | ||
* on auth, which is reference to the scheme via | ||
* which user got authenticated. | ||
* For compatibility with the old API | ||
*/ | ||
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) | ||
} | ||
auth.current = auth.authenticatorInstance | ||
@@ -83,0 +79,0 @@ /** |
@@ -53,3 +53,3 @@ 'use strict' | ||
if (!user) { | ||
throw CE.UserNotFoundException.invoke(`Cannot find user with ${this._config.uid} as ${uid}`) | ||
throw this.missingUserFor(uid) | ||
} | ||
@@ -59,3 +59,3 @@ | ||
if (!validated) { | ||
throw CE.PasswordMisMatchException.invoke('Cannot verify user password') | ||
throw this.invalidPassword() | ||
} | ||
@@ -62,0 +62,0 @@ |
@@ -12,2 +12,4 @@ 'use strict' | ||
const CE = require('../Exceptions') | ||
/** | ||
@@ -182,4 +184,34 @@ * The base scheme is supposed to be extend by other | ||
} | ||
/** | ||
* Raises UserNotFoundException exception and pass required data to it | ||
* | ||
* @method missingUserFor | ||
* | ||
* @param {String|Number} uidValue | ||
* @param {String} [uid=this._config.uid] | ||
* @param {String} [password=this._config.password] | ||
* | ||
* @return {UserNotFoundException} | ||
*/ | ||
missingUserFor (uidValue, uid = this._config.uid, password = this._config.password) { | ||
const message = `Cannot find user with ${uid} as ${uidValue}` | ||
return CE.UserNotFoundException.invoke(message, uid, password, this.scheme) | ||
} | ||
/** | ||
* Raises PasswordMisMatchException exception and pass required data to it | ||
* | ||
* @method invalidPassword | ||
* | ||
* @param {String} message | ||
* @param {String} [password=this._config.password] | ||
* | ||
* @return {PasswordMisMatchException} | ||
*/ | ||
invalidPassword (password = this._config.password) { | ||
return CE.PasswordMisMatchException.invoke('Cannot verify user password', password, this.scheme) | ||
} | ||
} | ||
module.exports = BaseScheme |
@@ -34,3 +34,3 @@ 'use strict' | ||
if (!user) { | ||
throw CE.UserNotFoundException.invoke(`Cannot find user with ${this._config.uid} as ${uid}`) | ||
throw this.missingUserFor(uid) | ||
} | ||
@@ -40,3 +40,3 @@ | ||
if (!validated) { | ||
throw CE.PasswordMisMatchException.invoke('Cannot verify user password') | ||
throw this.invalidPassword() | ||
} | ||
@@ -43,0 +43,0 @@ |
@@ -180,3 +180,3 @@ 'use strict' | ||
if (!user) { | ||
throw CE.UserNotFoundException.invoke(`Cannot find user with ${this._config.uid} as ${uid}`) | ||
throw this.missingUserFor(uid) | ||
} | ||
@@ -186,3 +186,3 @@ | ||
if (!validated) { | ||
throw CE.PasswordMisMatchException.invoke('Cannot verify user password') | ||
throw this.invalidPassword() | ||
} | ||
@@ -213,2 +213,13 @@ | ||
/** | ||
* @method login | ||
* | ||
* @throws {RuntimeException} If jwt secret is not defined or user doesn't have a primary key value | ||
*/ | ||
login () { | ||
throw GE | ||
.RuntimeException | ||
.invoke('method not implemented, use generate() to retrieve jwt token', 500, 'E_CANNOT_LOGIN') | ||
} | ||
/** | ||
* Generates a jwt token for a user | ||
@@ -215,0 +226,0 @@ * |
@@ -124,3 +124,3 @@ 'use strict' | ||
if (!user) { | ||
throw CE.UserNotFoundException.invoke(`Cannot find user with ${this._config.uid} as ${uid}`) | ||
throw this.missingUserFor(uid) | ||
} | ||
@@ -130,3 +130,3 @@ | ||
if (!validated) { | ||
throw CE.PasswordMisMatchException.invoke('Cannot verify user password') | ||
throw this.invalidPassword() | ||
} | ||
@@ -207,3 +207,3 @@ | ||
if (!user) { | ||
throw CE.UserNotFoundException.invoke(`Cannot find user with ${this.primaryKey} as ${id}`) | ||
throw this.missingUserFor(id, this.primaryKey) | ||
} | ||
@@ -210,0 +210,0 @@ |
@@ -10,3 +10,3 @@ 'use strict' | ||
table.integer('user_id').unsigned().references('id').inTable('users') | ||
table.string('token', 40).notNullable().unique() | ||
table.string('token', 255).notNullable().unique() | ||
table.string('type', 80).notNullable() | ||
@@ -13,0 +13,0 @@ table.boolean('is_revoked').defaultTo(false) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
95218
2825