Skip to content

Commit 7b83a90

Browse files
committed
Initial commit
1 parent 5df0a1c commit 7b83a90

File tree

10 files changed

+14797
-0
lines changed

10 files changed

+14797
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
**NOTICE TO CONTRIBUTORS**
2+
3+
This repository is not actively monitored and any pull requests made to this repository will be closed/ignored.
4+
5+
Please submit the pull request to [edgio-docs/edgio-examples](https://github.com/edgio-docs/edgio-examples) instead.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Deploy to Edgio
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
7+
jobs:
8+
deploy-to-edgio:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: actions/setup-node@v3
13+
with:
14+
node-version: 16
15+
- run: if [ -f yarn.lock ]; then yarn install; else npm ci; fi
16+
- run: if [ -f yarn.lock ]; then yarn edgio:deploy -- --token=$EDGIO_DEPLOY_TOKEN; else npm run edgio:deploy -- --token=$EDGIO_DEPLOY_TOKEN; fi
17+
env:
18+
EDGIO_DEPLOY_TOKEN: ${{secrets.EDGIO_DEPLOY_TOKEN}}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Edgio generated build directory
2+
/.edgio
3+
4+
/node_modules
5+
.env
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Buffer } from 'buffer'
2+
import * as Base64 from 'crypto-js/enc-base64url'
3+
import { HmacSHA256, HmacSHA384, HmacSHA512 } from 'crypto-js'
4+
5+
const base64decode = (str) => Buffer.from(str, 'base64').toString()
6+
7+
const hashLibraries = {
8+
HS256: HmacSHA256,
9+
HS384: HmacSHA384,
10+
HS512: HmacSHA512,
11+
}
12+
13+
export class JWT {
14+
// JWT validation process:
15+
// 1. Split the token by '.' to get the header (json), payload (json), and signature (string).
16+
// 2. Calculate a signature using the algorithm in the header (hardcoded here) to join the header and payload with a
17+
// '.', and hash it using a secret value
18+
// 3. Compare the calculated signature with the one from the token. If they match, the token is valid. If not, the
19+
// token has been tampered with.
20+
21+
constructor(token, secret) {
22+
const [ header_base64, payload_base64, origSignature ] = token.split('.')
23+
24+
this.header_base64 = header_base64
25+
this.payload_base64 = payload_base64
26+
27+
this.header = JSON.parse(base64decode(header_base64))
28+
this.payload = JSON.parse(base64decode(payload_base64))
29+
30+
this.origSignature = origSignature
31+
32+
this.hasher = hashLibraries[this.header.alg]
33+
this.secret = secret
34+
}
35+
36+
validate() {
37+
console.log(`validating token using ${this.header.alg} algorithm.`)
38+
const calculatedSignature = Base64.stringify(
39+
this.hasher(
40+
`${this.header_base64}.${this.payload_base64}`,
41+
this.secret
42+
)
43+
)
44+
return calculatedSignature === this.origSignature
45+
}
46+
47+
payloadObject() {
48+
return this.payload
49+
}
50+
51+
algUsed() {
52+
return this.header.alg
53+
}
54+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { JWT } from './JWT.js'
2+
3+
export async function handleHttpRequest(request, context) {
4+
const token = await request.text()
5+
const secret = context.environmentVars['JWT_SECRET'] || ''
6+
const resp = {
7+
valid: false
8+
}
9+
10+
const jwt = new JWT(token, secret)
11+
const isValid = jwt.validate()
12+
if (isValid) {
13+
resp.valid = true
14+
resp.payload = jwt.payloadObject()
15+
resp.alg = jwt.algUsed()
16+
}
17+
18+
return new Response(JSON.stringify(resp), {
19+
status: isValid ? 200 : 403
20+
})
21+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// This file was automatically added by edgio init.
2+
// You should commit this file to source control.
3+
// Learn more about this file at https://docs.edg.io/guides/edgio_config
4+
module.exports = {
5+
// The name of the site in Edgio to which this app should be deployed.
6+
name: "ef-jwt-validate",
7+
8+
// The name of the organization in Edgio to which this app should be deployed.
9+
// organization: 'my-organization-name',
10+
11+
// Overrides the default path to the routes file. The path should be relative to the root of your app.
12+
// routes: 'routes.js',
13+
14+
// When set to true or omitted entirely, Edgio includes the deployment number in the cache key,
15+
// effectively purging the cache each time you deploy.
16+
purgeCacheOnDeploy: true,
17+
// purgeCacheOnDeploy: false,
18+
19+
origins: [
20+
{
21+
// The name of the backend origin
22+
name: "origin",
23+
24+
// Use the following to override the host header sent from the browser when connecting to the origin
25+
override_host_header: "httpbin.org",
26+
27+
// The list of origin hosts to which to connect
28+
hosts: [
29+
{
30+
// The domain name or IP address of the origin server
31+
location: "httpbin.org",
32+
},
33+
],
34+
35+
tls_verify: {
36+
use_sni: true,
37+
sni_hint_and_strict_san_check: "httpbin.org",
38+
},
39+
40+
// Uncomment the following to configure a shield
41+
// shields: { us_east: 'DCD' },
42+
},
43+
],
44+
45+
// Uncomment the following to specify environment specific configs
46+
// environments: {
47+
// production: {
48+
// hostnames: [{ hostname: 'www.mysite.com' }],
49+
// },
50+
// staging: {
51+
// hostnames: [{ hostname: 'staging.mysite.com' }],
52+
// origins: [
53+
// {
54+
// name: 'origin',
55+
// hosts: [{ location: 'staging-origin.mysite.com' }],
56+
// override_host_header: 'staging-origin.mysite.com',
57+
// tls_verify: {
58+
// use_sni: true,
59+
// sni_hint_and_strict_san_check: 'staging-origin.mysite.com',
60+
// },
61+
// shields: { us_east: 'DCD' },
62+
// },
63+
// ],
64+
// },
65+
// },
66+
67+
// Options for hosting serverless functions on Edgio
68+
// serverless: {
69+
// // Set to true to include all packages listed in the dependencies property of package.json when deploying to Edgio.
70+
// // This option generally isn't needed as Edgio automatically includes all modules imported by your code in the bundle that
71+
// // is uploaded during deployment
72+
// includeNodeModules: true,
73+
//
74+
// // Include additional paths that are dynamically loaded by your app at runtime here when building the serverless bundle.
75+
// include: ['views/**/*'],
76+
// },
77+
78+
// The maximum number of URLs that will be concurrently prerendered during deployment when static prerendering is enabled.
79+
// Defaults to 200, which is the maximum allowed value.
80+
// prerenderConcurrency: 200,
81+
82+
// A list of glob patterns identifying which source files should be uploaded when running edgio deploy --includeSources.
83+
// This option is primarily used to share source code with Edgio support personnel for the purpose of debugging. If omitted,
84+
// edgio deploy --includeSources will result in all files which are not gitignored being uploaded to Edgio.
85+
//
86+
// sources : [
87+
// '**/*', // include all files
88+
// '!(**/secrets/**/*)', // except everything in the secrets directory
89+
// ],
90+
};

0 commit comments

Comments
 (0)