generated from VilledeMontreal/repository-template
/
authenticator.ts
157 lines (149 loc) · 4.51 KB
/
authenticator.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
/*!
* Copyright (c) 2020 Ville de Montreal. All rights reserved.
* Licensed under the MIT license.
* See LICENSE file in the project root for full license information.
*/
import {
cleanupHttpError,
hookMethod,
TypedProperty,
} from '@villedemontreal/auth-core';
import {
IOidcSession,
IOidcAuthenticatorConfig,
TokenSet,
} from '@villedemontreal/auth-oidc';
import * as superagent from 'superagent';
const tokenProperty = new TypedProperty<TokenSet, superagent.SuperAgentRequest>(
Symbol('token'),
);
/**
*
* @param session the OIDC session used to provide the access tokens
* @param [config] the config for customizing this authenticator
*/
export function authenticator(
session: IOidcSession,
config?: Readonly<IOidcAuthenticatorConfig>,
) {
return (request: superagent.SuperAgentRequest): superagent.Request => {
if (canApplyAuthenticator(request)) {
// setup retries
if (config && config.retryUnauthenticatedRequests !== false) {
// eslint-disable-next-line no-underscore-dangle
const maxRetries = (request as any)._maxRetries;
// eslint-disable-next-line no-underscore-dangle
const retryCallback = (request as any)._retryCallback;
request.retry(maxRetries || 1, canRetryRequest(retryCallback));
}
// override end
hookMethod(request, 'end', async args => {
const [callback] = args.methodArgs;
await onEnd(request, callback);
});
// override callback
hookMethod(request, 'callback', async args => {
const [, /* previous arg is err */ res] = args.methodArgs;
await onCallback(request, res);
});
}
return request;
};
function canApplyAuthenticator(
request: superagent.SuperAgentRequest,
): boolean {
if (request.get('Authorization')) {
return false;
}
if (config && config.onAcceptRequest && !config.onAcceptRequest(request)) {
return false;
}
if (config && config.urlFilter && !config.urlFilter.test(request.url)) {
return false;
}
return true;
}
async function onEnd(request: superagent.SuperAgentRequest, callback: any) {
try {
await injectTokenInHeaders(request);
} catch (err) {
session.logger.error(
{
error: cleanupHttpError(err),
method: request.method,
url: request.url,
},
`Could not inject valid access token into request headers of ${request.method} ${request.url}`,
);
// if there is any error, we must invoke the callback
// to let the wrapped promise being rejected
callback(err);
throw err;
}
}
async function onCallback(request: superagent.SuperAgentRequest, res: any) {
const token = tokenProperty.get(request);
if (
res &&
res.status === 401 &&
token &&
token.toAuthorizationString() === request.get('Authorization')
) {
try {
// ensure that we don't use the token any more since the request returned a 401
await session.deleteToken(token);
} catch (e) {
session.logger.error(
{
error: cleanupHttpError(e),
method: request.method,
url: request.url,
},
'Could not delete bad token from session',
);
}
if (config && config.retryUnauthenticatedRequests !== false) {
try {
// generate a new token now that we have deleted the previous one
await injectTokenInHeaders(request);
} catch (e) {
session.logger.error(
{
error: cleanupHttpError(e),
method: request.method,
url: request.url,
},
'Could not obtain a new access token before retrying the request',
);
}
}
}
}
async function injectTokenInHeaders(
request: superagent.SuperAgentRequest,
): Promise<TokenSet> {
const token = await session.getToken();
request.set('Authorization', token.toAuthorizationString());
tokenProperty.set(request, token);
if (config && config.beforeSendRequest) {
await config.beforeSendRequest(request, token);
}
return token;
}
function canRetryRequest(
originalCallback: (
err: any,
res: superagent.Response,
) => boolean | undefined,
) {
return (err: any, res: superagent.Response): boolean | undefined => {
if (res && res.status === 401) {
return true;
}
if (originalCallback) {
return originalCallback(err, res);
}
return undefined;
};
}
}