This repository was archived by the owner on Jan 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathlogout.js
More file actions
80 lines (69 loc) · 3.23 KB
/
Copy pathlogout.js
File metadata and controls
80 lines (69 loc) · 3.23 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
/*!
* Copyright (c) 2019-Present, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/
const fetch = require('node-fetch');
const querystring = require('querystring');
const OIDCMiddlewareError = require('./OIDCMiddlewareError');
const logout = module.exports;
const makeErrorHandler = emitter => err => {
// Emit the errors outside the promise chain so they can be received by event listeners
setTimeout(function() {
emitter.emit('error', err);
}, 1);
};
const makeAuthorizationHeader = ({ client_id, client_secret }) =>
'Basic ' + Buffer.from(`${client_id}:${client_secret}`).toString('base64');
const makeTokenRevoker = ({ issuer, client_id, client_secret, errorHandler }) => {
const revokeEndpoint = `${issuer}/v1/revoke`;
return ({ token_hint, token }) => {
return fetch(revokeEndpoint, {
method: 'POST',
headers: {
'accepts': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
'authorization': makeAuthorizationHeader({ client_id, client_secret }),
},
body: querystring.stringify({token, token_type_hint: token_hint}),
})
// eslint-disable-next-line promise/no-nesting
// .then( r => r.ok ? r : r.text().then(message => Promise.reject(new OIDCMiddlewareError('revokeError', message)) ))
.then( r => r.ok ? r : r.text().then((message) => {
throw new OIDCMiddlewareError('revokeError', message);
}))
.catch( errorHandler ) // catch and emit - this promise chain can never fail
};
};
logout.forceLogoutAndRevoke = context => {
const emitter = context.emitter;
let { issuer, client_id, client_secret } = context.options;
const REVOKABLE_TOKENS = ['refresh_token', 'access_token'];
// Support ORG Authorization Server
if (issuer.indexOf('/oauth2') === -1) {
issuer = issuer + '/oauth2';
}
const revokeToken = makeTokenRevoker({ issuer, client_id, client_secret, errorHandler: makeErrorHandler(emitter) });
return async (req, res /*, next */) => {
const tokens = req.userContext.tokens;
const revokeIfExists = token_hint => tokens[token_hint] ? revokeToken({token_hint, token: tokens[token_hint]}) : null;
const revokes = REVOKABLE_TOKENS.map( revokeIfExists );
// clear local session
req.logout();
// attempt all revokes
await Promise.all(revokes); // these capture (emit) all rejections, no wrapping catch needed, no early fail of .all()
const params = {
id_token_hint: tokens.id_token,
post_logout_redirect_uri: context.options.logoutRedirectUri,
};
// redirect to Okta to clear SSO session
const endOktaSessionEndpoint = `${issuer}/v1/logout?${querystring.stringify(params)}`;
return res.redirect(endOktaSessionEndpoint);
};
};