Skip to content

Commit 2102e1c

Browse files
authored
Merge pull request #975 from Azure/developer/long/issuer
fix: find auth_endpoint from OpenIDIssuer for aad
2 parents 11fe14d + 209f12e commit 2102e1c

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

package-lock.json

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@azure/static-web-apps-cli",
32
"version": "2.0.6",
3+
"name": "@azure/static-web-apps-cli",
44
"description": "Azure Static Web Apps CLI",
55
"type": "module",
66
"scripts": {
@@ -55,6 +55,7 @@
5555
"keytar": "^7.9.0",
5656
"node-fetch": "^2.7.0",
5757
"open": "^8.4.2",
58+
"openid-client": "^6.7.1",
5859
"ora": "^5.4.1",
5960
"pem": "^1.14.8",
6061
"prompts": "^2.4.2",

src/core/utils/openidHelper.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as client from "openid-client";
2+
3+
export class OpenIdHelper {
4+
private issuerUrl: URL;
5+
private clientId: string;
6+
7+
constructor(issuerUrl: string, clientId: string) {
8+
if (!issuerUrl || issuerUrl.trim() === "") {
9+
throw new Error("Issuer URL is required");
10+
}
11+
if (!clientId || clientId.trim() === "") {
12+
throw new Error("Client ID is required");
13+
}
14+
this.issuerUrl = new URL(issuerUrl);
15+
this.clientId = clientId;
16+
}
17+
18+
/**
19+
* Discover issuer metadata from the OpenID Connect provider
20+
*/
21+
async discoverIssuer() {
22+
return await client.discovery(this.issuerUrl, this.clientId);
23+
}
24+
25+
/**
26+
* Retrieve the authorization endpoint from the issuer
27+
*/
28+
async getAuthorizationEndpoint(): Promise<string> {
29+
const issuer = await this.discoverIssuer();
30+
if (!issuer.serverMetadata().authorization_endpoint) {
31+
throw new Error("Authorization endpoint not found in issuer metadata");
32+
}
33+
return issuer.serverMetadata().authorization_endpoint!;
34+
}
35+
}

src/msha/auth/routes/auth-login-provider-custom.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { response } from "../../../core/utils/net.js";
44
import { CUSTOM_AUTH_REQUIRED_FIELDS, ENTRAID_FULL_NAME, SWA_CLI_APP_PROTOCOL } from "../../../core/constants.js";
55
import { DEFAULT_CONFIG } from "../../../config.js";
66
import { encryptAndSign, extractPostLoginRedirectUri, hashStateGuid, newNonceWithExpiration } from "../../../core/utils/auth.js";
7+
import { OpenIdHelper } from "../../../core/utils/openidHelper.js";
78

89
export const normalizeAuthProvider = function (providerName?: string) {
910
if (providerName === ENTRAID_FULL_NAME) {
@@ -87,7 +88,8 @@ const httpTrigger = async function (context: Context, request: IncomingMessage,
8788
location = `https://github.com/login/oauth/authorize?response_type=code&client_id=${authFields?.clientIdSettingName}&redirect_uri=${redirectUri}/.auth/login/github/callback&scope=read:user&state=${hashedState}`;
8889
break;
8990
case "aad":
90-
location = `${authFields?.openIdIssuer}/authorize?response_type=code&client_id=${authFields?.clientIdSettingName}&redirect_uri=${redirectUri}/.auth/login/aad/callback&scope=openid+profile+email&state=${hashedState}`;
91+
const authorizationEndpoint = await new OpenIdHelper(authFields?.openIdIssuer, authFields?.clientIdSettingName).getAuthorizationEndpoint();
92+
location = `${authorizationEndpoint}?response_type=code&client_id=${authFields?.clientIdSettingName}&redirect_uri=${redirectUri}/.auth/login/aad/callback&scope=openid+profile+email&state=${hashedState}`;
9193
break;
9294
case "facebook":
9395
location = `https://facebook.com/v11.0/dialog/oauth?client_id=${authFields?.appIdSettingName}&redirect_uri=${redirectUri}/.auth/login/facebook/callback&scope=openid&state=${hashedState}&response_type=code`;

0 commit comments

Comments
 (0)