Skip to content

Commit

Permalink
Address PR comments. Change AAD variable to ENTRA_ID. Change route ma…
Browse files Browse the repository at this point in the history
…tching to use supported auth constant list.
  • Loading branch information
Timothy Wang committed Aug 8, 2024
1 parent 3b1a557 commit 6c2ca6c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ export const SWA_AUTH_COOKIE = `StaticWebAppsAuthCookie`;
export const ALLOWED_HTTP_METHODS_FOR_STATIC_CONTENT = ["GET", "HEAD", "OPTIONS"];

// Custom Auth constants
export const SUPPORTED_CUSTOM_AUTH_PROVIDERS = ["google", "github", "aad"];
// Full name is required in staticwebapp.config.json's schema so we will normalize it to aad
export const AAD_FULL_NAME = "azureActiveDirectory";
export const SUPPORTED_CUSTOM_AUTH_PROVIDERS = ["google", "github", "aad", "dummy"];
/*
The full name is required in staticwebapp.config.json's schema that will be normalized to aad
https://learn.microsoft.com/en-us/azure/static-web-apps/authentication-custom?tabs=aad%2Cinvitations
*/
export const ENTRAID_FULL_NAME = "azureActiveDirectory";
export const CUSTOM_AUTH_TOKEN_ENDPOINT_MAPPING: AuthIdentityTokenEndpoints = {
google: {
host: "oauth2.googleapis.com",
Expand Down
7 changes: 5 additions & 2 deletions src/msha/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ import type http from "node:http";
import { serializeCookie } from "../../core/utils/cookie.js";
import { logger } from "../../core/utils/logger.js";
import { response as newResponse } from "../../core/utils/net.js";
import { SUPPORTED_CUSTOM_AUTH_PROVIDERS } from "../../core/constants.js";

function getAuthPaths(isCustomAuth: boolean): Path[] {
const paths: Path[] = [];

if (isCustomAuth) {
const supportedAuthsRegex = SUPPORTED_CUSTOM_AUTH_PROVIDERS.join("|");

paths.push({
method: "GET",
// only match for providers with custom auth support implemented (github, google, aad)
route: /^\/\.auth\/login\/(?<provider>github|google|aad|dummy)\/callback(\?.*)?$/i,
route: new RegExp(`^/\\.auth/login/(?<provider>${supportedAuthsRegex})/callback(\\?.*)?$`, "i"),
function: "auth-login-provider-callback",
});
paths.push({
method: "GET",
// only match for providers with custom auth support implemented (github, google, aad)
route: /^\/\.auth\/login\/(?<provider>github|google|aad|dummy)(\?.*)?$/i,
route: new RegExp(`^/\\.auth/login/(?<provider>${supportedAuthsRegex})(\\?.*)?$`, "i"),
function: "auth-login-provider-custom",
});
paths.push({
Expand Down
4 changes: 2 additions & 2 deletions src/msha/auth/routes/auth-login-provider-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as querystring from "node:querystring";
import { CookiesManager, decodeAuthContextCookie, validateAuthContextCookie } from "../../../core/utils/cookie.js";
import { parseUrl, response } from "../../../core/utils/net.js";
import {
AAD_FULL_NAME,
ENTRAID_FULL_NAME,
CUSTOM_AUTH_ISS_MAPPING,
CUSTOM_AUTH_TOKEN_ENDPOINT_MAPPING,
CUSTOM_AUTH_USER_ENDPOINT_MAPPING,
Expand Down Expand Up @@ -335,7 +335,7 @@ const httpTrigger = async function (context: Context, request: http.IncomingMess
}

const { clientIdSettingName, clientSecretSettingName, openIdIssuer } =
customAuth?.identityProviders?.[providerName == "aad" ? AAD_FULL_NAME : providerName]?.registration || {};
customAuth?.identityProviders?.[providerName == "aad" ? ENTRAID_FULL_NAME : providerName]?.registration || {};

if (!clientIdSettingName) {
context.res = response({
Expand Down
8 changes: 4 additions & 4 deletions src/msha/auth/routes/auth-login-provider-custom.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { IncomingMessage } from "node:http";
import { CookiesManager } from "../../../core/utils/cookie.js";
import { response } from "../../../core/utils/net.js";
import { AAD_FULL_NAME, SUPPORTED_CUSTOM_AUTH_PROVIDERS, SWA_CLI_APP_PROTOCOL } from "../../../core/constants.js";
import { ENTRAID_FULL_NAME, SUPPORTED_CUSTOM_AUTH_PROVIDERS, SWA_CLI_APP_PROTOCOL } from "../../../core/constants.js";
import { DEFAULT_CONFIG } from "../../../config.js";
import { encryptAndSign, extractPostLoginRedirectUri, hashStateGuid, newNonceWithExpiration } from "../../../core/utils/auth.js";

export const normalizeAuthProvider = (providerName?: string) => {
if (providerName === AAD_FULL_NAME) {
if (providerName === ENTRAID_FULL_NAME) {
return "aad";
}
return providerName?.toLowerCase() || "";
Expand All @@ -28,7 +28,7 @@ const httpTrigger = async function (context: Context, request: IncomingMessage,
}

const clientIdSettingName =
customAuth?.identityProviders?.[providerName == "aad" ? AAD_FULL_NAME : providerName]?.registration?.clientIdSettingName;
customAuth?.identityProviders?.[providerName == "aad" ? ENTRAID_FULL_NAME : providerName]?.registration?.clientIdSettingName;

if (!clientIdSettingName) {
context.res = response({
Expand All @@ -54,7 +54,7 @@ const httpTrigger = async function (context: Context, request: IncomingMessage,

let aadIssuer;
if (providerName == "aad") {
aadIssuer = customAuth?.identityProviders?.[AAD_FULL_NAME]?.registration?.openIdIssuer;
aadIssuer = customAuth?.identityProviders?.[ENTRAID_FULL_NAME]?.registration?.openIdIssuer;

if (!aadIssuer) {
context.res = response({
Expand Down

0 comments on commit 6c2ca6c

Please sign in to comment.