Skip to content

Feature/refactor to es6 #227

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 60 additions & 65 deletions lib/grant-types/abstract-grant-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,97 +9,92 @@ const InvalidScopeError = require('../errors/invalid-scope-error');
const isFormat = require('@node-oauth/formats');
const tokenUtil = require('../utils/token-util');

/**
* Constructor.
*/
class AbstractGrantType {
constructor (options) {
options = options || {};

function AbstractGrantType(options) {
options = options || {};
if (!options.accessTokenLifetime) {
throw new InvalidArgumentError('Missing parameter: `accessTokenLifetime`');
}

if (!options.accessTokenLifetime) {
throw new InvalidArgumentError('Missing parameter: `accessTokenLifetime`');
}
if (!options.model) {
throw new InvalidArgumentError('Missing parameter: `model`');
}

if (!options.model) {
throw new InvalidArgumentError('Missing parameter: `model`');
this.accessTokenLifetime = options.accessTokenLifetime;
this.model = options.model;
this.refreshTokenLifetime = options.refreshTokenLifetime;
this.alwaysIssueNewRefreshToken = options.alwaysIssueNewRefreshToken;
}

this.accessTokenLifetime = options.accessTokenLifetime;
this.model = options.model;
this.refreshTokenLifetime = options.refreshTokenLifetime;
this.alwaysIssueNewRefreshToken = options.alwaysIssueNewRefreshToken;
}

/**
* Generate access token.
*/
/**
* Generate access token.
*/
async generateAccessToken (client, user, scope) {
if (this.model.generateAccessToken) {
const accessToken = await this.model.generateAccessToken(client, user, scope);
return accessToken || tokenUtil.generateRandomToken();
}

AbstractGrantType.prototype.generateAccessToken = async function(client, user, scope) {
if (this.model.generateAccessToken) {
const accessToken = await this.model.generateAccessToken(client, user, scope);
return accessToken || tokenUtil.generateRandomToken();
return tokenUtil.generateRandomToken();
}

return tokenUtil.generateRandomToken();
};

/**
/**
* Generate refresh token.
*/
async generateRefreshToken (client, user, scope) {
if (this.model.generateRefreshToken) {
const refreshToken = await this.model.generateRefreshToken(client, user, scope);
return refreshToken || tokenUtil.generateRandomToken();
}

AbstractGrantType.prototype.generateRefreshToken = async function(client, user, scope) {
if (this.model.generateRefreshToken) {
const refreshToken = await this.model.generateRefreshToken(client, user, scope);
return refreshToken || tokenUtil.generateRandomToken();
return tokenUtil.generateRandomToken();
}

return tokenUtil.generateRandomToken();
};

/**
/**
* Get access token expiration date.
*/
getAccessTokenExpiresAt() {
return new Date(Date.now() + this.accessTokenLifetime * 1000);
}

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 refresh token expiration date.
*/
getRefreshTokenExpiresAt () {
return new Date(Date.now() + this.refreshTokenLifetime * 1000);
}

/**
* Get scope from the request body.
*/
/**
* Get scope from the request body.
*/
getScope (request) {
if (!isFormat.nqschar(request.body.scope)) {
throw new InvalidArgumentError('Invalid parameter: `scope`');
}

AbstractGrantType.prototype.getScope = function(request) {
if (!isFormat.nqschar(request.body.scope)) {
throw new InvalidArgumentError('Invalid parameter: `scope`');
return request.body.scope;
}

return request.body.scope;
};
/**
* Validate requested scope.
*/
async validateScope (user, client, scope) {
if (this.model.validateScope) {
const validatedScope = await this.model.validateScope(user, client, scope);

/**
* Validate requested scope.
*/
AbstractGrantType.prototype.validateScope = async function(user, client, scope) {
if (this.model.validateScope) {
const validatedScope = await this.model.validateScope(user, client, scope);
if (!validatedScope) {
throw new InvalidScopeError('Invalid scope: Requested scope is invalid');
}

if (!validatedScope) {
throw new InvalidScopeError('Invalid scope: Requested scope is invalid');
return validatedScope;
} else {
return scope;
}

return validatedScope;
} else {
return scope;
}
};
}

/**
* Export constructor.
Expand Down
Loading