-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathabstract-grant-type.js
More file actions
108 lines (83 loc) · 2.58 KB
/
Copy pathabstract-grant-type.js
File metadata and controls
108 lines (83 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
/**
* Module dependencies.
*/
const InvalidArgumentError = require('../errors/invalid-argument-error');
const InvalidScopeError = require('../errors/invalid-scope-error');
const isFormat = require('@node-oauth/formats');
const tokenUtil = require('../utils/token-util');
/**
* Constructor.
*/
function AbstractGrantType(options) {
options = options || {};
if (!options.accessTokenLifetime) {
throw new InvalidArgumentError('Missing parameter: `accessTokenLifetime`');
}
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;
}
/**
* Generate access token.
*/
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();
};
/**
* Generate refresh token.
*/
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();
};
/**
* 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 (!isFormat.nqschar(request.body.scope)) {
throw new InvalidArgumentError('Invalid parameter: `scope`');
}
return request.body.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');
}
return validatedScope;
} else {
return scope;
}
};
/**
* Export constructor.
*/
module.exports = AbstractGrantType;