UNPKG

1.59 kBPlain TextView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4import { HttpRequestUser } from '@azure/functions';
5import { Headers } from 'undici';
6import { nonNullValue } from '../utils/nonNull';
7
8/* grandfathered in. Should fix when possible */
9/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access */
10
11export function extractHttpUserFromHeaders(headers: Headers): HttpRequestUser | null {
12 let user: HttpRequestUser | null = null;
13
14 const clientPrincipal = headers.get('x-ms-client-principal');
15 if (clientPrincipal) {
16 const claimsPrincipalData = JSON.parse(Buffer.from(clientPrincipal, 'base64').toString('utf-8'));
17
18 if (claimsPrincipalData['identityProvider']) {
19 user = {
20 type: 'StaticWebApps',
21 id: claimsPrincipalData['userId'],
22 username: claimsPrincipalData['userDetails'],
23 identityProvider: claimsPrincipalData['identityProvider'],
24 claimsPrincipalData,
25 };
26 } else {
27 user = {
28 type: 'AppService',
29 id: nonNullValue(headers.get('x-ms-client-principal-id'), 'user-id'),
30 username: nonNullValue(headers.get('x-ms-client-principal-name'), 'user-name'),
31 identityProvider: nonNullValue(headers.get('x-ms-client-principal-idp'), 'user-idp'),
32 claimsPrincipalData,
33 };
34 }
35 }
36
37 return user;
38}