diff --git a/example/request.http b/example/request.http index 4c4306a..19da098 100644 --- a/example/request.http +++ b/example/request.http @@ -1 +1,15 @@ +### Call function test-header-authentication-function POST http://localhost:8080/api/authentication +x-ms-client-principal-id: 42 + +### Call function test-jwt-authorization-function +POST http://localhost:8080/api/authorization/user-id +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyLWlkIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.UtSlzAkI3g8amFZFZcsismCy0f_MZD7lOQ5zgJn5FJU + +### Call function test-validation-function +POST http://localhost:8080/api/validation +Content-Type: application/json + +{ + "name": "John Doe" +} \ No newline at end of file diff --git a/example/src/test-jwt-authorization-function.ts b/example/src/test-jwt-authorization-function.ts index 5576409..b8eb553 100644 --- a/example/src/test-jwt-authorization-function.ts +++ b/example/src/test-jwt-authorization-function.ts @@ -3,8 +3,16 @@ import { HttpHandler, HttpRequestParams, app } from '@azure/functions'; import { middleware } from '../../src'; import authorization from '../../src/jwtAuthorization'; +type JwtClaims = { + sub: string; + name: string; +}; + export const handler: HttpHandler = async (req, context) => { - context.log('Function called'); + const jwtClaims = context.extraInputs.get('jwt') as JwtClaims; + + context.log(`Function called by ${JSON.stringify(jwtClaims)}`); + return { status: 204 }; }; @@ -17,7 +25,7 @@ app.http('test-jwt-authorization-function', { authorization([ { parameterExtractor: (parameters: HttpRequestParams) => parameters.id, - jwtExtractor: (jwt: { userId: string }) => jwt.userId, + jwtExtractor: (jwt: { sub: string }) => jwt.sub, }, ]), ], diff --git a/integration-test/jwt-authorization-test.integration.ts b/integration-test/jwt-authorization-test.integration.ts index 44bea48..4d41329 100644 --- a/integration-test/jwt-authorization-test.integration.ts +++ b/integration-test/jwt-authorization-test.integration.ts @@ -2,9 +2,9 @@ import axios from 'axios'; import waitTillFunctionReady from './waitTillFunctionReady'; -// Token generated with https://jwt.io/ containing the "userId" "c8e65ca7-a008-4b1c-b52a-4ad0ee417017" +// Token generated with https://jwt.io/ containing the "sub" "c8e65ca7-a008-4b1c-b52a-4ad0ee417017" const sampleToken = - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjOGU2NWNhNy1hMDA4LTRiMWMtYjUyYS00YWQwZWU0MTcwMTcifQ.EvY_4nO-uZrc2mNY9E-RvLb5CI-q5tzv1pwBhFaUWgs'; + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjOGU2NWNhNy1hMDA4LTRiMWMtYjUyYS00YWQwZWU0MTcwMTciLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE1MTYyMzkwMjJ9.TYD4sZM42CWiIPlKm4n2uesaGHRi_priukmc4xSn0K0'; describe('The example azure function is started and the JWT authorization should execute the request', () => { beforeAll(async () => {