@@ -9,99 +9,94 @@ const InvalidScopeError = require('../errors/invalid-scope-error');
9
9
const isFormat = require ( '@node-oauth/formats' ) ;
10
10
const tokenUtil = require ( '../utils/token-util' ) ;
11
11
12
- /**
13
- * Constructor.
14
- */
12
+ class AbstractGrantType {
13
+ constructor ( options ) {
14
+ options = options || { } ;
15
15
16
- function AbstractGrantType ( options ) {
17
- options = options || { } ;
16
+ if ( ! options . accessTokenLifetime ) {
17
+ throw new InvalidArgumentError ( 'Missing parameter: `accessTokenLifetime`' ) ;
18
+ }
18
19
19
- if ( ! options . accessTokenLifetime ) {
20
- throw new InvalidArgumentError ( 'Missing parameter: `accessTokenLifetime `' ) ;
21
- }
20
+ if ( ! options . model ) {
21
+ throw new InvalidArgumentError ( 'Missing parameter: `model `' ) ;
22
+ }
22
23
23
- if ( ! options . model ) {
24
- throw new InvalidArgumentError ( 'Missing parameter: `model`' ) ;
24
+ this . accessTokenLifetime = options . accessTokenLifetime ;
25
+ this . model = options . model ;
26
+ this . refreshTokenLifetime = options . refreshTokenLifetime ;
27
+ this . alwaysIssueNewRefreshToken = options . alwaysIssueNewRefreshToken ;
25
28
}
26
29
27
- this . accessTokenLifetime = options . accessTokenLifetime ;
28
- this . model = options . model ;
29
- this . refreshTokenLifetime = options . refreshTokenLifetime ;
30
- this . alwaysIssueNewRefreshToken = options . alwaysIssueNewRefreshToken ;
31
- }
32
-
33
- /**
34
- * Generate access token.
35
- */
30
+ /**
31
+ * Generate access token.
32
+ */
33
+ async generateAccessToken ( client , user , scope ) {
34
+ if ( this . model . generateAccessToken ) {
35
+ // We should not fall back to a random accessToken, if the model did not
36
+ // return a token, in order to prevent unintended token-issuing.
37
+ return this . model . generateAccessToken ( client , user , scope ) ;
38
+ }
36
39
37
- AbstractGrantType . prototype . generateAccessToken = async function ( client , user , scope ) {
38
- if ( this . model . generateAccessToken ) {
39
- // We should not fall back to a random accessToken, if the model did not
40
- // return a token, in order to prevent unintended token-issuing.
41
- return this . model . generateAccessToken ( client , user , scope ) ;
40
+ return tokenUtil . generateRandomToken ( ) ;
42
41
}
43
42
44
- return tokenUtil . generateRandomToken ( ) ;
45
- } ;
46
-
47
- /**
43
+ /**
48
44
* Generate refresh token.
49
45
*/
46
+ async generateRefreshToken ( client , user , scope ) {
47
+ if ( this . model . generateRefreshToken ) {
48
+ // We should not fall back to a random refreshToken, if the model did not
49
+ // return a token, in order to prevent unintended token-issuing.
50
+ return this . model . generateRefreshToken ( client , user , scope ) ;
51
+ }
50
52
51
- AbstractGrantType . prototype . generateRefreshToken = async function ( client , user , scope ) {
52
- if ( this . model . generateRefreshToken ) {
53
- // We should not fall back to a random refreshToken, if the model did not
54
- // return a token, in order to prevent unintended token-issuing.
55
- return this . model . generateRefreshToken ( client , user , scope ) ;
53
+ return tokenUtil . generateRandomToken ( ) ;
56
54
}
57
55
58
- return tokenUtil . generateRandomToken ( ) ;
59
- } ;
60
-
61
- /**
56
+ /**
62
57
* Get access token expiration date.
63
58
*/
59
+ getAccessTokenExpiresAt ( ) {
60
+ return new Date ( Date . now ( ) + this . accessTokenLifetime * 1000 ) ;
61
+ }
64
62
65
- AbstractGrantType . prototype . getAccessTokenExpiresAt = function ( ) {
66
- return new Date ( Date . now ( ) + this . accessTokenLifetime * 1000 ) ;
67
- } ;
68
63
69
- /**
70
- * Get refresh token expiration date.
71
- */
72
64
73
- AbstractGrantType . prototype . getRefreshTokenExpiresAt = function ( ) {
74
- return new Date ( Date . now ( ) + this . refreshTokenLifetime * 1000 ) ;
75
- } ;
65
+ /**
66
+ * Get refresh token expiration date.
67
+ */
68
+ getRefreshTokenExpiresAt ( ) {
69
+ return new Date ( Date . now ( ) + this . refreshTokenLifetime * 1000 ) ;
70
+ }
76
71
77
- /**
78
- * Get scope from the request body.
79
- */
72
+ /**
73
+ * Get scope from the request body.
74
+ */
75
+ getScope ( request ) {
76
+ if ( ! isFormat . nqschar ( request . body . scope ) ) {
77
+ throw new InvalidArgumentError ( 'Invalid parameter: `scope`' ) ;
78
+ }
80
79
81
- AbstractGrantType . prototype . getScope = function ( request ) {
82
- if ( ! isFormat . nqschar ( request . body . scope ) ) {
83
- throw new InvalidArgumentError ( 'Invalid parameter: `scope`' ) ;
80
+ return request . body . scope ;
84
81
}
85
82
86
- return request . body . scope ;
87
- } ;
83
+ /**
84
+ * Validate requested scope.
85
+ */
86
+ async validateScope ( user , client , scope ) {
87
+ if ( this . model . validateScope ) {
88
+ const validatedScope = await this . model . validateScope ( user , client , scope ) ;
88
89
89
- /**
90
- * Validate requested scope.
91
- */
92
- AbstractGrantType . prototype . validateScope = async function ( user , client , scope ) {
93
- if ( this . model . validateScope ) {
94
- const validatedScope = await this . model . validateScope ( user , client , scope ) ;
90
+ if ( ! validatedScope ) {
91
+ throw new InvalidScopeError ( 'Invalid scope: Requested scope is invalid' ) ;
92
+ }
95
93
96
- if ( ! validatedScope ) {
97
- throw new InvalidScopeError ( 'Invalid scope: Requested scope is invalid' ) ;
94
+ return validatedScope ;
95
+ } else {
96
+ return scope ;
98
97
}
99
-
100
- return validatedScope ;
101
- } else {
102
- return scope ;
103
98
}
104
- } ;
99
+ }
105
100
106
101
/**
107
102
* Export constructor.
0 commit comments