1 |
|
2 |
|
3 |
|
4 | import { HttpRequestUser } from '@azure/functions';
|
5 | import { Headers } from 'undici';
|
6 | import { nonNullValue } from '../utils/nonNull';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export 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 | }
|