Skip to content

Instantly share code, notes, and snippets.

@fl0wo
Created December 12, 2022 20:51
Show Gist options
  • Select an option

  • Save fl0wo/97a1c9a7c9c00181b4e4398ca557f5e2 to your computer and use it in GitHub Desktop.

Select an option

Save fl0wo/97a1c9a7c9c00181b4e4398ca557f5e2 to your computer and use it in GitHub Desktop.
Stytch token integration and side code
export const getStytchTokenFromSessionIdLambdaIntegration = (
stack: Construct,
id:string,
options:InfrastructureOptions) => {
const getToken = new NodejsFunction(stack, id, {
architecture: Architecture.ARM_64,
entry: `${__dirname}/token.ts`,
logRetention: RetentionDays.ONE_WEEK,
timeout: Duration.minutes(1),
memorySize: 256,
initialPolicy: [
new PolicyStatement({
actions: ["ssm:GetParameter","ssm:DeleteParameter"],
resources: ["*"],
}),
],
environment:{
ENV: options.environ
}
});
return getToken;
}
export const whenJwtExpiresLocal = (token: string) => {
try{
const obj = parseJwt(token);
return whenJwtExpiresMs(obj);
}catch (e){
Logger.ERROR(e);
Logger.ERROR('ERROR DURING authenticateJwtLocal WITH ' + token);
return null;
}
}
export function whenJwtExpiresMs(payload: ParsedJwtSession) {
const expAt = new Date(payload["https://stytch.com/session"].expires_at).getTime();
const now = new Date().getTime();
return expAt - now;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment