-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
282 lines (240 loc) · 9.39 KB
/
index.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { redirect } from '@remix-run/node';
import * as jose from 'jose';
import { ensureDomain } from './lib/ensureDomainFormat.js';
import { getCredentials, saveUserToSession } from './lib/session.js';
import { transformUserData } from './lib/transformUserData.js';
import type {
Auth0Credentials,
Auth0CredentialsCallback,
Auth0RemixOptions,
Auth0UserProfile,
ClientCredentials,
HandleCallbackOptions,
SessionStore,
UserCredentials,
UserProfile,
TokenError,
AuthorizeOptions
} from './Auth0RemixTypes.js';
import type { AppLoadContext } from '@remix-run/node';
export enum Token {
ID = 'id',
ACCESS = 'access'
}
interface Auth0Urls {
authorizationURL: string;
openIDConfigurationURL: string;
jwksURL: string;
userProfileUrl: string;
tokenURL: string;
}
const noop = () => { /* empty */ };
export class Auth0RemixServer {
private readonly domain: string;
private readonly refreshTokenRotationEnabled: boolean;
private readonly callbackURL: string;
private readonly failedLoginRedirect: string;
private readonly jwks: ReturnType<typeof jose.createRemoteJWKSet>;
private readonly clientCredentials: ClientCredentials;
private readonly session: SessionStore;
private readonly auth0Urls: Auth0Urls;
private readonly credentialsCallback: Auth0CredentialsCallback;
constructor(auth0RemixOptions: Auth0RemixOptions) {
this.domain = ensureDomain(auth0RemixOptions.clientDetails.domain);
/**
* Refresh token rotation allows us to store the refresh tokens in the user's session.
* It is off by default because it requires an explicit setup in Auth0.
*
* @see https://auth0.com/docs/tokens/refresh-tokens/refresh-token-rotation
* @see https://auth0.com/blog/refresh-tokens-what-are-they-and-when-to-use-them/#Refresh-Token-Rotation
*/
this.refreshTokenRotationEnabled = auth0RemixOptions.refreshTokenRotationEnabled || false;
this.failedLoginRedirect = auth0RemixOptions.failedLoginRedirect;
this.callbackURL = auth0RemixOptions.callbackURL;
this.clientCredentials = {
clientID: auth0RemixOptions.clientDetails.clientID,
clientSecret: auth0RemixOptions.clientDetails.clientSecret,
audience: auth0RemixOptions.clientDetails.audience || `${this.domain}/api/v2/`,
organization: auth0RemixOptions.clientDetails.organization
};
this.session = {
store: auth0RemixOptions.session.store,
key: auth0RemixOptions.session.key || 'user'
};
this.auth0Urls = {
tokenURL: `${this.domain}/oauth/token`,
userProfileUrl: `${this.domain}/userinfo`,
authorizationURL: `${this.domain}/authorize`,
jwksURL: `${this.domain}/.well-known/jwks.json`,
openIDConfigurationURL: `${this.domain}/.well-known/openid-configuration`
};
// eslint-disable-next-line @typescript-eslint/no-empty-function
this.credentialsCallback = auth0RemixOptions.credentialsCallback || noop;
this.jwks = jose.createRemoteJWKSet(new URL(this.auth0Urls.jwksURL));
}
public async decodeToken(token: string, type: Token) {
const { payload } = await jose.jwtVerify(token, this.jwks, {
issuer: this.domain + '/',
audience: type === Token.ACCESS ? this.clientCredentials.audience : this.clientCredentials.clientID
});
return payload;
}
public async verifyToken(token: string, type: Token) {
await this.decodeToken(token, type);
}
public async isValid(token: string, type: Token) {
try {
await this.verifyToken(token, type);
return true;
} catch (_) {
return false;
}
}
public authorize(opts: AuthorizeOptions = {}) {
const scope = [
'offline_access', // required for refresh token
'openid', // required for id_token and the /userinfo api endpoint
'profile',
'email'
];
const authorizationURL = new URL(this.auth0Urls.authorizationURL);
authorizationURL.searchParams.set('response_type', 'code');
authorizationURL.searchParams.set('response_mode', 'form_post');
authorizationURL.searchParams.set('client_id', this.clientCredentials.clientID);
authorizationURL.searchParams.set('redirect_uri', this.callbackURL);
authorizationURL.searchParams.set('scope', scope.join(' '));
authorizationURL.searchParams.set('audience', this.clientCredentials.audience);
if (this.clientCredentials.organization) {
authorizationURL.searchParams.set('organization', this.clientCredentials.organization);
}
if (opts.forceLogin) {
authorizationURL.searchParams.set('prompt', 'login');
}
if (opts.forceSignup) {
authorizationURL.searchParams.set('screen_hint', 'signup');
}
throw redirect(authorizationURL.toString());
}
public async handleCallback(request: Request, options: HandleCallbackOptions): Promise<UserCredentials> {
const formData = await request.formData();
const code = formData.get('code');
if (!code) {
console.error('No code found in callback');
throw redirect(this.failedLoginRedirect);
}
const body = new URLSearchParams();
body.set('grant_type', 'authorization_code');
body.set('client_id', this.clientCredentials.clientID);
body.set('client_secret', this.clientCredentials.clientSecret);
body.set('code', code.toString());
body.set('redirect_uri', this.callbackURL);
const response = await fetch(this.auth0Urls.tokenURL, {
headers: { 'content-type': 'application/x-www-form-urlencoded' },
method: 'POST',
body: body.toString()
});
if (!response.ok) {
console.error('Failed to get token from Auth0');
throw redirect(this.failedLoginRedirect);
}
const data = (await response.json()) as Auth0Credentials;
const userData: UserCredentials = {
accessToken: data.access_token,
expiresIn: data.expires_in,
lastRefreshed: Date.now(),
expiresAt: Date.now() + data.expires_in * 1000
};
if (this.refreshTokenRotationEnabled) {
userData.refreshToken = data.refresh_token;
}
this.credentialsCallback({ ...userData, refreshToken: data.refresh_token });
if (options.onSuccessRedirect) {
const headers = await saveUserToSession(request, userData, this.session);
throw redirect(options.onSuccessRedirect, {
headers: headers
});
}
return userData;
}
public logout(redirectTo: string, headers?: HeadersInit) {
const logoutURL = new URL(`${this.domain}/v2/logout`);
logoutURL.searchParams.set('client_id', this.clientCredentials.clientID);
logoutURL.searchParams.set('returnTo', redirectTo);
throw redirect(logoutURL.toString(), {
headers: headers || {}
});
}
public async getUser(request: Request, context: AppLoadContext): Promise<UserProfile> {
let credentials: UserCredentials;
try {
credentials = await getCredentials(request, this.session);
} catch (err) {
console.error('No credentials found');
throw redirect(this.failedLoginRedirect);
}
try {
await this.decodeToken(credentials.accessToken, Token.ACCESS);
return await this.getUserProfile(credentials);
} catch (error) {
if ((error as TokenError).code === 'ERR_JWT_EXPIRED') {
if (!context.refresh) {
context.refresh = this.refreshCredentials(credentials);
const result = (await context.refresh) as UserCredentials;
const headers = await saveUserToSession(request, result, this.session);
throw redirect(request.url, {
headers: headers
});
}
await context.refresh;
return await this.getUser(request, context);
}
console.error('Failed to verify JWT', error);
throw redirect(this.failedLoginRedirect);
}
}
private async refreshCredentials(credentials: UserCredentials): Promise<UserCredentials> {
if (!credentials.refreshToken) {
console.error('No refresh token found within the credentials.');
throw redirect(this.failedLoginRedirect);
}
const body = new URLSearchParams();
body.set('grant_type', 'refresh_token');
body.set('client_id', this.clientCredentials.clientID);
body.set('client_secret', this.clientCredentials.clientSecret);
body.set('refresh_token', credentials.refreshToken);
const response = await fetch(this.auth0Urls.tokenURL, {
headers: { 'content-type': 'application/x-www-form-urlencoded' },
method: 'POST',
body: body.toString()
});
if (!response.ok) {
console.error('Failed to refresh token from Auth0');
throw redirect(this.failedLoginRedirect);
}
const data = (await response.json()) as Auth0Credentials;
const userData: UserCredentials = {
accessToken: data.access_token,
expiresIn: data.expires_in,
lastRefreshed: Date.now(),
expiresAt: Date.now() + data.expires_in * 1000
};
if (this.refreshTokenRotationEnabled) {
userData.refreshToken = data.refresh_token;
}
this.credentialsCallback({ ...userData, refreshToken: data.refresh_token });
return userData;
}
private async getUserProfile(credentials: UserCredentials): Promise<UserProfile> {
const response = await fetch(this.auth0Urls.userProfileUrl, {
headers: {
Authorization: `Bearer ${credentials.accessToken}`
}
});
if (!response.ok) {
console.error('Failed to get user profile from Auth0');
throw redirect(this.failedLoginRedirect);
}
const data = (await response.json()) as Auth0UserProfile;
return transformUserData(data);
}
}