1- import { APIGatewayAuthorizerResult , APIGatewayEventClientCertificate , APIGatewayRequestAuthorizerEvent , APIGatewayRequestAuthorizerEventHeaders , APIGatewayRequestAuthorizerHandler ,
2- Callback , Context } from 'aws-lambda' ;
1+ import {
2+ APIGatewayAuthorizerResult , APIGatewayEventClientCertificate , APIGatewayRequestAuthorizerEvent , APIGatewayRequestAuthorizerEventHeaders , APIGatewayRequestAuthorizerHandler ,
3+ Callback , Context
4+ } from 'aws-lambda' ;
35import { Deps } from './deps' ;
46import { Supplier } from '@internal/datastore' ;
57
68export function createAuthorizerHandler ( deps : Deps ) : APIGatewayRequestAuthorizerHandler {
79
8- return (
10+ return (
911 event : APIGatewayRequestAuthorizerEvent ,
1012 context : Context ,
1113 callback : Callback < APIGatewayAuthorizerResult >
@@ -29,41 +31,41 @@ export function createAuthorizerHandler(deps: Deps): APIGatewayRequestAuthorizer
2931
3032async function getSupplier ( headers : APIGatewayRequestAuthorizerEventHeaders | null , deps : Deps ) : Promise < Supplier > {
3133 const apimId = Object . entries ( headers || { } )
32- . find ( ( [ headerName , _ ] ) => headerName . toLowerCase ( ) === deps . env . APIM_APPLICATION_ID_HEADER . toLowerCase ( ) ) ?. [ 1 ] as string ;
34+ . find ( ( [ headerName , _ ] ) => headerName . toLowerCase ( ) === deps . env . APIM_SUPPLIER_ID_HEADER . toLowerCase ( ) ) ?. [ 1 ] as string ;
3335
34- if ( ! apimId ) {
36+ if ( ! apimId ) {
3537 throw new Error ( 'No APIM application ID found in header' ) ;
3638 }
3739 const supplier = await deps . supplierRepo . getSupplierByApimId ( apimId ) ;
3840 if ( supplier . status === 'DISABLED' ) {
39- throw new Error ( `Supplier ${ supplier . id } is disabled` ) ;
41+ throw new Error ( `Supplier ${ supplier . id } is disabled` ) ;
4042 }
4143 return supplier ;
4244}
4345
4446
45- // Helper function to generate an IAM policy
46- function generatePolicy (
47- principalId : string ,
48- effect : 'Allow' | 'Deny' ,
49- resource : string
50- ) : APIGatewayAuthorizerResult {
51- // Required output:
52- const authResponse : APIGatewayAuthorizerResult = {
53- principalId,
54- policyDocument : {
55- Version : '2012-10-17' ,
56- Statement : [
57- {
58- Action : 'execute-api:Invoke' ,
59- Effect : effect ,
60- Resource : resource ,
61- } ,
62- ] ,
63- } ,
64- } ;
65- return authResponse ;
66- }
47+ // Helper function to generate an IAM policy
48+ function generatePolicy (
49+ principalId : string ,
50+ effect : 'Allow' | 'Deny' ,
51+ resource : string
52+ ) : APIGatewayAuthorizerResult {
53+ // Required output:
54+ const authResponse : APIGatewayAuthorizerResult = {
55+ principalId,
56+ policyDocument : {
57+ Version : '2012-10-17' ,
58+ Statement : [
59+ {
60+ Action : 'execute-api:Invoke' ,
61+ Effect : effect ,
62+ Resource : resource ,
63+ } ,
64+ ] ,
65+ } ,
66+ } ;
67+ return authResponse ;
68+ }
6769
6870function generateAllow ( resource : string , supplierId : string ) : APIGatewayAuthorizerResult {
6971 return generatePolicy ( supplierId , 'Allow' , resource ) ;
@@ -76,7 +78,7 @@ function generateDeny(resource: string): APIGatewayAuthorizerResult {
7678function getCertificateExpiryInDays ( certificate : APIGatewayEventClientCertificate ) : number {
7779 const now = new Date ( ) . getTime ( ) ;
7880 const expiry = new Date ( certificate . validity . notAfter ) . getTime ( ) ;
79- return ( expiry - now ) / ( 1000 * 60 * 60 * 24 ) ;
81+ return ( expiry - now ) / ( 1000 * 60 * 60 * 24 ) ;
8082}
8183
8284async function checkCertificateExpiry ( certificate : APIGatewayEventClientCertificate | null , deps : Deps ) : Promise < void > {
@@ -117,7 +119,7 @@ async function checkCertificateExpiry(certificate: APIGatewayEventClientCertific
117119 ] ,
118120 } ,
119121 'SUBJECT_DN' : certificate . subjectDN ,
120- 'NOT_AFTER' : certificate . validity . notAfter ,
122+ 'NOT_AFTER' : certificate . validity . notAfter ,
121123 'apim-client-certificate-near-expiry' : 1 ,
122124 } ;
123125 }
0 commit comments