forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuthProvider.ts
More file actions
440 lines (419 loc) · 19.9 KB
/
Copy pathOAuthProvider.ts
File metadata and controls
440 lines (419 loc) · 19.9 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// import * as OAuthServer from "oauth2-server";
import * as express from "express";
import { TokenUser, Base, NoderedUtil, User, InsertOrUpdateOneMessage } from "@openiap/openflow-api";
import { Config } from "./Config";
import { Crypt } from "./Crypt";
import { Provider, KoaContextWithOIDC } from "oidc-provider";
import { MongoAdapter } from "./MongoAdapter";
import { Span } from "@opentelemetry/api";
import { Logger } from "./Logger";
import { Audit } from "./Audit";
// const Request = OAuthServer.Request;
// const Response = OAuthServer.Response;
export class OAuthProvider {
private app: express.Express;
public static instance: OAuthProvider = null;
public clients = [];
public oidc: Provider;
static async interactionsUrl(ctx: KoaContextWithOIDC, interaction): Promise<any> {
return "/oidclogin";
}
static async logoutSource(ctx, form) {
if (ctx && ctx.oidc && ctx.oidc.session && ctx.oidc.session.authorizations) {
for (var i = 0; i < this.instance.clients.length; i++) {
var cli = this.instance.clients[i];
var auth = ctx.oidc.session.authorizations[cli.id];
if (auth) {
if (cli.openflowsignout && cli.openflowsignout == true) {
ctx.req.logout();
}
if (cli.signin_url) {
ctx.res.cookie("oidcrefere", cli.signin_url, { maxAge: 900000, httpOnly: true });
}
}
}
}
ctx.body = `<!DOCTYPE html>
<head>
<title>Logout Request</title>
<style>/* css and html classes omitted for brevity, see lib/helpers/defaults.js */</style>
</head>
<body onload="logout()">
<div>
<h1>Do you want to sign-out from ${ctx.hostname}?</h1>
<script>
function logout() {
var form = document.getElementById('op.logoutForm');
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'logout';
input.value = 'yes';
form.appendChild(input);
form.submit();
}
function rpLogoutOnly() {
var form = document.getElementById('op.logoutForm');
form.submit();
}
</script>
${form}
<button onclick="logout()">Yes, sign me out</button>
<button onclick="rpLogoutOnly()">No, stay signed in</button>
</div>
</body>
</html>`;
}
static async postLogoutSuccessSource(ctx) {
// @param ctx - koa request context
// @param form - form source (id="op.logoutForm") to be embedded in the page and submitted by
// the End-User
var oidcrefere = "";
if (!NoderedUtil.IsNullEmpty(ctx.req.cookies.oidcrefere)) {
oidcrefere = ctx.req.cookies.oidcrefere;
}
ctx.body = `<!DOCTYPE html>
<head>
<title>Logout Request</title>
<style>/* css and html classes omitted for brevity, see lib/helpers/defaults.js */</style>
</head>
<body onload="logout()">
<div>
<h1>You have successfully signed out from ${ctx.hostname}</h1>`;
if (!NoderedUtil.IsNullEmpty(oidcrefere)) {
ctx.body += `<a href="${ctx.req.cookies.oidcrefere}">Return to ${ctx.req.cookies.oidcrefere}</a> ?`;
}
ctx.body += `</div>
</body>
</html>`;
if (!NoderedUtil.IsNullEmpty(ctx.req.cookies.oidcrefere)) {
// ctx.res.cookie("oidcrefere", "", { expires: new Date(0) });
// LoginProvider.redirect(ctx.res, ctx.req.cookies.oidcrefere);
}
}
static store = new Map();
public static generatekeys() {
return new Promise((resolve, reject) => {
const jose = require('jose');
const keystore = new jose.JWKS.KeyStore();
Promise.all([
keystore.generate('RSA', 2048, { use: 'sig' }),
keystore.generate('RSA', 2048, { use: 'enc' }),
keystore.generate('EC', 'P-256', { use: 'sig' }),
keystore.generate('EC', 'P-256', { use: 'enc' }),
keystore.generate('OKP', 'Ed25519', { use: 'sig' }),
]).then(() => {
resolve(keystore.toJWKS(true));
}).catch((err) => {
reject(err);
});
});
}
public static async LoadClients() {
const instance = OAuthProvider.instance;
const span = Logger.otel.startSpan("OAuthProvider.LoadClients");
try {
const jwksresults = await Config.db.query<Base>({ query: { _type: "jwks" }, top: 10, collectionname: "config", jwt: Crypt.rootToken() }, span);
let jwks = null;
if (jwksresults.length == 0) {
jwks = await this.generatekeys();
jwks._type = "jwks";
await Config.db.InsertOne(jwks, "config", 1, true, Crypt.rootToken(), span);
} else {
jwks = jwksresults[0];
}
const result = await Config.db.query<Base>({ query: { _type: "oauthclient" }, top: 10, collectionname: "config", jwt: Crypt.rootToken() }, span);
instance.clients = result;
instance.clients.forEach(cli => {
cli.client_id = cli.clientId;
cli.client_secret = cli.clientSecret;
cli.redirect_uris = cli.redirectUris;
// token_endpoint_auth_method can only be none, client_secret_post, client_secret_basic, private_key_jwt or tls_client_auth
if (NoderedUtil.IsNullEmpty(cli.token_endpoint_auth_method)) cli.token_endpoint_auth_method = "none";
// response_types can only contain 'code id_token', 'code', 'id_token', or 'none'
// id_token token
if (NoderedUtil.IsNullEmpty(cli.response_types)) cli.response_types = ['code', 'id_token', 'code id_token'];
// https://github.com/panva/node-oidc-provider/blob/64edda69a84e556531f45ac814788c8c92ab6212/test/claim_types/claim_types.test.js
if (cli.grant_types == null) cli.grant_types = ['implicit', 'authorization_code'];
});
const provider = new Provider(Config.baseurl() + "oidc", {
clients: instance.clients,
adapter: MongoAdapter,
formats: {
AccessToken: 'jwt',
},
ttl: {
AccessToken: (Config.oidc_access_token_ttl * 60),
AuthorizationCode: (Config.oidc_authorization_code_ttl * 60),
ClientCredentials: (Config.oidc_client_credentials_ttl * 60),
RefreshToken: (Config.oidc_refresh_token_ttl * 60),
Session: (Config.oidc_session_ttl * 60)
},
jwks: jwks,
features: {
encryption: { enabled: true },
introspection: { enabled: true },
revocation: { enabled: true },
devInteractions: { enabled: false },
clientCredentials: { enabled: true },
userinfo: { enabled: true },
jwtUserinfo: { enabled: true },
claimsParameter: { enabled: false },
rpInitiatedLogout: {
enabled: true,
logoutSource: this.logoutSource.bind(this),
postLogoutSuccessSource: this.postLogoutSuccessSource.bind(this)
}
},
claims: {
acr: null,
auth_time: null,
iss: null,
openid: [
'sub', 'name', 'email', 'email_verified', 'role', 'roles'
],
sid: null
},
conformIdTokenClaims: false,
interactions: {
url: this.interactionsUrl
},
findAccount: Account.findAccount,
cookies: {
short: {
path: '/',
},
keys: [Config.oidc_cookie_key],
},
});
provider.proxy = true;
const { invalidate: orig } = (provider.Client as any).Schema.prototype;
(provider.Client as any).Schema.prototype.invalidate = function invalidate(message, code) {
if (code === 'implicit-force-https' || code === 'implicit-forbid-localhost') {
return;
}
if (message === 'redirect_uris must contain members') {
return;
}
orig.call(this, message);
};
const orgpostLogoutRedirectUriAllowed = provider.Client.prototype.postLogoutRedirectUriAllowed;
provider.Client.prototype.postLogoutRedirectUriAllowed = function (value) {
if (this.postLogoutRedirectUris == null || this.postLogoutRedirectUris.length == 0) return true;
return orgpostLogoutRedirectUriAllowed(value);
};
const orgredirectUriAllowed = provider.Client.prototype.redirectUriAllowed;
provider.Client.prototype.redirectUriAllowed = function (value) {
if (this.redirectUris == null || this.redirectUris.length == 0) return true;
return orgredirectUriAllowed(value);
};
if (instance.oidc != null) {
instance.oidc = provider;
return;
}
instance.oidc = provider;
instance.app.use('/oidc', async (req, res, next) => {
if (req.originalUrl == "/oidc/me/emails") { // Grafana old school hack
res.send('[]');
return;
}
if (req.originalUrl.startsWith("/oidc/session/end")) {
if (!NoderedUtil.IsNullEmpty(req.headers.referer)) {
if (req.headers.referer.indexOf("oidc/session") == -1) {
res.cookie("oidcrefere", req.headers.referer, { maxAge: 900000, httpOnly: true });
}
}
}
instance.oidc.callback(req, res);
// return next();
// if (req.originalUrl.indexOf('/oidc') > -1) return next();
});
instance.app.use('/oidclogin', async (req, res, next) => {
if (req && (req as any).user) {
var validated = true;
if (Config.validate_user_form != "") {
if (!(req as any).user.formvalidated) validated = false;
}
if (Config.validate_emails) {
if (!(req as any).user.emailvalidated) validated = false;
}
if (!validated) {
res.cookie("originalUrl", "/oidclogin", { maxAge: 900000, httpOnly: true });
res.redirect("/login");
return next();
}
res.cookie("originalUrl", "/oidccb", { maxAge: 900000, httpOnly: true });
res.redirect("/oidccb");
} else {
res.cookie("originalUrl", "/oidclogin", { maxAge: 900000, httpOnly: true });
res.redirect("/login");
}
});
instance.app.use('/oidccb', async (req, res, next) => {
try {
const {
uid, prompt, params, session,
} = await this.instance.oidc.interactionDetails(req, res);
var r = req;
var u = req.user;
const isAuthenticated: boolean = req.isAuthenticated();
if (isAuthenticated) {
} else {
res.cookie("originalUrl", "/oidccb", { maxAge: 900000, httpOnly: true });
res.redirect('/login');
return;
}
if (req.user) {
const client = await this.instance.clients.filter(x => x.clientId == params.client_id)[0];
let _user: User = req.user as any;
let tuser: TokenUser = TokenUser.From(_user);
if (!NoderedUtil.IsNullEmpty(_user.impersonating)) {
const tempjwt = Crypt.createToken(tuser, Config.shorttoken_expires_in);
const items = await Config.db.query({ query: { _id: _user.impersonating }, top: 1, collectionname: "users", jwt: tempjwt }, span);
if (items.length == 1) {
const tuserimpostor = tuser;
_user = User.assign(items[0] as User);
tuser = TokenUser.From(_user);
Logger.instanse.info("Message", "Signin", tuser.username + " successfully impersonated");
await Audit.ImpersonateSuccess(tuser, tuserimpostor, "browser", Config.version, span);
}
}
Account.AddAccount(tuser, client);
await this.instance.oidc.interactionFinished(req, res,
// result should be an object with some or all the following properties
{
// authentication/login prompt got resolved, omit if no authentication happened, i.e. the user
// cancelled
login: {
account: tuser._id, // logged-in account id
acr: "acr", // acr value for the authentication
remember: false, // true if provider should use a persistent cookie rather than a session one, defaults to true
// ts: number, // unix timestamp of the authentication, defaults to now()
},
// consent was given by the user to the client for this session
consent: {
rejectedScopes: [], // array of strings, scope names the end-user has not granted
rejectedClaims: [], // array of strings, claim names the end-user has not granted
},
// meta is a free object you may store alongside an authorization. It can be useful
// during the interaction check to verify information on the ongoing session.
meta: {
// object structure up-to-you
"openflow": "true"
},
['custom prompt name resolved']: {},
}
);
}
} catch (error) {
if (error.name == "SessionNotFound") {
res.redirect(`/`);
res.end();
return;
}
res.json(error)
}
});
} catch (error) {
span?.recordException(error);
Logger.instanse.error("OAuthProvider", "LoadClients", error);
}
finally {
Logger.otel.endSpan(span);
}
}
static configure(app: express.Express, parent: Span): OAuthProvider {
const span: Span = Logger.otel.startSubSpan("OAuthProvider.configure", parent);
try {
const instance = new OAuthProvider();
try {
OAuthProvider.instance = instance;
instance.app = app;
// @ts-ignore
this.LoadClients();
} catch (error) {
Logger.instanse.error("OAuthProvider", "configure", error);
throw error;
}
return instance;
} catch (error) {
span?.recordException(error);
Logger.instanse.error("OAuthProvider", "configure", error);
return OAuthProvider.instance;
} finally {
Logger.otel.endSpan(span);
}
}
}
export class Account {
constructor(public accountId: string, public user: TokenUser) {
Logger.DBHelper.DeleteKey("user" + accountId);
if (user == null) throw new Error("Cannot create Account from null user for id ${this.accountId}");
user = Object.assign(user, { accountId: accountId, sub: accountId });
// node-bb username hack
if (NoderedUtil.IsNullEmpty(user.email)) user.email = user.username;
if (user.name == user.email && user.email.indexOf("@") > -1) {
user.name = user.email.substr(0, user.email.indexOf("@") - 1);
}
if (user.name == user.email && user.email.indexOf("@") == -1) {
user.email = user.email + "@unknown.local"
}
if (user.name == user.email) {
user.name = "user " + user.email;
}
if (!NoderedUtil.IsNullUndefinded(user.roles) && Array.isArray(user.roles) && user.roles.length > 0) {
if (!NoderedUtil.IsNullEmpty(user.roles[0].name)) {
user.roles = user.roles.map(x => x.name) as any;
}
} else {
user.roles = ["users"] as any;
}
}
claims() {
return this.user;
}
static async findAccount(ctx: KoaContextWithOIDC, id, test): Promise<any> {
let acc = await Logger.DBHelper.memoryCache.get("oidc" + id);
if (acc == null) {
acc = await Logger.DBHelper.FindById(id, undefined, undefined);
}
var res = new Account(id, TokenUser.From(acc))
return res;
}
static AddAccount(tuser: TokenUser, client: any) {
try {
let role = client.defaultrole;
const keys: string[] = Object.keys(client.rolemappings);
Logger.instanse.debug("OAuthProvider", "AddAccount", "[" + tuser.username + "] Lookup roles for " + tuser.username);
for (let i = 0; i < keys.length; i++) {
if (tuser.HasRoleName(keys[i])) {
Logger.instanse.debug("OAuthProvider", "AddAccount", "[" + tuser.username + "] User has role " + keys[i] + " set role " + client.rolemappings[keys[i]]);
role = client.rolemappings[keys[i]];
}
}
(tuser as any).role = role;
Logger.DBHelper.memoryCache.set("oidc" + tuser._id, tuser);
// DBHelper.DeleteKey("user" + tuser._id);
var res = new Account(tuser._id, tuser);
return res;
} catch (error) {
Logger.instanse.error("OAuthProvider", "AddAccount", error);
}
return undefined;
}
static async GetTokenRequest(code: string, parent: Span) {
let tokens = await Config.db.query<Base>({ query: { _type: "tokenrequest", "code": code }, top: 1, collectionname: "oauthtokens", jwt: Crypt.rootToken() }, parent);
if (tokens.length == 0) return null;
return tokens[0];
}
static async AddTokenRequest(code: string, item: Base, parent: Span) {
var q: InsertOrUpdateOneMessage = new InsertOrUpdateOneMessage();
q.item = item; q.uniqeness = "_type,code"; q.collectionname = "oauthtokens", q.jwt = Crypt.rootToken();
q.w = 1; q.j = true;
let token = await Config.db.InsertOrUpdateOne<Base>(q, parent);
return token.item;
}
static async RemoveTokenRequest(code: string, parent: Span) {
let tokens = await Config.db.DeleteMany({ _type: "tokenrequest", "code": code }, null, "oauthtokens", null, Crypt.rootToken(), parent);
return tokens[0];
}
}