forked from node-oauth/node-oauth2-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefresh-token-grant-type.js
More file actions
183 lines (139 loc) · 4.8 KB
/
refresh-token-grant-type.js
File metadata and controls
183 lines (139 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
/**
* Module dependencies.
*/
const AbstractGrantType = require('./abstract-grant-type');
const InvalidArgumentError = require('../errors/invalid-argument-error');
const InvalidGrantError = require('../errors/invalid-grant-error');
const InvalidRequestError = require('../errors/invalid-request-error');
const ServerError = require('../errors/server-error');
const isFormat = require('@node-oauth/formats');
const InvalidScopeError = require('../errors/invalid-scope-error');
/**
* Constructor.
*/
class RefreshTokenGrantType extends AbstractGrantType {
constructor(options = {}) {
if (!options.model) {
throw new InvalidArgumentError('Missing parameter: `model`');
}
if (!options.model.getRefreshToken) {
throw new InvalidArgumentError('Invalid argument: model does not implement `getRefreshToken()`');
}
if (!options.model.revokeToken) {
throw new InvalidArgumentError('Invalid argument: model does not implement `revokeToken()`');
}
if (!options.model.saveToken) {
throw new InvalidArgumentError('Invalid argument: model does not implement `saveToken()`');
}
super(options);
}
/**
* Handle refresh token grant.
*
* @see https://tools.ietf.org/html/rfc6749#section-6
*/
async handle(request, client) {
if (!request) {
throw new InvalidArgumentError('Missing parameter: `request`');
}
if (!client) {
throw new InvalidArgumentError('Missing parameter: `client`');
}
let token;
token = await this.getRefreshToken(request, client);
// Validate scope before revoking token to prevent destroying tokens on scope validation errors
const scope = this.getScope(request, token);
token = await this.revokeToken(token);
return this.saveToken(token.user, client, scope);
}
/**
* Get refresh token.
*/
async getRefreshToken(request, client) {
if (!request.body.refresh_token) {
throw new InvalidRequestError('Missing parameter: `refresh_token`');
}
if (!isFormat.vschar(request.body.refresh_token)) {
throw new InvalidRequestError('Invalid parameter: `refresh_token`');
}
const token = await this.model.getRefreshToken(request.body.refresh_token);
if (!token) {
throw new InvalidGrantError('Invalid grant: refresh token is invalid');
}
if (!token.client) {
throw new ServerError('Server error: `getRefreshToken()` did not return a `client` object');
}
if (!token.user) {
throw new ServerError('Server error: `getRefreshToken()` did not return a `user` object');
}
if (token.client.id !== client.id) {
throw new InvalidGrantError('Invalid grant: refresh token was issued to another client');
}
if (token.refreshTokenExpiresAt && !(token.refreshTokenExpiresAt instanceof Date)) {
throw new ServerError('Server error: `refreshTokenExpiresAt` must be a Date instance');
}
if (token.refreshTokenExpiresAt && token.refreshTokenExpiresAt < new Date()) {
throw new InvalidGrantError('Invalid grant: refresh token has expired');
}
return token;
}
/**
* Revoke the refresh token.
*
* @see https://tools.ietf.org/html/rfc6749#section-6
*/
async revokeToken(token) {
if (this.alwaysIssueNewRefreshToken === false) {
return token;
}
const status = await this.model.revokeToken(token);
if (!status) {
throw new InvalidGrantError('Invalid grant: refresh token is invalid or could not be revoked');
}
return token;
}
/**
* Save token.
*/
async saveToken(user, client, scope) {
const accessToken = await this.generateAccessToken(client, user, scope);
const refreshToken = await this.generateRefreshToken(client, user, scope);
const accessTokenExpiresAt = await this.getAccessTokenExpiresAt();
const refreshTokenExpiresAt = await this.getRefreshTokenExpiresAt();
const token = {
accessToken,
accessTokenExpiresAt,
scope,
};
if (this.alwaysIssueNewRefreshToken !== false) {
token.refreshToken = refreshToken;
token.refreshTokenExpiresAt = refreshTokenExpiresAt;
}
return this.model.saveToken(token, client, user);
}
getScope (request, token) {
const requestedScope = super.getScope(request);
const originalScope = token.scope;
if (!originalScope && !requestedScope) {
return;
}
if (!originalScope && requestedScope) {
throw new InvalidScopeError('Invalid scope: Unable to add extra scopes');
}
if (!requestedScope) {
return originalScope;
}
const valid = requestedScope.every(scope => {
return originalScope.includes(scope);
});
if (!valid) {
throw new InvalidScopeError('Invalid scope: Unable to add extra scopes');
}
return requestedScope;
}
}
/**
* Export constructor.
*/
module.exports = RefreshTokenGrantType;