This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 841
/
OidcClient.js
229 lines (184 loc) · 8.83 KB
/
OidcClient.js
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
// 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.js';
import { OidcClientSettings } from './OidcClientSettings.js';
import { ErrorResponse } from './ErrorResponse.js';
import { SigninRequest } from './SigninRequest.js';
import { SigninResponse } from './SigninResponse.js';
import { SignoutRequest } from './SignoutRequest.js';
import { SignoutResponse } from './SignoutResponse.js';
import { SigninState } from './SigninState.js';
import { State } from './State.js';
export class OidcClient {
constructor(settings = {}) {
if (settings instanceof OidcClientSettings) {
this._settings = settings;
}
else {
this._settings = new OidcClientSettings(settings);
}
}
get _stateStore() {
return this.settings.stateStore;
}
get _validator() {
return this.settings.validator;
}
get _metadataService() {
return this.settings.metadataService;
}
get settings() {
return this._settings;
}
get metadataService() {
return this._metadataService;
}
createSigninRequest({
response_type, scope, redirect_uri,
// data was meant to be the place a caller could indicate the data to
// have round tripped, but people were getting confused, so i added state (since that matches the spec)
// and so now if data is not passed, but state is then state will be used
data, state, prompt, display, max_age, ui_locales, id_token_hint, login_hint, acr_values,
resource, request, request_uri, response_mode, extraQueryParams, extraTokenParams, request_type, skipUserInfo } = {},
stateStore
) {
Log.debug("OidcClient.createSigninRequest");
let client_id = this._settings.client_id;
response_type = response_type || this._settings.response_type;
scope = scope || this._settings.scope;
redirect_uri = redirect_uri || this._settings.redirect_uri;
// id_token_hint, login_hint aren't allowed on _settings
prompt = prompt || this._settings.prompt;
display = display || this._settings.display;
max_age = max_age || this._settings.max_age;
ui_locales = ui_locales || this._settings.ui_locales;
acr_values = acr_values || this._settings.acr_values;
resource = resource || this._settings.resource;
response_mode = response_mode || this._settings.response_mode;
extraQueryParams = extraQueryParams || this._settings.extraQueryParams;
extraTokenParams = extraTokenParams || this._settings.extraTokenParams;
let authority = this._settings.authority;
if (SigninRequest.isCode(response_type) && response_type !== "code") {
return Promise.reject(new Error("OpenID Connect hybrid flow is not supported"));
}
return this._metadataService.getAuthorizationEndpoint().then(url => {
Log.debug("OidcClient.createSigninRequest: Received authorization endpoint", url);
let signinRequest = new SigninRequest({
url,
client_id,
redirect_uri,
response_type,
scope,
data: data || state,
authority,
prompt, display, max_age, ui_locales, id_token_hint, login_hint, acr_values,
resource, request, request_uri, extraQueryParams, extraTokenParams, request_type, response_mode,
client_secret: this._settings.client_secret,
skipUserInfo
});
var signinState = signinRequest.state;
stateStore = stateStore || this._stateStore;
return stateStore.set(signinState.id, signinState.toStorageString()).then(() => {
return signinRequest;
});
});
}
readSigninResponseState(url, stateStore, removeState = false) {
Log.debug("OidcClient.readSigninResponseState");
let useQuery = this._settings.response_mode === "query" ||
(!this._settings.response_mode && SigninRequest.isCode(this._settings.response_type));
let delimiter = useQuery ? "?" : "#";
var response = new SigninResponse(url, delimiter);
if (!response.state) {
Log.error("OidcClient.readSigninResponseState: No state in response");
return Promise.reject(new Error("No state in response"));
}
stateStore = stateStore || this._stateStore;
var stateApi = removeState ? stateStore.remove.bind(stateStore) : stateStore.get.bind(stateStore);
return stateApi(response.state).then(storedStateString => {
if (!storedStateString) {
Log.error("OidcClient.readSigninResponseState: No matching state found in storage");
throw new Error("No matching state found in storage");
}
let state = SigninState.fromStorageString(storedStateString);
return {state, response};
});
}
processSigninResponse(url, stateStore) {
Log.debug("OidcClient.processSigninResponse");
return this.readSigninResponseState(url, stateStore, true).then(({state, response}) => {
Log.debug("OidcClient.processSigninResponse: Received state from storage; validating response");
return this._validator.validateSigninResponse(state, response);
});
}
createSignoutRequest({id_token_hint, data, state, post_logout_redirect_uri, extraQueryParams, request_type } = {},
stateStore
) {
Log.debug("OidcClient.createSignoutRequest");
post_logout_redirect_uri = post_logout_redirect_uri || this._settings.post_logout_redirect_uri;
extraQueryParams = extraQueryParams || this._settings.extraQueryParams;
return this._metadataService.getEndSessionEndpoint().then(url => {
if (!url) {
Log.error("OidcClient.createSignoutRequest: No end session endpoint url returned");
throw new Error("no end session endpoint");
}
Log.debug("OidcClient.createSignoutRequest: Received end session endpoint", url);
let request = new SignoutRequest({
url,
id_token_hint,
post_logout_redirect_uri,
data: data || state,
extraQueryParams,
request_type
});
var signoutState = request.state;
if (signoutState) {
Log.debug("OidcClient.createSignoutRequest: Signout request has state to persist");
stateStore = stateStore || this._stateStore;
stateStore.set(signoutState.id, signoutState.toStorageString());
}
return request;
});
}
readSignoutResponseState(url, stateStore, removeState = false) {
Log.debug("OidcClient.readSignoutResponseState");
var response = new SignoutResponse(url);
if (!response.state) {
Log.debug("OidcClient.readSignoutResponseState: No state in response");
if (response.error) {
Log.warn("OidcClient.readSignoutResponseState: Response was error: ", response.error);
return Promise.reject(new ErrorResponse(response));
}
return Promise.resolve({state: undefined, response});
}
var stateKey = response.state;
stateStore = stateStore || this._stateStore;
var stateApi = removeState ? stateStore.remove.bind(stateStore) : stateStore.get.bind(stateStore);
return stateApi(stateKey).then(storedStateString => {
if (!storedStateString) {
Log.error("OidcClient.readSignoutResponseState: No matching state found in storage");
throw new Error("No matching state found in storage");
}
let state = State.fromStorageString(storedStateString);
return {state, response};
});
}
processSignoutResponse(url, stateStore) {
Log.debug("OidcClient.processSignoutResponse");
return this.readSignoutResponseState(url, stateStore, true).then(({state, response}) => {
if (state) {
Log.debug("OidcClient.processSignoutResponse: Received state from storage; validating response");
return this._validator.validateSignoutResponse(state, response);
}
else {
Log.debug("OidcClient.processSignoutResponse: No state from storage; skipping validating response");
return response;
}
});
}
clearStaleState(stateStore) {
Log.debug("OidcClient.clearStaleState");
stateStore = stateStore || this._stateStore;
return State.clearStaleState(stateStore, this.settings.staleStateAge);
}
}