Skip to content

Instantly share code, notes, and snippets.

View joawan's full-sized avatar

Joakim Wånggren joawan

View GitHub Profile
@joawan
joawan / serverless.yml
Last active September 15, 2021 08:38
Serverless file, inital
service: sls-oncall
provider:
name: aws
runtime: nodejs14.x
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'eu-west-1'}
timeout: 10
memorySize: 256
logRetentionInDays: 14
@joawan
joawan / handler.js
Created August 26, 2021 14:07
Lambda code to send email
const { readFile } = require('fs').promises;
const mjml = require('mjml');
const i18n = require('i18n');
const htmlToText = require('html-to-text');
const assert = require('assert');
const mustache = require('mustache');
const AWS = require('aws-sdk');
i18n.configure({
directory: `${process.cwd()}/locales`,
@joawan
joawan / template.yaml
Created August 25, 2021 14:27
AWS SAM for SES event pipeline
EventsSNS:
Type: AWS::SNS::Topic
Properties:
TopicName: sam-pigeon-events
Subscription:
- Endpoint: !GetAtt EventsQueue.Arn
Protocol: sqs
EventsQueue:
Type: AWS::SQS::Queue
@joawan
joawan / template.yaml
Created August 25, 2021 13:33
AWS SAM for SQS to Lambda to SES
SendLambda:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/send-mail/
Description: Polls SQS, builds email, sends to SES
MemorySize: 256
Policies:
- SQSPollerPolicy:
QueueName: !GetAtt ApiQueue.QueueName
- SESCrudPolicy:
@joawan
joawan / template.yaml
Created August 25, 2021 13:12
AWS SAM for API Gateway to SQS
ApiGateway:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref Stage
DefinitionBody:
swagger: "2.0"
info:
title: !Ref AWS::StackName
x-amazon-apigateway-request-validators:
body-only:
@joawan
joawan / middleware.js
Created April 15, 2020 21:39
Token introspection as Express middleware
const tokenIntrospection = require('token-introspection');
const createError = require('http-errors');
const wrap = (fn) => (...args) => fn(...args).catch(args[2]);
const introspectMiddleware = (opts = {}) => {
const introspect = tokenIntrospection(opts);
return wrap(async (req, res, next) => {
try {
@joawan
joawan / authorizer.js
Created April 15, 2020 19:54
Token introspection as Lambda Authorizer
const tokenIntrospection = require('token-introspection');
const introspect = tokenIntrospection({
jwks_uri: process.env.JWKS_URI,
jwks_cache_time: 60 * 60,
});
const hasScope = (token, scope) => token.scope && token.scope.split(' ').includes(scope);
const generatePolicy = (principalId, effect, resource, context = {}) => ({
@joawan
joawan / init.js
Created April 15, 2020 19:42
Token introspection init
const tokenIntrospection = require('token-introspection')({
jwks_uri: 'https://example.com/jwks',
endpoint: 'https://example.com/introspect',
client_id: 'client-id',
client_secret: 'client-secret',
});
tokenIntrospection(token).then(console.log).catch(console.warn);
@joawan
joawan / token-introspection-middleware.js
Created October 10, 2019 06:11
Token introspection middleware with cache
const tokenIntrospection = require('token-introspection');
const crypto = require('crypto');
const createError = require('http-errors');
const throwIfTrue = require('throw-if-true');
const Cache = require('../lib/cache');
const wrap = require('../lib/routeWrapper');
const logger = require('../lib/logger');
const cache = new Cache();
@joawan
joawan / p1.js
Created December 10, 2017 20:08
AoC 2017, Day 1, oneliner
console.log(process.argv[2].split('').filter((e, i, a) => e === a[(i + 1) % a.length]).reduce((s, c) => s + +c, 0));