mickey phoenix
06/13/2022, 4:36 PMRoss Coundon
06/13/2022, 4:38 PMmickey phoenix
06/13/2022, 4:44 PMmickey phoenix
06/13/2022, 4:46 PMRoss Coundon
06/13/2022, 4:47 PMmickey phoenix
06/13/2022, 4:49 PMconst singleUseAuthRequestTokenAuthorizerFunction = new Function(
this,
"SingleUseAuthRequestTokenAuthorizerFunction",
{
handler: '@hbo/hbomax-oyster-lambda/lib/SingleUseAuthRequestTokenAuthorizer.handler',
environment: {
SINGLE_USE_REQUEST_AUTH_TOKEN_TABLE_NAME: props.singleUseRequestAuthTokenTable.tableName
},
permissions: [props.singleUseRequestAuthTokenTable]
}
);
const singleUseAuthRequestTokenAuthorizer: HttpLambdaAuthorizer = new HttpLambdaAuthorizer(
'SingleUseAuthRequestTokenHttpLambdaAuthorizer',
singleUseAuthRequestTokenAuthorizerFunction,
{
authorizerName: "SingleUseAuthRequestTokenAuthorizer",
responseTypes: [HttpLambdaResponseType.IAM],
identitySource: ['$request.header.Authorization'],
resultsCacheTtl: Duration.seconds(0)
}
);
// Later, in the routes...
'POST /CreateStbTesterNodeAuthToken': {
handler: '@hbo/hbomax-oyster-lambda/lib/CreateStbTesterNodeAuthToken.handler',
environment: {
SINGLE_USE_REQUEST_AUTH_TOKEN_TABLE_NAME: props.singleUseRequestAuthTokenTable.tableName,
STB_TESTER_NODE_AUTH_TOKEN_TABLE_NAME: props.stbTesterNodeAuthTokenTable.tableName
},
authorizationType: ApiAuthorizationType.CUSTOM,
authorizer: singleUseAuthRequestTokenAuthorizer
}
mickey phoenix
06/13/2022, 4:54 PMRoss Coundon
06/13/2022, 5:37 PMmickey phoenix
06/13/2022, 9:06 PMmickey phoenix
06/13/2022, 9:07 PMmickey phoenix
06/13/2022, 9:54 PMimport { Duration } from "aws-cdk-lib";
import { HttpLambdaAuthorizer } from "@aws-cdk/aws-apigatewayv2-authorizers-alpha";
const authHandler = new Function(stack, "AuthHandler", {
handler: "src/authorizer.main",
});
const authorizer = new HttpLambdaAuthorizer("MyAuthorizer", authHandler, {
authorizerName: "LambdaAuthorizer",
resultsCacheTtl: Duration.seconds(30),
});
new Api(stack, "Api", {
defaultAuthorizationType: ApiAuthorizationType.CUSTOM,
defaultAuthorizer: authorizer,
routes: {
"GET /notes": "src/list.main",
"POST /notes": {
function: "create.main",
authorizer: authorizer
authorizationType: ApiAuthorizationType.CUSTOM,
}
},
});
This appears to me to be substantially the same as my code.
I do have one question about that code, though. I don’t understand the meaning/purpose/intent/effect of the three different names (“AuthHandler”, “MyAuthorizer”, and “LambdaAuthorizer”) for the two constructs (authHandler
and authorizer
). Why does the HttpLambdaAuthorizer
take both a name as the first argument to its constructor, and also take an authorizerName
as one of its props? And why does the authorizerName
(“LambdaAuthorizer”) not appear anywhere else in the code? Is it a magic string, rather than a placeholder for a real name? And, if it is a real name, why is it needed when we have already named the HttpLambdaAuthorizer
something else (“MyAuthorizer”)?Frank
'POST /CreateStbTesterNodeAuthToken': {
handler: '...',
environment: {
...
},
authorizationType: ApiAuthorizationType.CUSTOM,
authorizer: singleUseAuthRequestTokenAuthorizer
}
with
'POST /CreateStbTesterNodeAuthToken': {
function: {
handler: '...',
environment: {
...
},
},
authorizationType: ApiAuthorizationType.CUSTOM,
authorizer: singleUseAuthRequestTokenAuthorizer
}
mickey phoenix
06/13/2022, 10:42 PMFrank
'POST /route': {
handler,
environment,
}
Or this if authorizer is provided
'POST /route': {
function: {
handler,
environment,
},
authorizationType,
authorizer
}
mickey phoenix
06/13/2022, 10:43 PMfunction
version or the not-function
version. So why does not-function
version not work in 0.x?Frank
'POST /route': {
handler,
environment,
authorizationType,
authorizer
}
mickey phoenix
06/13/2022, 10:43 PMFrank
Frank
'POST /route': {
handler,
environment,
}
You always have to do this
'POST /route': {
function: {
handler,
environment,
},
}
Frank
Frank
mickey phoenix
06/13/2022, 10:48 PMmickey phoenix
06/13/2022, 10:51 PMObject literal may only specify known properties, and ''fred'' does not exist in type 'Function | FunctionProps | ApiFunctionRouteProps | ApiHttpRouteProps | ApiAlbRouteProps'
Unless the “Object literal may only specify known properties” is less smart than it should be, and just checks the union of all known properties of the types being combined. Which would be awful, but is TypeScript’s fault and not yours. 😄mickey phoenix
06/13/2022, 10:52 PMFrank
'POST /route': {
handler,
environment,
authorizationType,
authorizer
}
mickey phoenix
06/13/2022, 11:03 PMtype Hobbit = { footHairs : number };
const frodo: Hobbit = { footHairs: 99134, palantirs: 13 }; // Object literal may only specify known properties, and 'palantirs' does not exist in type 'Hobbit'.ts(2322)
type Wizard = { palantirs : number };
const gandalf: Wizard = { footHairs: 13, palantirs: 1 }; // Object literal may only specify known properties, and 'footHairs' does not exist in type 'Wizard'.ts(2322)
type DenizenOfMiddleEarth = Hobbit | Wizard;
const HobbitsCannotBeMaiar: DenizenOfMiddleEarth = { footHairs: 99999, palantirs: 12345}; // TypeScript permits an abomination in the eyes of Eru Ilúvatar!!!
mickey phoenix
06/13/2022, 11:07 PMauthorizationType
and authorization
in the FunctionDefinition
branch of the routes?
It kinda feels like a booby trap, the way it is right now… /wry grinmickey phoenix
06/13/2022, 11:13 PM'POST /CreateStbTesterNodeAuthToken': {
function: '@hbo/hbomax-oyster-lambda/lib/CreateStbTesterNodeAuthToken.handler',
environment: {
SINGLE_USE_REQUEST_AUTH_TOKEN_TABLE_NAME: props.singleUseRequestAuthTokenTable.tableName,
STB_TESTER_NODE_AUTH_TOKEN_TABLE_NAME: props.stbTesterNodeAuthTokenTable.tableName
},
authorizer: singleUseAuthRequestTokenAuthorizer
}
Seems to me that it would, given that it would still end up matching the definition of ApiFunctionRouteProps
. But I want to make sure that the code that processes it is written such that it handles it correctly.Frank
environment
needs to go into function
, so this would work:
'POST /CreateStbTesterNodeAuthToken': {
function: {
handler: '@hbo/hbomax-oyster-lambda/lib/CreateStbTesterNodeAuthToken.handler',
environment: {
SINGLE_USE_REQUEST_AUTH_TOKEN_TABLE_NAME: props.singleUseRequestAuthTokenTable.tableName,
STB_TESTER_NODE_AUTH_TOKEN_TABLE_NAME: props.stbTesterNodeAuthTokenTable.tableName
},
},
authorizer: singleUseAuthRequestTokenAuthorizer
}
Frank
Frank
function checkRoutes(routes) {
if (routes.handler && routes.authorizer) {
throw new Error("Move handler inside function");
}
if (routes.function && routes.environment) {
throw new Error("Move environment inside function");
}
// add more checks
}
Frank
const routes = {
'POST /CreateStbTesterNodeAuthToken': {
...
},
'POST /anotherRoute': {
...
},
};
checkRoutes(routes);
new Api(this, "MyAPI", {
routes
});
Frank