Skip to content

Commit

Permalink
Azure AD provider: Use userinfo endpoint (#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Aug 22, 2023
1 parent 30a114d commit 9c31e82
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
8 changes: 4 additions & 4 deletions documentation/content/oauth/providers/azure-ad.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ type AzureADTokens = {
```ts
type AzureADUser = {
sub: string;
roles: string[];
oid: string;
name: string;
preferred_username: string;
email?: string; // require `email` scope
family_name: string;
given_name: string;
picture: string;
email?: string; // requires `email` scope
};
```

Expand Down
23 changes: 18 additions & 5 deletions packages/oauth/src/providers/azure-ad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { decodeIdToken } from "../core/oidc.js";
import { ProviderUserAuth } from "../core/provider.js";

import type { Auth } from "lucia";
import { authorizationHeader, handleRequest } from "../utils/request.js";

type Config = {
clientId: string;
Expand Down Expand Up @@ -62,7 +63,7 @@ export class AzureADAuth<
code,
code_verifier
);
const azureADUser = decodeIdToken<AzureADUser>(azureADTokens.idToken);
const azureADUser = await getAzureADUser(azureADTokens.accessToken);
return new AzureADUserAuth(this.auth, azureADUser, azureADTokens);
};

Expand Down Expand Up @@ -97,6 +98,18 @@ export class AzureADAuth<
};
}

const getAzureADUser = async (accessToken: string): Promise<AzureADUser> => {
const azureADUserRequest = new Request(
"https://graph.microsoft.com/oidc/userinfo",
{
headers: {
Authorization: authorizationHeader("bearer", accessToken)
}
}
);
return await handleRequest(azureADUserRequest);
};

export class AzureADUserAuth<
_Auth extends Auth = Auth
> extends ProviderUserAuth<_Auth> {
Expand All @@ -123,9 +136,9 @@ export type AzureADTokens = {

export type AzureADUser = {
sub: string;
roles: string[];
oid: string;
name: string;
preferred_username: string;
email?: string; // may require `email` scope
family_name: string;
given_name: string;
picture: string;
email?: string;
};
16 changes: 10 additions & 6 deletions packages/oauth/src/providers/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ export class GithubAuth<_Auth extends Auth = Auth> extends OAuth2ProviderAuth<
code: string
): Promise<GithubUserAuth<_Auth>> => {
const githubTokens = await this.validateAuthorizationCode(code);
const githubUserRequest = new Request("https://api.github.com/user", {
headers: {
Authorization: authorizationHeader("bearer", githubTokens.accessToken)
}
});
const githubUser = await handleRequest<GithubUser>(githubUserRequest);
const githubUser = await getGithubUser(githubTokens.accessToken);
return new GithubUserAuth(this.auth, githubUser, githubTokens);
};

Expand Down Expand Up @@ -91,6 +86,15 @@ export class GithubAuth<_Auth extends Auth = Auth> extends OAuth2ProviderAuth<
};
}

const getGithubUser = async (accessToken: string): Promise<GithubUser> => {
const githubUserRequest = new Request("https://api.github.com/user", {
headers: {
Authorization: authorizationHeader("bearer", accessToken)
}
});
return await handleRequest<GithubUser>(githubUserRequest);
};

export class GithubUserAuth<
_Auth extends Auth
> extends ProviderUserAuth<_Auth> {
Expand Down

0 comments on commit 9c31e82

Please sign in to comment.