@@ -9,97 +9,92 @@ 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+ const accessToken = await this . model . generateAccessToken ( client , user , scope ) ;
36+ return accessToken || tokenUtil . generateRandomToken ( ) ;
37+ }
3638
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 ( ) ;
4140 }
4241
43- return tokenUtil . generateRandomToken ( ) ;
44- } ;
45-
46- /**
42+ /**
4743 * Generate refresh token.
4844 */
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+ }
4950
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 ( ) ;
5452 }
5553
56- return tokenUtil . generateRandomToken ( ) ;
57- } ;
58-
59- /**
54+ /**
6055 * Get access token expiration date.
6156 */
57+ getAccessTokenExpiresAt ( ) {
58+ return new Date ( Date . now ( ) + this . accessTokenLifetime * 1000 ) ;
59+ }
6260
63- AbstractGrantType . prototype . getAccessTokenExpiresAt = function ( ) {
64- return new Date ( Date . now ( ) + this . accessTokenLifetime * 1000 ) ;
65- } ;
6661
67- /**
68- * Get refresh token expiration date.
69- */
7062
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+ }
7469
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+ }
7877
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 ;
8279 }
8380
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 ) ;
8687
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+ }
9391
94- if ( ! validatedScope ) {
95- throw new InvalidScopeError ( 'Invalid scope: Requested scope is invalid' ) ;
92+ return validatedScope ;
93+ } else {
94+ return scope ;
9695 }
97-
98- return validatedScope ;
99- } else {
100- return scope ;
10196 }
102- } ;
97+ }
10398
10499/**
105100 * Export constructor.
0 commit comments