Skip to content

Commit

Permalink
Merge pull request #56 from Cimpress-MCP/akincel/fix-canonical-id-pri…
Browse files Browse the repository at this point in the history
…ority

Release 5.3.1
  • Loading branch information
akincel committed Oct 25, 2023
2 parents 53a7427 + 2879946 commit 3bbdbd4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

## [5.3.1] - 2023-10-25

### Fixed

The `getUserToken()` and `getUserPrincipal()` order was wrongly set in version `5.3.0`. The new fixed
priority order:

`getUserToken()`

1. `request.authorizerContext.jwt`
2. `request.authorizerContext.accessToken` (new)
3. `request.headers.Authorization`

`getUserPrincipal()`

1. `authorizerContext.canonicalId` (**prefer canonicalId**)
2. `authorizerContext.principalId` (new)
3. `request.headers.Authorization`

## [5.3.0] - 2023-09-07

### Changed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lambda-essentials-ts",
"version": "5.3.0",
"version": "5.3.1",
"description": "A selection of the finest modules supporting authorization, API routing, error handling, logging and sending HTTP requests.",
"main": "lib/index.js",
"private": false,
Expand Down
4 changes: 2 additions & 2 deletions src/openApi/openApiWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ export default class OpenApiWrapper {
} {
if (authorizerContext) {
return {
userPrincipal: authorizerContext.principalId ?? authorizerContext.canonicalId,
userToken: authorizerContext.accessToken ?? authorizerContext.jwt,
userPrincipal: authorizerContext.canonicalId ?? authorizerContext.principalId,
userToken: authorizerContext.jwt ?? authorizerContext.accessToken,
};
}

Expand Down
20 changes: 20 additions & 0 deletions tests/openApi/openApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ describe('Open API Wrapper', () => {
requestId: 'tests-request-id',
},
};
const requestWithOldAndNewStyleAuthorizer: ApiRequest<any> = {
...request,
requestContext: {
authorizer: {
canonicalId,
principalId,
jwt,
accessToken,
},
requestId: 'tests-request-id',
},
};
const testJwt =
'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaHR0cHM6Ly9jbGFpbXMuY2ltcHJlc3MuaW8vY2Fub25pY2FsX2lkIjoiam9obkBkb2Uub3JnIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.XNjjaJDz4g8AecLBIDZY6aDwANCNMKg2NrcNxaJ-0JaqoGm0fBGPCZfbtGuf4-8DVqnwmrWslt7tMEj8QIU_TL1cWsX83ZGggM4crGva8tLw54Vhg5BrNWCOBiMphxGzU-5DbXPWvtnWatJgDdBuRSegZK5slpa8DnmXiMNkXxZhyulTbZYkArE2e16NFZhVANWmR3A4K_0ETF-s3uARvua9rPOxkaaxHPIkoZ58CsuD1p6pqi8KDthiW0OCry6o2uPIG-MfyP0gKDPD88XtVD5pcr6WWhNv37ZnucG75wuxE8c6eMj_pPCrt_eoM8ygUc9GY7XoLmZZAvI-szlivw';
const requestWithAuthorizationHeader: ApiRequest<any> = {
Expand Down Expand Up @@ -105,6 +117,14 @@ describe('Open API Wrapper', () => {
expect(openApi.getUserPrincipal()).toEqual(canonicalId);
});

test('sets userToken and userPrincipal from old-style Authorizer with priority', async () => {
const openApi = new OpenApiWrapper(new LoggerMock());
await openApi.api.requestMiddleware(requestWithOldAndNewStyleAuthorizer);

expect(openApi.getUserToken()).toEqual(jwt);
expect(openApi.getUserPrincipal()).toEqual(canonicalId);
});

test('sets userToken and userPrincipal from Authorization header', async () => {
const openApi = new OpenApiWrapper(new LoggerMock());
await openApi.api.requestMiddleware(requestWithAuthorizationHeader);
Expand Down

0 comments on commit 3bbdbd4

Please sign in to comment.