|
| 1 | +import { PayloadRequest } from "payload"; |
| 2 | +import { OAuth2Plugin, defaultGetToken } from "../src"; |
| 3 | + |
| 4 | +//////////////////////////////////////////////////////////////////////////////// |
| 5 | +// Microsoft Entra Id OAuth |
| 6 | +//////////////////////////////////////////////////////////////////////////////// |
| 7 | +const clientId = process.env.MICROSOFT_ENTRA_ID_CLIENT_ID; |
| 8 | +const clientSecret = process.env.MICROSOFT_ENTRA_ID_CLIENT_SECRET; |
| 9 | +const tenantId = process.env.MICROSOFT_ENTRA_ID_TENANT_ID; |
| 10 | +const administratorGroupId = |
| 11 | + process.env.MICROSOFT_ENTRA_ID_ADMINISTRATOR_GROUP_ID; |
| 12 | +const serverUrl = process.env.NEXT_PUBLIC_URL || "http://localhost:3000"; |
| 13 | + |
| 14 | +const strategyName = "microsoft-entra-id"; |
| 15 | +const tokenEndpoint = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`; |
| 16 | +const authorizationUrl = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/authorize`; |
| 17 | +const microsoftGraphBaseUrl = "https://graph.microsoft.com/v1.0"; |
| 18 | + |
| 19 | +export const microsoftEntraIdOAuth = OAuth2Plugin({ |
| 20 | + enabled: |
| 21 | + typeof clientId === "string" && |
| 22 | + typeof clientSecret === "string" && |
| 23 | + typeof tenantId === "string", |
| 24 | + strategyName, |
| 25 | + useEmailAsIdentity: true, |
| 26 | + serverURL: serverUrl, |
| 27 | + clientId: clientId || "", |
| 28 | + clientSecret: clientSecret || "", |
| 29 | + authorizePath: `/oauth/${strategyName}`, |
| 30 | + callbackPath: `/oauth/${strategyName}/callback`, |
| 31 | + authCollection: "users", |
| 32 | + tokenEndpoint: tokenEndpoint, |
| 33 | + scopes: ["openid", "email", "profile", "offline_access", "User.Read"], |
| 34 | + providerAuthorizationUrl: authorizationUrl, |
| 35 | + getUserInfo: async (accessToken: string, req: PayloadRequest) => { |
| 36 | + const userInfoResponse = await fetch(`${microsoftGraphBaseUrl}/me`, { |
| 37 | + headers: { Authorization: `Bearer ${accessToken}` }, |
| 38 | + }); |
| 39 | + |
| 40 | + /* |
| 41 | + * The following is an example of how to check if the user is a member of a specific group. |
| 42 | + * You can use this to restrict login when not part of the group, or to restrict access in payload. |
| 43 | + */ |
| 44 | + const groupsResponse = await fetch(`${microsoftGraphBaseUrl}/me/memberOf`, { |
| 45 | + headers: { Authorization: `Bearer ${accessToken}` }, |
| 46 | + }); |
| 47 | + const groups = |
| 48 | + (await groupsResponse.json()) as MicrosoftGraphGroupsResponse; |
| 49 | + |
| 50 | + const groupIds = groups.value.map((group) => group.id); |
| 51 | + |
| 52 | + if (administratorGroupId && !groupIds.includes(administratorGroupId)) |
| 53 | + throw new Error("You are not authorized to access this application."); |
| 54 | + |
| 55 | + const user = await userInfoResponse.json(); |
| 56 | + return { email: user.mail, sub: user.id, name: user.displayName }; |
| 57 | + }, |
| 58 | + /** |
| 59 | + * This param is optional to demonstrate how to customize your own |
| 60 | + * `getToken` function (i.e. add hooks to run after getting the token) |
| 61 | + * Leave this blank should you wish to use the default getToken function |
| 62 | + */ |
| 63 | + getToken: async (code: string, req: PayloadRequest) => { |
| 64 | + const redirectUri = `${serverUrl}/api/users/oauth/${strategyName}/callback`; |
| 65 | + const token = await defaultGetToken( |
| 66 | + tokenEndpoint, |
| 67 | + clientId || "", |
| 68 | + clientSecret || "", |
| 69 | + redirectUri, |
| 70 | + code, |
| 71 | + ); |
| 72 | + |
| 73 | + if (req.user) { |
| 74 | + await req.payload.update({ |
| 75 | + collection: "users", |
| 76 | + id: req.user.id, |
| 77 | + data: {}, |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + return token; |
| 82 | + }, |
| 83 | + successRedirect: (req: PayloadRequest, accessToken?: string) => { |
| 84 | + return "/admin"; |
| 85 | + }, |
| 86 | + failureRedirect: (req, err) => { |
| 87 | + req.payload.logger.error(err); |
| 88 | + return "/admin/login"; |
| 89 | + }, |
| 90 | +}); |
| 91 | + |
| 92 | +type MicrosoftGraphGroupsResponse = { |
| 93 | + value: MicrosoftGraphGroup[]; |
| 94 | +}; |
| 95 | + |
| 96 | +type MicrosoftGraphGroup = { |
| 97 | + id: string; |
| 98 | +}; |
0 commit comments