Skip to content

Instantly share code, notes, and snippets.

View ArthurKnoep's full-sized avatar

Arthur Knoepflin ArthurKnoep

View GitHub Profile
@ArthurKnoep
ArthurKnoep / jwt.js
Created May 20, 2022 12:28
Authenticating with JSON Web Tokens (JWT)
'use strict';
var jwt = require('jsonwebtoken');
var jwkToPem = require('jwk-to-pem');
var USERPOOLID = '##USERPOOLID##';
var JWKS = '##JWKS##';
var COGNITOREGION = '##COGNITOREGION##';
var iss = 'https://cognito-idp.' + COGNITOREGION + '.amazonaws.com/' + USERPOOLID;
var pems;
@ArthurKnoep
ArthurKnoep / redirect-based-on-latency.py
Created May 20, 2022 12:22
Redirecting Request by Country or Latency
# Need to install DNS package
import dns.resolver
def lambda_handler(event, context):
 request = event['Records'][0]['cf']['request']
 res = dns.resolver.query("latency-cross-region.example.com", "txt")
 buff = res[0].to_text().replace('"', '').split(";")
 bucket_name = buff[0]
 region = buff[1]
 domainName = f"{bucket_name}.s3.{region}.amazonaws.com"
 request['origin']['s3']['domainName'] = domainName
@ArthurKnoep
ArthurKnoep / cors.js
Created May 20, 2022 12:12
Managing CORS, CSP, X-Frame-Options, and other Security HTTP Headers
function handler(event) {
var response = event.response;
var headers = response.headers;
headers['strict-transport-security'] = { value: 'max-age=63072000; includeSubdomains; preload'};
headers['content-security-policy'] = { value: "default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'"};
headers['x-content-type-options'] = { value: 'nosniff'};
headers['x-frame-options'] = {value: 'DENY'};
headers['x-xss-protection'] = {value: '1; mode=block'};
headers['access-control-allow-origin'] = {value: "*"};
@ArthurKnoep
ArthurKnoep / control-cache-key.js
Created May 20, 2022 10:33
Modifying the CloudFront Cache Key
const crypto = require('crypto');
function hashText(input) {
return crypto.createHash('md5').update(input).digest('hex');
}
function handler(event) {
var request = event.request;
var headers = request.headers;
@ArthurKnoep
ArthurKnoep / redirect-based-on-conditions.js
Last active May 20, 2022 10:34
Redirecting Traffic Based on Simple Conditions
function handler(event) {
var request = event.request;
var supportedCountries = ['de', 'it', 'fr'];
if (request.uri.substr(3, 1) != '/') {
var headers = request.headers;
var nextUri;
if (headers['cloudfront-viewer-country']) {
var countryCode = headers['cloudfront-viewer-country'].value.toLowerCase();
### Keybase proof
I hereby claim:
* I am arthurknoep on github.
* I am needlex (https://keybase.io/needlex) on keybase.
* I have a public key ASBfryd_koswiTQB6Ul15YFKIDfS1VtPQ0ueUrJKAhh14go
To claim this, I am signing this object: