@@ -9,97 +9,92 @@ 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
+ const accessToken = await this . model . generateAccessToken ( client , user , scope ) ;
36
+ return accessToken || tokenUtil . generateRandomToken ( ) ;
37
+ }
36
38
37
- AbstractGrantType . prototype . generateAccessToken = async function ( client , user , scope ) {
38
- if ( this . model . generateAccessToken ) {
39
- const accessToken = await this . model . generateAccessToken ( client , user , scope ) ;
40
- return accessToken || tokenUtil . generateRandomToken ( ) ;
39
+ return tokenUtil . generateRandomToken ( ) ;
41
40
}
42
41
43
- return tokenUtil . generateRandomToken ( ) ;
44
- } ;
45
-
46
- /**
42
+ /**
47
43
* Generate refresh token.
48
44
*/
45
+ async generateRefreshToken ( client , user , scope ) {
46
+ if ( this . model . generateRefreshToken ) {
47
+ const refreshToken = await this . model . generateRefreshToken ( client , user , scope ) ;
48
+ return refreshToken || tokenUtil . generateRandomToken ( ) ;
49
+ }
49
50
50
- AbstractGrantType . prototype . generateRefreshToken = async function ( client , user , scope ) {
51
- if ( this . model . generateRefreshToken ) {
52
- const refreshToken = await this . model . generateRefreshToken ( client , user , scope ) ;
53
- return refreshToken || tokenUtil . generateRandomToken ( ) ;
51
+ return tokenUtil . generateRandomToken ( ) ;
54
52
}
55
53
56
- return tokenUtil . generateRandomToken ( ) ;
57
- } ;
58
-
59
- /**
54
+ /**
60
55
* Get access token expiration date.
61
56
*/
57
+ getAccessTokenExpiresAt ( ) {
58
+ return new Date ( Date . now ( ) + this . accessTokenLifetime * 1000 ) ;
59
+ }
62
60
63
- AbstractGrantType . prototype . getAccessTokenExpiresAt = function ( ) {
64
- return new Date ( Date . now ( ) + this . accessTokenLifetime * 1000 ) ;
65
- } ;
66
61
67
- /**
68
- * Get refresh token expiration date.
69
- */
70
62
71
- AbstractGrantType . prototype . getRefreshTokenExpiresAt = function ( ) {
72
- return new Date ( Date . now ( ) + this . refreshTokenLifetime * 1000 ) ;
73
- } ;
63
+ /**
64
+ * Get refresh token expiration date.
65
+ */
66
+ getRefreshTokenExpiresAt ( ) {
67
+ return new Date ( Date . now ( ) + this . refreshTokenLifetime * 1000 ) ;
68
+ }
74
69
75
- /**
76
- * Get scope from the request body.
77
- */
70
+ /**
71
+ * Get scope from the request body.
72
+ */
73
+ getScope ( request ) {
74
+ if ( ! isFormat . nqschar ( request . body . scope ) ) {
75
+ throw new InvalidArgumentError ( 'Invalid parameter: `scope`' ) ;
76
+ }
78
77
79
- AbstractGrantType . prototype . getScope = function ( request ) {
80
- if ( ! isFormat . nqschar ( request . body . scope ) ) {
81
- throw new InvalidArgumentError ( 'Invalid parameter: `scope`' ) ;
78
+ return request . body . scope ;
82
79
}
83
80
84
- return request . body . scope ;
85
- } ;
81
+ /**
82
+ * Validate requested scope.
83
+ */
84
+ async validateScope ( user , client , scope ) {
85
+ if ( this . model . validateScope ) {
86
+ const validatedScope = await this . model . validateScope ( user , client , scope ) ;
86
87
87
- /**
88
- * Validate requested scope.
89
- */
90
- AbstractGrantType . prototype . validateScope = async function ( user , client , scope ) {
91
- if ( this . model . validateScope ) {
92
- const validatedScope = await this . model . validateScope ( user , client , scope ) ;
88
+ if ( ! validatedScope ) {
89
+ throw new InvalidScopeError ( 'Invalid scope: Requested scope is invalid' ) ;
90
+ }
93
91
94
- if ( ! validatedScope ) {
95
- throw new InvalidScopeError ( 'Invalid scope: Requested scope is invalid' ) ;
92
+ return validatedScope ;
93
+ } else {
94
+ return scope ;
96
95
}
97
-
98
- return validatedScope ;
99
- } else {
100
- return scope ;
101
96
}
102
- } ;
97
+ }
103
98
104
99
/**
105
100
* Export constructor.
0 commit comments