Skip to content

Instantly share code, notes, and snippets.

View bcnzer's full-sized avatar

Ben Chartrand bcnzer

View GitHub Profile
@bcnzer
bcnzer / workerEdgedGets.js
Last active November 5, 2018 09:28
Sample Worker and KV code to demonstrate GETs that are purely handled on the edge - both the request AND data
// NOTE: all auth code has been removed for the sake of brevity. Please see part 2 of blog series
// for more info: https://liftcodeplay.com/2018/10/16/pushing-my-api-to-the-edge-part-2-authentication-and-authorization/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
const genderFemale = 'Female'
const genderMale = 'Male'
@bcnzer
bcnzer / ipf.json
Created November 2, 2018 18:30
IPF (International Powerlifting Federation) divisions and weight classes, for both men and women
{
"divisions": [
{
"gender": "male",
"startAge": 14,
"endAge": 18,
"dateNumber": 1,
"dateMonth": 1,
"name": "Sub-Junior",
"abbrev": "SJ"
@bcnzer
bcnzer / worker.js
Created October 15, 2018 09:31
Cloudflare Worker that uses Workers KV to get Authorization data. All authentication and authorization is done on the edge
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
/**
* Entry point of the worker
*/
async function handleRequest(event) {
try {
// Get the JWT
@bcnzer
bcnzer / sampleFunctions.cs
Created October 15, 2018 02:47
Cut down version of a Azure Functions HTTP trigger
namespace EventMgr.Functions.Events
{
[DependencyInjectionConfig(typeof(DIConfig))]
public static class Attendee
{
[FunctionName("Attendee")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "delete", Route = "attendee")]
HttpRequest request, ILogger log, ExecutionContext context,
[Inject] IAttendeeRepository attendeeRepository,
@bcnzer
bcnzer / samplePermissions.json
Created October 14, 2018 19:20
Sample authorization data from Auth0. This was stored in the user's app_metadata
{
"EventPermissions": [
{
"EventId": 6002,
"Role": "Admin",
"AllowedPermissions": [
"All"
]
},
{
@bcnzer
bcnzer / cloudflareWorkerExample.js
Created October 10, 2018 07:50
Example of a worker adding a header value and returning a response in a specific condition
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Fetch and log a request
* @param {Request} request
*/
async function handleRequest(request) {
var currentDateTime = new Date()
@bcnzer
bcnzer / cloudflareworker-verifyjwt.js
Last active March 21, 2024 14:09
Sample Cloudflare worker that gets the JWT, ensures it hasn't expired, decrypts it and returns a result
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
// Following code is a modified version of that found at https://blog.cloudflare.com/dronedeploy-and-cloudflare-workers/
/**
* Fetch and log a request
* @param {Request} request
*/
@bcnzer
bcnzer / deploy.ps1
Created September 14, 2018 09:35
Script used to deploy a Cloudflare worker using the Serverless Framework via Azure DevOps. It rusn the deploy command and then hunts and destroys the dummy route I had to create
# Needed to talk to Cloudflare and by default Powershell uses a now-obsolete version of TLS (1.0?)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# We need these environment variables for the serverless step
[Environment]::SetEnvironmentVariable("CLOUDFLARE_AUTH_KEY", $args[0], "Process") # API key - get it from key vault
[Environment]::SetEnvironmentVariable("CLOUDFLARE_AUTH_EMAIL", $args[1], "Process") # Cloudflare email address for the API key - get it from key vault
# https://github.com/serverless/serverless
Write-Host "Deploying YAML using serverless NPM tool"
serverless deploy
@bcnzer
bcnzer / local.settings.json
Created September 10, 2018 10:31
Example local.settings.json file for an Azure Functions project which specifies the CORS setting locally, for debug/dev
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"ConnectionStrings": {
"MyDb": "<my connection string>"
},
@bcnzer
bcnzer / deletecontent.ps1
Created August 31, 2018 08:31
Script to delete all the content in a blob storage container
$Context = New-AzureStorageContext -StorageAccountName $(BlobServiceName) -StorageAccountKey $(WolfTrackerBlobKey)
$blobs = Get-AzureStorageBlob -Context $Context -Container $(WolfTrackerBlobContainer)
foreach ($blob in $blobs)
{
Write-Host ("Removing Blob: {0}" -f $blob.Name)
Remove-AzureStorageBlob -ICloudBlob $blob.ICloudBlob -Context $Context
}