Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/http/extractHttpUserFromHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export function extractHttpUserFromHeaders(headers: Headers): HttpRequestUser |
user = {
type: 'AppService',
id: nonNullValue(headers.get('x-ms-client-principal-id'), 'user-id'),
username: nonNullValue(headers.get('x-ms-client-principal-name'), 'user-name'),
// App Service sends no name header for callers without a name claim, such as service principals
username: headers.get('x-ms-client-principal-name') ?? '',
identityProvider: nonNullValue(headers.get('x-ms-client-principal-idp'), 'user-idp'),
claimsPrincipalData,
};
Expand Down
34 changes: 34 additions & 0 deletions test/http/extractHttpUserFromHeaders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,40 @@ describe('Extract Http User Claims Principal from Headers', () => {
expect(user?.claimsPrincipalData).to.deep.equal(claimsPrincipalData);
});

it('Parses AppService headers without a principal name', () => {
// A service principal token has no name claim, so App Service leaves out x-ms-client-principal-name
const id = 'testId';
const provider = 'aad';
const claimsPrincipalData = {
auth_typ: provider,
claims: [
{ typ: 'appid', val: 'testAppId' },
{
typ: 'http://schemas.microsoft.com/identity/claims/objectidentifier',
val: id,
},
{ typ: 'ver', val: '2.0' },
],
name_typ: 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name',
role_typ: 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role',
};

const headers: Headers = new Headers({
'x-ms-client-principal-id': id,
'x-ms-client-principal-idp': provider,
'x-ms-client-principal': Buffer.from(JSON.stringify(claimsPrincipalData)).toString('base64'),
});

const user: HttpRequestUser | null = extractHttpUserFromHeaders(headers);

expect(user).to.not.be.null;
expect(user?.type).to.equal('AppService');
expect(user?.id).to.equal(id);
expect(user?.username).to.equal('');
expect(user?.identityProvider).to.equal(provider);
expect(user?.claimsPrincipalData).to.deep.equal(claimsPrincipalData);
});

it('Correctly parses StaticWebApps headers', () => {
const id = 'testId';
const username = 'test@example.com';
Expand Down
1 change: 1 addition & 0 deletions types/http.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export interface HttpRequestUser {

/**
* unique username
* Empty for App Service callers without a name, such as service principals
*/
username: string;

Expand Down