Skip to content

Commit 9fde89a

Browse files
committed
feat(typing)
Signed-off-by: Pierre PÉRONNET <pierre.peronnet@datadoghq.com>
1 parent 3a04251 commit 9fde89a

File tree

9 files changed

+28
-25
lines changed

9 files changed

+28
-25
lines changed

plugins/cloudflare-auto-session/functions/_middleware.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { withDefaults } from '../src/args'
55
const authenticatedQuery = 'authenticated'
66
const allowedQuery = 'allowed'
77

8-
export const onRequestGet = async (context: EventPluginContext<Record<string, string | undefined>, any, any, PluginArgs>): Promise<Response> => {
8+
export const onRequestGet = (context: EventPluginContext<Record<string, string | undefined>, any, any, PluginArgs>): Response | Promise<Response> => {
99
const { request, env, pluginArgs, next } = context
1010

1111
// Get the arguments given to the Plugin by the developer
@@ -20,21 +20,19 @@ export const onRequestGet = async (context: EventPluginContext<Record<string, st
2020

2121
console.debug('Proxy to login form', url.toString())
2222

23-
return await env.ASSETS.fetch(new Request(url, request))
23+
return env.ASSETS.fetch(new Request(url, request))
2424
}
2525

26-
return await next()
26+
return next()
2727
}
2828

29-
export const onRequestPost = async ({ request, pluginArgs }: EventPluginContext<Record<string, string | undefined>, any, any, PluginArgs>): Promise<Response> => {
29+
export const onRequestPost = ({ request, pluginArgs }: EventPluginContext<Record<string, string | undefined>, any, any, PluginArgs>): Response | Promise<Response> => {
3030
// Get the arguments given to the Plugin by the developer
3131
const { cookieName, cookieSecret, login, isValid } = withDefaults(pluginArgs)
3232

3333
const session = new Session(cookieName, cookieSecret, isValid)
3434

3535
if (session.valid(request)) {
36-
await request.body?.cancel()
37-
3836
return new Response('Already logged in', {
3937
status: 302,
4038
headers: {
@@ -45,7 +43,7 @@ export const onRequestPost = async ({ request, pluginArgs }: EventPluginContext<
4543

4644
const url = new URL(request.url)
4745

48-
return await request.formData()
46+
return request.formData()
4947
.then(login)
5048
.then(({ authenticated, allowed, cookie }: SessionSpec): Response => {
5149
url.searchParams.set(authenticatedQuery, authenticated.toString())

plugins/cloudflare-auto-session/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@natbienetre/cloudflare-auto-session",
33
"main": "dist/index.js",
44
"types": "index.d.ts",
5-
"version": "0.3.1",
5+
"version": "0.3.2",
66
"license": "MPL-2.0",
77
"funding": "https://github.com/sponsors/holyhope",
88
"files": [
-1.67 KB
Binary file not shown.

plugins/cloudflare-env-var-password/functions/_middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { PluginArgs } from '../src/types'
44
import { Auth } from '../src/authenticator'
55
import { withDefaults } from '../src/args'
66

7-
export const onRequest = (context: EventPluginContext<Record<string, string | undefined>, any, any, PluginArgs>): Response => {
7+
export const onRequest = (context: EventPluginContext<Record<string, string | undefined>, any, any, PluginArgs>): Response | Promise<Response> => {
88
const { passwordEncodingMethod, passwordFieldName, getEnvVarName } = withDefaults(context.pluginArgs)
99

1010
console.log('onRequest', new URL(context.request.url).toString())

plugins/cloudflare-env-var-password/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@natbienetre/cloudflare-env-var-password",
33
"main": "dist/index.js",
44
"types": "index.d.ts",
5-
"version": "0.1.3",
5+
"version": "0.1.4",
66
"license": "MPL-2.0",
77
"funding": "https://github.com/sponsors/holyhope",
88
"files": [
@@ -18,7 +18,7 @@
1818
"dev": "npm run build"
1919
},
2020
"dependencies": {
21-
"@natbienetre/cloudflare-auto-session": "^0.3.1"
21+
"@natbienetre/cloudflare-auto-session": "0.3.2"
2222
},
2323
"devDependencies": {
2424
"@cloudflare/workers-types": "^4.20230814.0",

plugins/cloudflare-env-var-password/src/authenticator.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ export class Auth {
4343

4444
async sessionData (formData: FormData): Promise<SessionSpec> {
4545
const expected = this.getExpectedPassword()
46-
4746
const password = formData.get(this.passwordFieldName)
4847

4948
if (password === null || password === undefined) {
50-
throw new Error(`Password field '${this.passwordFieldName}' not found`)
49+
return {
50+
authenticated: false,
51+
allowed: false
52+
}
5153
}
5254

5355
formData.delete(this.passwordFieldName)
@@ -63,13 +65,16 @@ export class Auth {
6365
return btoa(String.fromCharCode(...new Uint8Array(hash)))
6466
})
6567
}
66-
return await hash.then((hash: string) => {
67-
console.debug(`Password: ${hash} === ${password}`, hash === password)
6868

69-
if (hash !== expected) {
69+
return await hash.then((hash: string): SessionSpec => {
70+
const passwordMatch = hash === expected
71+
72+
console.debug(`Password: ${hash} === ${expected}`, passwordMatch)
73+
74+
if (!passwordMatch) {
7075
return {
71-
authenticated: false,
72-
allowed: false
76+
authenticated: true,
77+
allowed: passwordMatch
7378
}
7479
}
7580

@@ -78,8 +83,8 @@ export class Auth {
7883
allowed: true,
7984
cookie: {
8085
data: {
81-
username: formData.get('username'),
82-
path: this.url.pathname
86+
path: this.url.pathname,
87+
...formData.entries()
8388
},
8489
path: this.url.pathname
8590
}

plugins/cloudflare-env-var-password/yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ __metadata:
8686
languageName: node
8787
linkType: hard
8888

89-
"@natbienetre/cloudflare-auto-session@npm:^0.3.1":
90-
version: 0.3.1
91-
resolution: "@natbienetre/cloudflare-auto-session@npm:0.3.1"
89+
"@natbienetre/cloudflare-auto-session@npm:0.3.2":
90+
version: 0.3.2
91+
resolution: "@natbienetre/cloudflare-auto-session@npm:0.3.2"
9292
dependencies:
9393
cookie: ^0.5.0
9494
crypto-js: ^4.1.1
95-
checksum: bd361c70974ef0e618ec6a2ea315a04b0bca0ca1996b9611ed626f17315de89650f10443848f88eca6e5eba9bf463054392215e63ce4104c2bbc79e9990da6c6
95+
checksum: 4220a07c866a98d035f2f6f72eaf8d41da428689b3e1feaa80cc4f326f39d0209c1fd958832d8eeaf0038bf791a5977d984a49d022b704fbf70a6ee9b513573f
9696
languageName: node
9797
linkType: hard
9898

@@ -101,7 +101,7 @@ __metadata:
101101
resolution: "@natbienetre/cloudflare-env-var-password@workspace:."
102102
dependencies:
103103
"@cloudflare/workers-types": ^4.20230814.0
104-
"@natbienetre/cloudflare-auto-session": ^0.3.1
104+
"@natbienetre/cloudflare-auto-session": 0.3.2
105105
"@typescript-eslint/eslint-plugin": ^5.62.0
106106
eslint: ^8.47.0
107107
eslint-config-standard-with-typescript: ^37.0.0

0 commit comments

Comments
 (0)