-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: handle Auth logic in the handler instead of globally (#41)
* fix: handle auth per handler * fix: conditions order in token validation * fix: code cleaness and methods improvements * fix: new api response and add missed token condition
- Loading branch information
Showing
10 changed files
with
77 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
APIGatewayProxyEvent, | ||
APIGatewayProxyHandler, | ||
APIGatewayProxyResult, | ||
Callback, | ||
Context, | ||
} from 'aws-lambda'; | ||
import { isTokenValid } from '@utils/api/apiAuth'; | ||
import { | ||
forbiddenResponse, | ||
noContentResponse, | ||
unauthorizedResponse, | ||
} from '@utils/api/apiResponses'; | ||
|
||
export function withAuthorization(handler: APIGatewayProxyHandler): APIGatewayProxyHandler { | ||
return async ( | ||
event: APIGatewayProxyEvent, | ||
context: Context, | ||
callback: Callback<APIGatewayProxyResult> | ||
): Promise<APIGatewayProxyResult> => { | ||
const authorization = event.headers?.['authorization'] || ''; | ||
|
||
if (!authorization) { | ||
return unauthorizedResponse; | ||
} | ||
|
||
if (!isTokenValid(authorization)) { | ||
return forbiddenResponse; | ||
} | ||
|
||
const result = await handler(event, context, callback); | ||
|
||
// Provide a default response if the handler returns void | ||
if (result === undefined) { | ||
return noContentResponse; | ||
} | ||
|
||
return result; | ||
}; | ||
} |