-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
84 lines (72 loc) · 2.7 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
import type { OAuth2Adapter } from "adminforth";
import { jwtDecode } from "jwt-decode";
export default class AdminForthAdapterMicrosoftOauth2 implements OAuth2Adapter {
private clientID: string;
private clientSecret: string;
private useOpenID: boolean;
constructor(options: {
clientID: string;
clientSecret: string;
useOpenID?: boolean;
}) {
this.clientID = options.clientID;
this.clientSecret = options.clientSecret;
this.useOpenID = options.useOpenID ?? true;
}
getAuthUrl(): string {
const params = new URLSearchParams({
client_id: this.clientID,
response_type: 'code',
scope: 'openid email profile https://graph.microsoft.com/user.read',
response_mode: 'query',
redirect_uri: 'http://localhost:3000/oauth/callback',
});
return `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?${params.toString()}`;
}
async getTokenFromCode(code: string, redirect_uri: string): Promise<{ email: string; }> {
const tokenResponse = await fetch('https://login.microsoftonline.com/common/oauth2/v2.0/token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
code,
client_id: this.clientID,
client_secret: this.clientSecret,
redirect_uri,
grant_type: 'authorization_code',
}),
});
const tokenData = await tokenResponse.json();
if (tokenData.error) {
console.error('Token error:', tokenData);
throw new Error(tokenData.error_description || tokenData.error);
}
if (this.useOpenID && tokenData.id_token) {
try {
const decodedToken: any = jwtDecode(tokenData.id_token);
if (decodedToken.email) {
return { email: decodedToken.email };
}
} catch (error) {
console.error("Error decoding token:", error);
}
}
const userResponse = await fetch('https://graph.microsoft.com/v1.0/me', {
headers: { Authorization: `Bearer ${tokenData.access_token}` },
});
const userData = await userResponse.json();
if (userData.error) {
throw new Error(userData.error.message || 'Failed to fetch user data');
}
return {
email: userData.mail || userData.userPrincipalName,
};
}
getIcon(): string {
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 21 21" fill="none">
<path d="M0 0H10V10H0V0Z" fill="#F35325"/>
<path d="M11 0H21V10H11V0Z" fill="#81BC06"/>
<path d="M0 11H10V21H0V11Z" fill="#05A6F0"/>
<path d="M11 11H21V21H11V11Z" fill="#FFBA08"/>
</svg>`;
}
}