@@ -3,44 +3,24 @@ import {
33 type CookieSpec ,
44} from '@natbienetre/cloudflare-auto-session' ;
55
6- import type { AllowedBots , PasswordEncodingMethod , CookieData } from './types' ;
7- import { allBots } from './google' ;
6+ import type { PasswordEncodingMethod , CookieData } from './types' ;
87
98export class Auth {
109 readonly passwordEncodingMethod : PasswordEncodingMethod ;
1110 readonly passwordFieldName : string ;
1211 readonly expectedPasswordHash : string ;
1312 readonly url : URL ;
14- readonly verifiers : Array < ( req : Request ) => Promise < boolean > > ;
1513
1614 constructor (
1715 request : Request ,
1816 passwordHash : string ,
1917 passwordEncodingMethod : PasswordEncodingMethod ,
20- passwordFieldName : string ,
21- allowedBots : AllowedBots
18+ passwordFieldName : string
2219 ) {
2320 this . url = new URL ( request . url ) ;
2421 this . passwordEncodingMethod = passwordEncodingMethod ;
2522 this . passwordFieldName = passwordFieldName ;
2623 this . expectedPasswordHash = passwordHash ;
27- this . verifiers = [ ...allowedBots . google ]
28- . filter ( value => value [ 1 ] )
29- . map (
30- value =>
31- allBots . get ( value [ 0 ] ) ??
32- ( async ( _ : Request ) : Promise < boolean > => false )
33- ) ;
34- }
35-
36- async verify ( req : Request ) : Promise < boolean > {
37- console . debug ( 'Checking if client is a trusted bot' , req . cf ?. botManagement ) ;
38- return this . verifiers
39- . map ( verif => verif ( req ) )
40- . reduce (
41- async ( acc , curr ) => ( await acc ) || ( await curr ) ,
42- Promise . resolve ( false )
43- ) ;
4424 }
4525
4626 isValid ( data : CookieData ) : boolean {
@@ -76,67 +56,53 @@ export class Auth {
7656 }
7757
7858 async sessionData ( request : Request ) : Promise < SessionSpec < CookieData > > {
79- return this . verify ( request ) . then ( verified => {
80- if ( verified ) {
81- console . info ( 'Trusted bot detected' ) ;
59+ return request . formData ( ) . then ( async formData => {
60+ const password = formData . get ( this . passwordFieldName ) ;
61+
62+ if ( password === null ) {
63+ console . warn ( 'No password provided' ) ;
8264
8365 return {
84- authenticated : true ,
85- allowed : true ,
66+ authenticated : false ,
67+ allowed : false ,
8668 cookie : this . cookieSpec ( {
87- source : 'trusted-bot ' ,
69+ source : 'no-password ' ,
8870 } ) ,
8971 } ;
9072 }
9173
92- return request . formData ( ) . then ( async formData => {
93- const password = formData . get ( this . passwordFieldName ) ;
94-
95- if ( password === null ) {
96- console . warn ( 'No password provided' ) ;
97-
98- return {
99- authenticated : false ,
100- allowed : false ,
101- cookie : this . cookieSpec ( {
102- source : 'no-password' ,
103- } ) ,
104- } ;
105- }
106-
107- return this . hashPassword ( password )
108- . then ( hashedPassword => this . expectedPasswordHash === hashedPassword )
109- . then ( passwordMatch => {
110- if ( ! passwordMatch ) {
111- console . warn (
112- `Password mismatch, expected ${ this . expectedPasswordHash } `
113- ) ;
114-
115- return {
116- authenticated : true ,
117- allowed : false ,
118- cookie : this . cookieSpec ( {
119- source : 'invalid-password' ,
120- } ) ,
121- } ;
122- }
123-
124- console . info ( 'Password match' ) ;
125-
126- // Remove the password from the form data
127- // before storing it in the cookie
128- formData . delete ( this . passwordFieldName ) ;
74+ return this . hashPassword ( password )
75+ . then ( hashedPassword => this . expectedPasswordHash === hashedPassword )
76+ . then ( passwordMatch => {
77+ if ( ! passwordMatch ) {
78+ console . warn (
79+ `Password mismatch, expected ${ this . expectedPasswordHash } `
80+ ) ;
12981
13082 return {
13183 authenticated : true ,
132- allowed : true ,
84+ allowed : false ,
13385 cookie : this . cookieSpec ( {
134- source : 'user-form' ,
135- userData : formData ,
86+ source : 'invalid-password' ,
13687 } ) ,
13788 } ;
138- } ) ;
139- } ) ;
89+ }
90+
91+ console . info ( 'Password match' ) ;
92+
93+ // Remove the password from the form data
94+ // before storing it in the cookie
95+ formData . delete ( this . passwordFieldName ) ;
96+
97+ return {
98+ authenticated : true ,
99+ allowed : true ,
100+ cookie : this . cookieSpec ( {
101+ source : 'user-form' ,
102+ userData : formData ,
103+ } ) ,
104+ } ;
105+ } ) ;
140106 } ) ;
141107 }
142108}
0 commit comments