Josimar Zimermann
01/26/2022, 12:52 PMJosimar Zimermann
01/26/2022, 12:53 PMSean Matheson
01/26/2022, 1:13 PMJosimar Zimermann
01/26/2022, 1:35 PMSean Matheson
01/26/2022, 1:41 PMJosimar Zimermann
01/26/2022, 2:16 PMSean Matheson
01/26/2022, 2:20 PMSean Matheson
01/26/2022, 2:21 PMSean Matheson
01/26/2022, 2:24 PMimport * as apigAuthorizers from "@aws-cdk/aws-apigatewayv2-authorizers-alpha";
import * as sst from "@serverless-stack/resources";
// The lambda function which will handle the authorisation checks:
const authorizerHandler = new sst.Function(this, "authorizer-lambda", {
handler: "lambdas/custom-authorizer.handler",
});
// Initialises the custom lambda authoriser for use against our API Gateway:
const httpLambdaAuthorizer = new apigAuthorizers.HttpLambdaAuthorizer(
"apigw-lambda-authorizer",
authorizerHandler,
{
authorizerName: "LambdaAuthorizer",
responseTypes: [apigAuthorizers.HttpLambdaResponseType.SIMPLE],
},
);
// Declare an API Gateway V2 API:
const api = new sst.Api(this, "my-api", {
// Our custom lambda authoriser will be used as the default for functions
// that have an authorizationType configured
defaultAuthorizer: httpLambdaAuthorizer,
routes: {
"GET /private": {
// We set an authorization type on this route:
authorizationType: sst.ApiAuthorizationType.CUSTOM,
function: "api/private.handler",
},
// The following route will not be protected by our custom authorizer:
"GET /public": "api/public.handler",
},
});
Sean Matheson
01/26/2022, 2:28 PMimport jwt from "jsonwebtoken";
export const handler = async (event) => {
const [token] = event.identitySource;
if (token == null || token === "") {
<http://console.info|console.info>("No token provided. Access denied.");
return {
isAuthorized: false,
};
}
try {
jwt.verify(token, "my-token-secret");
} catch (err) {
return {
isAuthorized: false,
};
}
return {
isAuthorized: true,
};
};
Josimar Zimermann
01/26/2022, 2:37 PMthdxr
01/26/2022, 3:13 PMthdxr
01/26/2022, 3:13 PMAdam Fanello
01/26/2022, 4:09 PMimport { decodeVerifyJwt } from "./decodeVerify";
import { APIGatewayProxyEvent } from "aws-lambda";
import { HttpError } from "http-errors";
import { WebSocketSubscribeAction } from "@pkg/models";
export const handler = async (event: APIGatewayProxyEvent) => {
try {
const action = JSON.parse(event.body!) as WebSocketSubscribeAction;
const claims = await decodeVerifyJwt(action.authorization);
if (!claims.isValid) {
log.warnObject("Invalid JWT ", claims.error);
return {
statusCode: 403, // Forbidden
body: JSON.stringify(claims.error),
};
}
Adam Fanello
01/26/2022, 4:12 PMDerek Kershner
01/26/2022, 4:18 PMAdam Fanello
01/26/2022, 4:19 PM