Skip to content

Removed Bluebird and Promisify-Any #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
2 changes: 1 addition & 1 deletion .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
recursive: true
reporter: "spec"
retries: 1
retries: 0
slow: 20
timeout: 2000
ui: "bdd"
Expand Down
106 changes: 64 additions & 42 deletions lib/grant-types/abstract-grant-type.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
'use strict';

/**
* Module dependencies.
*/

const InvalidArgumentError = require('../errors/invalid-argument-error');
const InvalidScopeError = require('../errors/invalid-scope-error');
const Promise = require('bluebird');
const promisify = require('promisify-any').use(Promise);
const is = require('../validator/is');
const tokenUtil = require('../utils/token-util');

/**
* Constructor.
*/

function AbstractGrantType(options) {
options = options || {};

Expand All @@ -35,55 +27,73 @@ function AbstractGrantType(options) {
/**
* Generate access token.
*/
AbstractGrantType.prototype.generateAccessToken = async function(client, user, scope) {

let accessToken;

if (
this.model &&
this.model.generateAccessToken &&
typeof this.model.generateAccessToken === 'function'
) {

try {
accessToken = await this.model.generateAccessToken
.call(this.model, client, user, scope);
} catch (err) {
return Promise.reject(err);
}

AbstractGrantType.prototype.generateAccessToken = function(client, user, scope) {
if (this.model.generateAccessToken) {
return promisify(this.model.generateAccessToken, 3).call(this.model, client, user, scope)
.then(function(accessToken) {
return accessToken || tokenUtil.generateRandomToken();
});
}

return tokenUtil.generateRandomToken();
return Promise.resolve( accessToken || tokenUtil.generateRandomToken() );
};

/**
* Generate refresh token.
*/
AbstractGrantType.prototype.generateRefreshToken = async function(client, user, scope) {

let refreshToken;

if (
this.model &&
this.model.generateRefreshToken &&
typeof this.model.generateRefreshToken === 'function'
) {

try {
refreshToken = await this.model.generateRefreshToken
.call(this.model, client, user, scope);
} catch (err) {
return Promise.reject(err);
}

AbstractGrantType.prototype.generateRefreshToken = function(client, user, scope) {
if (this.model.generateRefreshToken) {
return promisify(this.model.generateRefreshToken, 3).call(this.model, client, user, scope)
.then(function(refreshToken) {
return refreshToken || tokenUtil.generateRandomToken();
});
}

return tokenUtil.generateRandomToken();
return Promise.resolve( refreshToken || tokenUtil.generateRandomToken() );
};

/**
* Get access token expiration date.
*/

AbstractGrantType.prototype.getAccessTokenExpiresAt = function() {
return new Date(Date.now() + this.accessTokenLifetime * 1000);
};

/**
* Get refresh token expiration date.
*/

AbstractGrantType.prototype.getRefreshTokenExpiresAt = function() {
return new Date(Date.now() + this.refreshTokenLifetime * 1000);
};

/**
* Get scope from the request body.
*/

AbstractGrantType.prototype.getScope = function(request) {
if (!is.nqschar(request.body.scope)) {

if (!request || !request.body || !is.nqschar(request.body.scope)) {
throw new InvalidArgumentError('Invalid parameter: `scope`');
}

Expand All @@ -93,23 +103,35 @@ AbstractGrantType.prototype.getScope = function(request) {
/**
* Validate requested scope.
*/
AbstractGrantType.prototype.validateScope = function(user, client, scope) {
if (this.model.validateScope) {
return promisify(this.model.validateScope, 3).call(this.model, user, client, scope)
.then(function (scope) {
if (!scope) {
throw new InvalidScopeError('Invalid scope: Requested scope is invalid');
}

return scope;
});
} else {
return scope;
AbstractGrantType.prototype.validateScope = async function(user, client, scope) {

// scope is valid by default
let isValidScope = true;

if (
this.model &&
this.model.validateScope &&
typeof this.model.validateScope === 'function'
) {

try {
isValidScope = await this.model.validateScope
.call(this.model, user, client, scope);
} catch (err) {
return Promise.reject(err);
}

}
};

/**
* Export constructor.
*/
// This should never return an error, only true or false.
// if (!isValidScope) {
// Promise.reject(
// new InvalidScopeError('Invalid scope: Requested scope is invalid')
// );
// }

return Promise.resolve(isValidScope);

};

module.exports = AbstractGrantType;
Loading