UNPKG

@storybooker/azure

Version:

StoryBooker Adapter for interacting with Azure services.

1 lines 4.61 kB
{"version":3,"file":"easy-auth.cjs","names":["DEFAULT_AUTHORISE: AuthServiceAuthorise<AzureEasyAuthUser>","clientPrincipal: {\n claims: { typ: string; val: string }[];\n auth_typ: string;\n name_typ: string;\n role_typ: string;\n }"],"sources":["../src/easy-auth.ts"],"sourcesContent":["// oxlint-disable class-methods-use-this\n// oxlint-disable require-await\n\nimport type {\n AuthService,\n AuthServiceAuthorise,\n Permission,\n StoryBookerUser,\n} from \"@storybooker/core/types\";\n\nexport type { AuthServiceAuthorise } from \"@storybooker/core/types\";\n\nexport interface AzureEasyAuthUser extends StoryBookerUser {\n roles: string[] | null;\n type: \"application\" | \"user\";\n}\n\nexport type AzureEasyAuthRoleMap = Map<string, Permission[]>;\n\nconst DEFAULT_AUTHORISE: AuthServiceAuthorise<AzureEasyAuthUser> = ({\n permission,\n user,\n}) => {\n if (!user) {\n return false;\n }\n\n if (user.type === \"application\") {\n return true;\n }\n\n if (permission.action === \"read\") {\n return true;\n }\n\n return Boolean(user.roles && user.roles.length > 0);\n};\n\nexport class AzureEasyAuthService implements AuthService<AzureEasyAuthUser> {\n authorise: AuthService<AzureEasyAuthUser>[\"authorise\"];\n\n constructor(\n authorise: AuthServiceAuthorise<AzureEasyAuthUser> = DEFAULT_AUTHORISE,\n ) {\n this.authorise = authorise;\n }\n\n getUserDetails: AuthService<AzureEasyAuthUser>[\"getUserDetails\"] = async (\n request,\n ) => {\n const principalHeader = request.headers.get(\"x-ms-client-principal\");\n if (!principalHeader) {\n throw new Response(\n `Unauthorized access. Please provide a valid EasyAuth principal header.`,\n { status: 401 },\n );\n }\n\n // Decode and parse the claims\n const decodedPrincipal = Buffer.from(principalHeader, \"base64\").toString(\n \"utf8\",\n );\n\n const clientPrincipal: {\n claims: { typ: string; val: string }[];\n auth_typ: string;\n name_typ: string;\n role_typ: string;\n } = JSON.parse(decodedPrincipal);\n const claims = clientPrincipal?.claims || [];\n\n const azpToken = claims.find((claim) => claim.typ === \"azp\")?.val;\n if (azpToken) {\n return {\n displayName: \"App\",\n id: azpToken,\n roles: null,\n type: \"application\",\n };\n }\n\n const name = claims.find((claim) => claim.typ === \"name\")?.val;\n const email = claims.find(\n (claim) => claim.typ === clientPrincipal.name_typ,\n )?.val;\n const roles = claims\n .filter(\n (claim) =>\n claim.typ === clientPrincipal.role_typ || claim.typ === \"roles\",\n )\n .map((claim) => claim.val);\n\n return {\n displayName: name || \"\",\n id: email || \"\",\n roles,\n title: roles.join(\", \"),\n type: \"user\",\n };\n };\n\n login: AuthService<AzureEasyAuthUser>[\"login\"] = async (request) => {\n const url = new URL(\"/.auth/login\", request.url);\n\n return new Response(null, {\n headers: { Location: url.toString() },\n status: 302,\n });\n };\n\n logout: AuthService<AzureEasyAuthUser>[\"logout\"] = async (request) => {\n const url = new URL(\"/.auth/logout\", request.url);\n\n return new Response(null, {\n headers: { Location: url.toString() },\n status: 302,\n });\n };\n}\n"],"mappings":";;AAmBA,MAAMA,qBAA8D,EAClE,YACA,WACI;AACJ,KAAI,CAAC,KACH,QAAO;AAGT,KAAI,KAAK,SAAS,cAChB,QAAO;AAGT,KAAI,WAAW,WAAW,OACxB,QAAO;AAGT,QAAO,QAAQ,KAAK,SAAS,KAAK,MAAM,SAAS,EAAE;;AAGrD,IAAa,uBAAb,MAA4E;CAG1E,YACE,YAAqD,mBACrD;wBAIiE,OACjE,YACG;GACH,MAAM,kBAAkB,QAAQ,QAAQ,IAAI,wBAAwB;AACpE,OAAI,CAAC,gBACH,OAAM,IAAI,SACR,0EACA,EAAE,QAAQ,KAAK,CAChB;GAIH,MAAM,mBAAmB,OAAO,KAAK,iBAAiB,SAAS,CAAC,SAC9D,OACD;GAED,MAAMC,kBAKF,KAAK,MAAM,iBAAiB;GAChC,MAAM,SAAS,iBAAiB,UAAU,EAAE;GAE5C,MAAM,WAAW,OAAO,MAAM,UAAU,MAAM,QAAQ,MAAM,EAAE;AAC9D,OAAI,S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