forked from authts/oidc-client-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoidc.js
More file actions
130 lines (113 loc) · 3.48 KB
/
Copy pathoidc.js
File metadata and controls
130 lines (113 loc) · 3.48 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
// 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.
var jsrsasign = require("jsrsasign");
var rsaKey = jsrsasign.KEYUTIL.generateKeypair("RSA", 1024);
var e = jsrsasign.hextob64u(rsaKey.pubKeyObj.e.toString(16));
var n = jsrsasign.hextob64u(rsaKey.pubKeyObj.n.toString(16));
var path = "/oidc";
var metadataPath = path + "/.well-known/openid-configuration";
var signingKeysPath = path + "/.well-known/jwks";
var authorizationPath = path + "/connect/authorize";
var userInfoPath = path + "/connect/userinfo";
var endSessionPath = path + "/connect/endsession";
var tokenPath = path + "/connect/token";
var metadata = {
issuer: path,
jwks_uri: signingKeysPath,
authorization_endpoint: authorizationPath,
userinfo_endpoint: userInfoPath,
end_session_endpoint: endSessionPath,
token_endpoint: tokenPath,
};
function prependBaseUrlToMetadata(baseUrl) {
for (var name in metadata) {
metadata[name] = baseUrl + metadata[name];
}
}
function encodeBase64Url(str) {
return Buffer.from(str).toString('base64')
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_');
}
var keys = {
keys: [
{
kty: "RSA",
use: "sig",
kid: "1",
e: e,
n: n
}
]
};
var claims = {
"sub": "818727",
"email": "AliceSmith@email.com",
"email_verified": true,
"role": ["Admin", "Geek"]
};
module.exports = function(baseUrl, app) {
prependBaseUrlToMetadata(baseUrl);
app.get(metadataPath, function(req, res) {
//res.send("<h1>not json...</h1>"); return;
res.json(metadata);
});
app.get(signingKeysPath, function(req, res) {
res.json(keys);
});
app.get(authorizationPath, function(req, res) {
var url = new URL(req.query.redirect_uri);
const paramsKey = req.query.response_mode === "fragment" ? "hash" : "search";
const params = new URLSearchParams(url[paramsKey].slice(1));
var state = req.query.state;
if (state) {
params.append('state', state);
if (req.query.code_challenge) {
params.append("code", "foo");
}
}
//params.append("error", "bad_stuff");
url[paramsKey] = params.toString();
if (req.query.display === 'popup') {
res.status(200);
res.type('text/html')
res.send(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="3;url=${url.href}" />
</head>
<body>
<h1>Redirecting in 3 seconds...</h1>
</body>
</html>`)
} else {
res.redirect(url.href);
}
});
app.get(userInfoPath, function(req, res) {
res.json(claims);
});
app.get(endSessionPath, function(req, res) {
var url = req.query.post_logout_redirect_uri;
if (url) {
var state = req.query.state;
if (state) {
url += "?state=" + state;
}
res.redirect(url);
}
else {
res.send("logged out");
}
});
app.post(tokenPath, function(req, res) {
res.json({
access_token: 'foobar',
token_type: 'Bearer',
id_token: [{ alg: 'none' }, claims]
.map(obj => JSON.stringify(obj))
.map(encodeBase64Url).join('.') + '.',
});
});
};