Skip to content

Commit 6758bff

Browse files
committed
Merge branch 'development' into release-5.0.0
2 parents c1fb9d4 + 48baa8b commit 6758bff

23 files changed

+1059
-979
lines changed

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ declare namespace OAuth2Server {
269269

270270
/**
271271
* Invoked during request authentication to check if the provided access token was authorized the requested scopes.
272+
* Optional, if a custom authenticateHandler is used or if there is no scope part of the request.
272273
*
273274
*/
274275
verifyScope(token: Token, scope: string | string[]): Promise<boolean>;

lib/grant-types/abstract-grant-type.js

+62-67
Original file line numberDiff line numberDiff line change
@@ -9,99 +9,94 @@ const InvalidScopeError = require('../errors/invalid-scope-error');
99
const isFormat = require('@node-oauth/formats');
1010
const 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.

lib/grant-types/authorization-code-grant-type.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class AuthorizationCodeGrantType extends AbstractGrantType {
5353
}
5454

5555
const code = await this.getAuthorizationCode(request, client);
56-
await this.validateRedirectUri(request, code);
5756
await this.revokeAuthorizationCode(code);
57+
await this.validateRedirectUri(request, code);
5858

5959
return this.saveToken(code.user, client, code.authorizationCode, code.scope);
6060
}
@@ -187,10 +187,10 @@ class AuthorizationCodeGrantType extends AbstractGrantType {
187187
* Save token.
188188
*/
189189

190-
async saveToken(user, client, authorizationCode, scope) {
191-
const validatedScope = await this.validateScope(user, client, scope);
192-
const accessToken = await this.generateAccessToken(client, user, scope);
193-
const refreshToken = await this.generateRefreshToken(client, user, scope);
190+
async saveToken(user, client, authorizationCode, requestedScope) {
191+
const validatedScope = await this.validateScope(user, client, requestedScope);
192+
const accessToken = await this.generateAccessToken(client, user, validatedScope);
193+
const refreshToken = await this.generateRefreshToken(client, user, validatedScope);
194194
const accessTokenExpiresAt = await this.getAccessTokenExpiresAt();
195195
const refreshTokenExpiresAt = await this.getRefreshTokenExpiresAt();
196196

lib/grant-types/client-credentials-grant-type.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ class ClientCredentialsGrantType extends AbstractGrantType {
6868
* Save token.
6969
*/
7070

71-
async saveToken(user, client, scope) {
72-
const validatedScope = await this.validateScope(user, client, scope);
73-
const accessToken = await this.generateAccessToken(client, user, scope);
74-
const accessTokenExpiresAt = await this.getAccessTokenExpiresAt(client, user, scope);
71+
async saveToken(user, client, requestedScope) {
72+
const validatedScope = await this.validateScope(user, client, requestedScope);
73+
const accessToken = await this.generateAccessToken(client, user, validatedScope);
74+
const accessTokenExpiresAt = await this.getAccessTokenExpiresAt(client, user, validatedScope);
7575
const token = {
7676
accessToken,
7777
accessTokenExpiresAt,

lib/grant-types/password-grant-type.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ class PasswordGrantType extends AbstractGrantType {
8686
* Save token.
8787
*/
8888

89-
async saveToken(user, client, scope) {
90-
const validatedScope = await this.validateScope(user, client, scope);
91-
const accessToken = await this.generateAccessToken(client, user, scope);
92-
const refreshToken = await this.generateRefreshToken(client, user, scope);
89+
async saveToken(user, client, requestedScope) {
90+
const validatedScope = await this.validateScope(user, client, requestedScope);
91+
const accessToken = await this.generateAccessToken(client, user, validatedScope);
92+
const refreshToken = await this.generateRefreshToken(client, user, validatedScope);
9393
const accessTokenExpiresAt = await this.getAccessTokenExpiresAt();
9494
const refreshTokenExpiresAt = await this.getRefreshTokenExpiresAt();
9595

lib/grant-types/refresh-token-grant-type.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class RefreshTokenGrantType extends AbstractGrantType {
132132
const token = {
133133
accessToken,
134134
accessTokenExpiresAt,
135-
scope: scope,
135+
scope,
136136
};
137137

138138
if (this.alwaysIssueNewRefreshToken !== false) {

0 commit comments

Comments
 (0)