forked from Linaro/works-on-woa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.config.ts
172 lines (159 loc) · 4.03 KB
/
auth.config.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
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
// auth.config.ts
import { defineConfig } from "auth-astro";
import {
verifyBiscuitUser,
parseBiscuitMetadata,
type Bwks,
} from "./src/lib/auth";
const {
AUTH0_CLIENT_ID,
AUTH0_CLIENT_SECRET,
AUTH0_ISSUER_BASE,
AUTH_API_URL,
SPIRE_WEBSITES_ID,
PUBLIC_KEY_URL,
} = import.meta.env;
const isDev = import.meta.env.DEV;
const WEBSITE_URL = import.meta.env.SITE;
function getPublicKeys() {
return fetch(`${PUBLIC_KEY_URL}`, {
cache: "no-store",
});
}
async function getBiscuitFromJWT(accessToken: string) {
const res = await fetch(`${AUTH_API_URL}/session`, {
headers: {
Authorization: `Bearer ${accessToken}`
}
});
const resJson = await res.json();
if (!res.ok || !resJson) {
console.log(resJson);
throw new Error("failed to fetch biscuit from session");
}
return resJson.biscuit.token;
}
async function afterToken(accessToken: string) {
// fetch biscuit public keys
const res = await getPublicKeys();
const public_keys = await res.json();
if (!res.ok || !public_keys) {
console.error(public_keys);
throw new Error("failed to fetch public keys");
}
// verify biscuit and extract metadata
const auth = verifyBiscuitUser(accessToken, public_keys, SPIRE_WEBSITES_ID);
const metadata = parseBiscuitMetadata(auth);
if (!metadata) {
throw new Error("error extracting biscuit metadata");
}
const [id, first_name, surname] = metadata;
return {
profile: {
id,
first_name,
surname,
},
public_keys,
};
}
export default defineConfig({
providers: [
{
id: "spire",
name: "spire",
type: "oidc",
clientId: AUTH0_CLIENT_ID,
clientSecret: AUTH0_CLIENT_SECRET,
checks: ["pkce", "state"],
authorization: {
url: `${AUTH_API_URL}/oauth/authorize`,
params: {
scope: "openid email offline_access profile",
client_id: AUTH0_CLIENT_ID,
response_type: "code",
},
},
issuer: AUTH0_ISSUER_BASE,
jwks_endpoint: AUTH0_ISSUER_BASE + ".well-known/jwks.json",
client: {
token_endpoint_auth_method: "client_secret_post",
},
token: {
url: `${AUTH_API_URL}/oauth/token`,
params: {
scope: "openid email offline_access profile",
response_type: "code",
client_id: AUTH0_CLIENT_ID,
},
httpOptions: {
timeout: 30000,
},
},
profile: (profile: any) => {
return {
id: profile.sub,
...profile,
};
},
},
],
callbacks: {
async session({ session, token }: any) {
session.profile = token.profile;
session.access_token = token.access_token;
session.public_keys = token.public_keys;
session.expires_at = token.expires_at;
return session;
},
async jwt({ account, token }) {
// this is only run after sign in
if (account) {
try {
let biscuit = await getBiscuitFromJWT(account.access_token!);
const { profile, public_keys } = await afterToken(
biscuit
);
return {
access_token: biscuit,
// refresh_token: account.refresh_token,
expires_at: account.expires_at,
public_keys,
profile,
};
} catch (error) {
console.error(error);
throw new Error("AccessDenied");
}
}
// if not sign in and token is valid, return as is
return token;
},
},
pages: {
signIn: "/auth/signin",
error: "/auth/error",
},
});
declare module "@auth/core" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
profile: any;
}
}
declare module "@auth/core" {
interface JWT {
access_token: string;
expires_at: number;
// refresh_token: string;
public_keys: Bwks[];
profile: {
id: string;
first_name: string;
surname: string;
};
error?: "RefreshAccessTokenError" | "BiscuitPublicKeysError";
}
}