Skip to content

Instantly share code, notes, and snippets.

View loginov-rocks's full-sized avatar

Danila Loginov loginov-rocks

View GitHub Profile
@loginov-rocks
loginov-rocks / generatePolicy.js
Created January 22, 2024 04:08
WebSocket API Gateway Cognito Authorizer
/**
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html
*/
const generatePolicy = (principalId, effect, resource, context) => ({
context,
policyDocument: {
Version: '2012-10-17',
Statement: [
{
Action: 'execute-api:Invoke',
@loginov-rocks
loginov-rocks / verifyToken.js
Created January 22, 2024 04:05
WebSocket API Gateway Cognito Authorizer
import jwkToPem from 'jwk-to-pem';
// Simple in-memory cache, optional. In this Lambda it will always have a single promise stored because Region and User
// Pool ID are taken from the environment variables and remain unchanged within the life cycle of the Lambda, but I
// still kept this flexible in case someone will copy the code. Subject for improvement.
const getPemEncodedPublicKeysPromisesCache = new Map();
const getPemEncodedPublicKeysImplementation = async (jwksUrl) => {
console.log('jwksUrl', jwksUrl);
@loginov-rocks
loginov-rocks / verifyToken.js
Last active January 22, 2024 04:00
WebSocket API Gateway Cognito Authorizer
import { decode, verify } from 'jsonwebtoken';
export const verifyToken = async (region, userPoolId, token) => {
var decodedJwt = decode(token, { complete: true });
if (!decodedJwt || !decodedJwt.header || !decodedJwt.header.kid) {
return null;
}
const pemEncodedPublicKeys = await getPemEncodedPublicKeys(region, userPoolId);
@loginov-rocks
loginov-rocks / sign.mjs
Last active January 22, 2024 03:59
WebSocket API Gateway IAM Signer
import { defaultProvider } from '@aws-sdk/credential-provider-node';
import { Hash } from '@aws-sdk/hash-node';
import { HttpRequest } from '@aws-sdk/protocol-http';
import { SignatureV4 } from '@aws-sdk/signature-v4';
import { URL } from 'url';
export const sign = async (region, url, query = {}, headers = {}) => {
const urlObject = new URL(url);
// Add query parameters passed as argument, if any.
@loginov-rocks
loginov-rocks / platformio.ini
Created January 9, 2022 17:55
DIY Connected Espresso Machine: Over-the-Air Updates (Part 6) - platformio.ini
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
monitor_filters = esp8266_exception_decoder
upload_protocol = espota
upload_port = connected-espresso-machine.local
lib_deps =
@loginov-rocks
loginov-rocks / main.cpp
Last active January 13, 2022 17:23
DIY Connected Espresso Machine: Over-the-Air Updates (Part 6) - main.cpp
#include "ArduinoOTA.h"
#include "EspressoMachine.h"
#include "WiFiManager.h"
#define SERIAL_BAUDRATE 9600
#define WIFI_HOSTNAME "Connected-Espresso-Machine"
#define WIFI_ACCESS_POINT_SSID "Connected-Espresso-Machine"
#define OTA_UPDATES_HOSTNAME "connected-espresso-machine"
@loginov-rocks
loginov-rocks / main.cpp
Last active January 13, 2022 17:18
DIY Connected Espresso Machine: Over-the-Air Updates (Part 6) - setupOtaUpdates
#include "ArduinoOTA.h"
// ...
#define OTA_UPDATES_HOSTNAME "connected-espresso-machine"
// ...
/**
* @see https://github.com/esp8266/Arduino/blob/master/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino
@loginov-rocks
loginov-rocks / main.cpp
Last active January 13, 2022 17:18
DIY Connected Espresso Machine: Over-the-Air Updates (Part 6) - setupWiFi
#include "WiFiManager.h"
// ...
#define WIFI_HOSTNAME "Connected-Espresso-Machine"
#define WIFI_ACCESS_POINT_SSID "Connected-Espresso-Machine"
// ...
/**
@loginov-rocks
loginov-rocks / Dockerfile
Last active December 26, 2023 06:53
How to build and package Node.js Lambda code along with dependencies in Docker
# BASE
FROM amazonlinux:2023.3.20231218.0 AS base
RUN yum -y install gzip tar zip
RUN touch ~/.bashrc
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
RUN source ~/.bashrc && nvm install 18.9.0
# BUILD
FROM base AS build
@loginov-rocks
loginov-rocks / main.cpp
Created January 8, 2022 21:09
DIY Connected Espresso Machine: Main Class and Indicators (Part 5) - main.cpp
#include "EspressoMachine.h"
#define SERIAL_BAUDRATE 9600
#define PUMP_RELAY_PIN D1
#define BOILER_RELAY_PIN D2
#define BOILER_IS_BOILING_PIN D5
#define BOILER_IS_STEAM_PIN D6
#define TOGGLE_PIN A0
#define DONE_PIN D7