Skip to content

Commit

Permalink
refactor: convert remaining grant-types, handlers and token types to …
Browse files Browse the repository at this point in the history
…es6 classes

Merge pull request #227 from jorenvandeweyer/feature/refactor-to-es6 thanks to @jorenvandeweyer
  • Loading branch information
jankapunkt authored Aug 26, 2023
2 parents 45eef09 + 2b559ab commit aaf28b4
Show file tree
Hide file tree
Showing 7 changed files with 785 additions and 780 deletions.
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

0 comments on commit aaf28b4

Please sign in to comment.