Jose
04/21/2022, 2:17 PMexport default class MyStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);
// Create an HTTP API
const api = new sst.Api(this, "Api", {
routes: {
"GET /": "src/lambda.handler",
},
});
// Show the endpoint in the output
this.addOutputs({
"ApiEndpoint": api.url,
});
}
}
Is it possible to implement an api key to this api in the latest version? Thanks!Ross Coundon
04/21/2022, 2:18 PMDerek Kershner
04/21/2022, 2:18 PMUsage Plan
feature of REST API Gateways, you are u…thanks Ross.Ross Coundon
04/21/2022, 2:19 PMDerek Kershner
04/21/2022, 2:19 PMDerek Kershner
04/21/2022, 2:20 PMJose
04/21/2022, 2:25 PMSeth Geoghegan
04/21/2022, 2:29 PMDerek Kershner
04/21/2022, 2:32 PMRoss Coundon
04/21/2022, 2:32 PMDerek Kershner
04/21/2022, 2:34 PMDerek Kershner
04/21/2022, 2:35 PMRoss Coundon
04/21/2022, 2:36 PMRoss Coundon
04/21/2022, 2:36 PMDerek Kershner
04/21/2022, 2:36 PMRoss Coundon
04/21/2022, 2:36 PMDerek Kershner
04/21/2022, 2:37 PMLukasz K
04/21/2022, 2:37 PMLukasz K
04/21/2022, 2:37 PMLukasz K
04/21/2022, 2:37 PMDerek Kershner
04/21/2022, 2:38 PMLukasz K
04/21/2022, 2:39 PMLukasz K
04/21/2022, 2:40 PMRoss Coundon
04/21/2022, 2:40 PMLukasz K
04/21/2022, 2:41 PMDerek Kershner
04/21/2022, 2:41 PMLukasz K
04/21/2022, 2:41 PMDerek Kershner
04/21/2022, 2:42 PMDerek Kershner
04/21/2022, 2:44 PMDerek Kershner
04/21/2022, 2:48 PMRoss Coundon
04/21/2022, 2:49 PMDerek Kershner
04/21/2022, 3:07 PMFrank
having a lambda called to authenticate each requestSetting
resultsCacheTtl
cache should help? https://docs.serverless-stack.com/constructs/v1/Api#resultscachettlRoss Coundon
04/22/2022, 9:03 AMRoss Coundon
04/22/2022, 9:05 AMAPIGatewayTokenAuthorizerEvent
it seems it only receives the authorizationToken and not the headers.
If not, are you doing the header checks in the actual lambda using some middleware (or as a first check)?Ross Coundon
04/22/2022, 9:13 AMAPIGatewayRequestAuthorizerEventV2
via handler type APIGatewayRequestSimpleAuthorizerHandlerV2
?Lukasz K
04/22/2022, 9:20 AMconst boaAuthorizer = new HttpLambdaAuthorizer("boaTokenVerifier", boaAuthVerifier,
{
resultsCacheTtl: Duration.hours(1),
responseTypes: [HttpLambdaResponseType.SIMPLE],
authorizerName: 'boaTokenVerifier',
identitySource: ['$request.header.authToken']
}
);
APi def:
defaultAuthorizationType: ApiAuthorizationType.CUSTOM,
defaultAuthorizer: boaAuthorizer,
with the actual function being (shorthand):
export async function verifyToken(event): Promise<{isAuthorized: boolean, context: unknown}> {
const response = {
"isAuthorized": false,
"context": {}
};
try {
if (!tokenId) {
<grab actual token from secrets>
}
response.isAuthorized = tokenId === event.headers.authToken;
return response;
} catch (e) {
console.log(e);
return response;
}
Lukasz K
04/22/2022, 9:20 AMRoss Coundon
04/22/2022, 9:23 AMRoss Coundon
04/22/2022, 9:30 AMverifyToken(event)
or what interface is the verifyToken
function implementing?Lukasz K
04/22/2022, 9:32 AMRoss Coundon
04/22/2022, 9:33 AMLukasz K
04/22/2022, 9:57 AMLukasz K
04/22/2022, 10:01 AMLukasz K
04/22/2022, 10:03 AMRoss Coundon
04/22/2022, 10:04 AMDerek Kershner
04/22/2022, 1:40 PMRoss Coundon
04/22/2022, 3:38 PMJose
04/22/2022, 7:31 PMHttpLambdaAuthorizer
this way:
import * as sst from "@serverless-stack/resources";
import * as authorizers from "@aws-cdk/aws-apigatewayv2-authorizers";
export default class MyStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);
const authorizer = new authorizers.HttpLambdaAuthorizer({
authorizerName: "LambdaAuthorizer",
identitySource: ["$request.header.API-Key"],
responseTypes: [authorizers.HttpLambdaResponseType.SIMPLE],
handler: new sst.Function(this, "Authorizer", {
handler: "src/authorizer.main",
}),
});
// Create an HTTP API
const api = new sst.Api(this, "Api", {
defaultAuthorizationType: sst.ApiAuthorizationType.CUSTOM,
defaultAuthorizer: authorizer,
routes: {
"GET /": "src/lambda.handler",
},
});
// Show the endpoint in the output
this.addOutputs({
ApiEndpoint: api.url,
});
}
}
and a simple authorizer lambda:
export const main = async (event) => {
const key = event.headers['api-key'];
return {
isAuthorized: key === process.env.API_KEY,
};
};
Not sure if it's acceptable, it works and fits that I was looking for, some straightforward way to protect my endpoints for a demo project
Thanks for all your commentsDerek Kershner
04/22/2022, 8:36 PMDerek Kershner
04/22/2022, 8:37 PMRoss Coundon
04/22/2022, 8:40 PMDerek Kershner
04/22/2022, 8:41 PMDerek Kershner
04/22/2022, 8:41 PMRoss Coundon
04/22/2022, 8:46 PMRoss Coundon
04/22/2022, 8:47 PMDerek Kershner
04/22/2022, 8:48 PMRoss Coundon
04/22/2022, 8:48 PMDerek Kershner
04/22/2022, 8:49 PMDerek Kershner
04/22/2022, 8:50 PMRoss Coundon
04/22/2022, 8:52 PMDerek Kershner
04/22/2022, 8:53 PMRoss Coundon
04/22/2022, 8:54 PMLukasz K
04/23/2022, 7:20 AMLukasz K
04/23/2022, 7:22 AMRoss Coundon
04/23/2022, 7:25 AMLukasz K
05/11/2022, 11:05 AMRoss Coundon
05/11/2022, 11:05 AMRoss Coundon
05/11/2022, 11:06 AM