-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
143 lines (130 loc) · 3.78 KB
/
index.ts
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
import express from "express";
import path from "path";
import session from "express-session";
import {
createRelyingParty,
UserInfo,
Tokens,
createLogRotatingFilesystem,
createAuditLogRotatingFilesystem,
createInMemoryAsyncStorage,
} from "spid-cie-oidc";
const port = process.env.PORT ?? 3000;
const client_id = process.env.CLIENT_ID ?? `http://127.0.0.1:${port}/oidc/rp/`;
const trust_anchors = process.env.TRUST_ANCHOR
? [process.env.TRUST_ANCHOR]
: ["http://127.0.0.1:8000/"];
const identity_providers = process.env.IDENTITY_PROVIDER
? [process.env.IDENTITY_PROVIDER]
: ["http://127.0.0.1:8000/oidc/op/"];
const relyingParty = createRelyingParty({
client_id,
client_name: "My Application",
trust_anchors,
identity_providers: {
spid: identity_providers,
cie: ["http://127.0.0.1:8002/oidc/op/"],
},
public_jwks_path: "./public.jwks.json",
private_jwks_path: "./private.jwks.json",
trust_marks_path: "./trust_marks.json",
logger: createLogRotatingFilesystem(),
auditLogger: createAuditLogRotatingFilesystem(),
storage: createInMemoryAsyncStorage(),
});
relyingParty.validateConfiguration().catch((error) => {
console.error(error);
process.exit(1);
});
const app = express();
app.use(session({ secret: "spid-cie-oidc-nodejs" }));
declare module "express-session" {
interface SessionData {
user_info?: UserInfo;
tokens?: Tokens;
}
}
app.get("/oidc/rp/providers", async (req, res) => {
try {
res.json(await relyingParty.retrieveAvailableProviders());
} catch (error) {
res.status(500).json(error);
}
});
app.get("/oidc/rp/authorization", async (req, res) => {
try {
res.redirect(
await relyingParty.createAuthorizationRedirectURL(
req.query.provider as string
)
);
} catch (error) {
res.status(500).json(error);
}
});
app.get("/oidc/rp/callback", async (req, res) => {
try {
const outcome = await relyingParty.manageCallback(req.query as any);
switch (outcome.type) {
case "authentication-success": {
req.session.user_info = outcome.user_info;
req.session.tokens = outcome.tokens;
res.redirect(`/attributes`);
break;
}
case "authentication-error": {
res.redirect(
`/error?${new URLSearchParams({
error: outcome.error,
error_description: outcome.error_description ?? "",
})}`
);
break;
}
}
} catch (error) {
res.status(500).json(error);
}
});
app.get("/oidc/rp/revocation", async (req, res) => {
try {
if (!req.session.tokens) {
res.status(400).json({ error: "user is not logged in" });
} else {
await relyingParty.revokeTokens(req.session.tokens);
req.session.destroy(() => {
res.json({ message: "user logged out" });
});
}
} catch (error) {
res.status(500).json(error);
}
});
app.get("/oidc/rp/.well-known/openid-federation", async (req, res) => {
try {
const response = await relyingParty.createEntityConfigurationResponse();
res.status(response.status);
res.set("Content-Type", response.headers["Content-Type"]);
res.send(response.body);
} catch (error) {
res.status(500).json(error);
}
});
// this endpoint is outside of the oidc lib
// so you can provide your own way of storing and retreiving user data
app.get("/oidc/rp/user_info", (req, res) => {
if (req.session.user_info) {
res.json(req.session.user_info);
} else {
res.status(401).send("User is not legged in");
}
});
// serve frontend static files
app.use(express.static("frontend/build"));
// every route leads back to index beacuse it is a single page application
app.get("*", (req, res) =>
res.sendFile(path.resolve("frontend/build/index.html"))
);
app.listen(port, () => {
console.log(`Open browser at http://127.0.0.1:${port}`);
});