forked from DuendeArchive/identity-model-oidc-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenRevocationClient.js
More file actions
77 lines (61 loc) · 2.66 KB
/
TokenRevocationClient.js
File metadata and controls
77 lines (61 loc) · 2.66 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
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
import Log from './Log';
import MetadataService from './MetadataService';
import Global from './Global';
const AccessTokenTypeHint = "access_token";
export default class TokenRevocationClient {
constructor(settings, XMLHttpRequestCtor = Global.XMLHttpRequest, MetadataServiceCtor = MetadataService) {
if (!settings) {
Log.error("No settings provided");
throw new Error("No settings provided.");
}
this._settings = settings;
this._XMLHttpRequestCtor = XMLHttpRequestCtor;
this._metadataService = new MetadataServiceCtor(this._settings);
}
revoke(accessToken, required) {
Log.debug("TokenRevocationClient.revoke");
if (!accessToken) {
Log.error("No accessToken provided");
throw new Error("No accessToken provided.");
}
return this._metadataService.getRevocationEndpoint().then(url => {
if (!url) {
if (required) {
Log.error("Revocation not supported");
throw new Error("Revocation not supported");
}
// not required, so don't error and just return
return;
}
var client_id = this._settings.client_id;
var client_secret = this._settings.client_secret;
return this._revoke(url, client_id, client_secret, accessToken);
});
}
_revoke(url, client_id, client_secret, accessToken) {
Log.debug("Calling revocation endpoint");
return new Promise((resolve, reject) => {
var xhr = new this._XMLHttpRequestCtor();
xhr.open("POST", url);
xhr.onload = () => {
Log.debug("HTTP response received, status", xhr.status);
if (xhr.status === 200) {
resolve();
}
else {
reject(Error(xhr.statusText + " (" + xhr.status + ")"));
}
};
var body = "client_id=" + encodeURIComponent(client_id);
if (client_secret) {
body += "&client_secret=" + encodeURIComponent(client_secret);
}
body += "&token_type_hint=" + encodeURIComponent(AccessTokenTypeHint);
body += "&token=" + encodeURIComponent(accessToken);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(body);
});
}
}