@@ -9,99 +9,94 @@ const InvalidScopeError = require('../errors/invalid-scope-error');
99const isFormat = require ( '@node-oauth/formats' ) ;
1010const tokenUtil = require ( '../utils/token-util' ) ;
1111
12- /**
13- * Constructor.
14- */
12+ class AbstractGrantType {
13+ constructor ( options ) {
14+ options = options || { } ;
1515
16- function AbstractGrantType ( options ) {
17- options = options || { } ;
16+ if ( ! options . accessTokenLifetime ) {
17+ throw new InvalidArgumentError ( 'Missing parameter: `accessTokenLifetime`' ) ;
18+ }
1819
19- if ( ! options . accessTokenLifetime ) {
20- throw new InvalidArgumentError ( 'Missing parameter: `accessTokenLifetime `' ) ;
21- }
20+ if ( ! options . model ) {
21+ throw new InvalidArgumentError ( 'Missing parameter: `model `' ) ;
22+ }
2223
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 ;
2528 }
2629
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+ }
3639
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 ( ) ;
4241 }
4342
44- return tokenUtil . generateRandomToken ( ) ;
45- } ;
46-
47- /**
43+ /**
4844 * Generate refresh token.
4945 */
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+ }
5052
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 ( ) ;
5654 }
5755
58- return tokenUtil . generateRandomToken ( ) ;
59- } ;
60-
61- /**
56+ /**
6257 * Get access token expiration date.
6358 */
59+ getAccessTokenExpiresAt ( ) {
60+ return new Date ( Date . now ( ) + this . accessTokenLifetime * 1000 ) ;
61+ }
6462
65- AbstractGrantType . prototype . getAccessTokenExpiresAt = function ( ) {
66- return new Date ( Date . now ( ) + this . accessTokenLifetime * 1000 ) ;
67- } ;
6863
69- /**
70- * Get refresh token expiration date.
71- */
7264
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+ }
7671
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+ }
8079
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 ;
8481 }
8582
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 ) ;
8889
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+ }
9593
96- if ( ! validatedScope ) {
97- throw new InvalidScopeError ( 'Invalid scope: Requested scope is invalid' ) ;
94+ return validatedScope ;
95+ } else {
96+ return scope ;
9897 }
99-
100- return validatedScope ;
101- } else {
102- return scope ;
10398 }
104- } ;
99+ }
105100
106101/**
107102 * Export constructor.
0 commit comments