Skip to content

Commit

Permalink
Clean up after dbAuth debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Jun 28, 2024
1 parent 6e991de commit 17b6706
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 178 deletions.
107 changes: 0 additions & 107 deletions packages/auth-providers/dbAuth/api/src/DbAuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,29 +411,6 @@ export class DbAuthHandler<
get _deleteSessionHeader(): Headers {
const deleteHeaders = new Headers()

console.log('')
console.log('------')
console.log('')
console.trace()
console.log('')
console.log('------')
console.log(
'_deleteSessionHeader cookie string',
[
`${cookieName(this.options.cookie?.name)}=`,
...this._cookieAttributes({ expires: 'now' }),
].join(';'),
)
console.log('')
console.log(
'_deleteSessionHeader auth-provider string',
[`auth-provider=`, ...this._cookieAttributes({ expires: 'now' })].join(
';',
),
)
console.log('------')
console.log('')

deleteHeaders.append(
'set-cookie',
[
Expand All @@ -449,12 +426,6 @@ export class DbAuthHandler<
),
)

console.log('')
console.log('------')
console.log('deleteHeaders', deleteHeaders)
console.log('------')
console.log('')

return deleteHeaders
}

Expand Down Expand Up @@ -555,14 +526,6 @@ export class DbAuthHandler<
try {
const method = await this._getAuthMethod()

console.log('')
console.log('------')
console.log('')
console.log('invoke method', method)
console.log('')
console.log('------')
console.log('')

// get the auth method the incoming request is trying to call
if (!DbAuthHandler.METHODS.includes(method)) {
return this.createResponse(this._notFound(), corsHeaders)
Expand All @@ -577,18 +540,6 @@ export class DbAuthHandler<
const [body, headers, options = { statusCode: 200 }] =
await this[method]()

console.log('')
console.log('------')
console.log('')
console.log('invoke body', body)
console.log('')
console.log('invoke headers', headers)
console.log('')
console.log('invoke options', options)
console.log('')
console.log('------')
console.log('')

return this.createResponse(this._ok(body, headers, options), corsHeaders)
} catch (e: any) {
if (e instanceof DbAuthError.WrongVerbError) {
Expand Down Expand Up @@ -678,74 +629,21 @@ export class DbAuthHandler<
}

async getToken(): Promise<AuthMethodOutput> {
console.log('')
console.log('------')
console.log('')
console.log('getToken')
console.log('')
console.log('getToken cookie', this.cookie)
console.log('')
console.log('------')
console.log('')

try {
const user = await this._getCurrentUser()
console.log('')
console.log('------')
console.log('')
console.log('getToken user', user)
console.log('')
console.log('------')
console.log('')
let headers = new Headers()

// if the session was encrypted with the old algorithm, re-encrypt it
// with the new one
if (isLegacySession(this.cookie)) {
console.log('')
console.log('------')
console.log('')
console.log('getToken isLegacySession')
console.log('')
console.log('------')
console.log('')
headers = this._loginResponse(user)[1]
}

console.log('')
console.log('------')
console.log('')
console.log('getToken headers', headers)
console.log('')
console.log('------')
console.log('')

return [user[this.options.authFields.id], headers]
} catch (e: any) {
console.log('')
console.log('------')
console.log('')
console.log('getToken catch e', e)
console.log('')
console.log('------')
console.log('')
if (e instanceof DbAuthError.NotLoggedInError) {
console.log('')
console.log('------')
console.log('')
console.log('getToken catch NotLoggedInError')
console.log('')
console.log('------')
console.log('')
return this._logoutResponse()
} else {
console.log('')
console.log('------')
console.log('')
console.log('getToken catch !NotLoggedInError', e.message)
console.log('')
console.log('------')
console.log('')
return this._logoutResponse({ error: e.message })
}
}
Expand Down Expand Up @@ -1638,11 +1536,6 @@ export class DbAuthHandler<

const headers = new Headers()

console.log(
'_loginResponse cookie string',
this._createAuthProviderCookieString(),
)

headers.append('csrf-token', csrfToken)
headers.append('set-cookie', this._createAuthProviderCookieString())
headers.append(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { APIGatewayProxyEvent } from 'aws-lambda'
import { describe, it, expect } from 'vitest'

import { buildDbAuthResponse } from '../shared'
import { getDbAuthResponseBuilder } from '../shared'

describe('buildDbAuthResponse', () => {
it('should add cors headers and set-cookie as array to the response', () => {
it('should add cors headers and set-cookie as array to the response to Requests', () => {
const resHeaders = new Headers({
header1: 'value1',
header2: 'value2',
Expand Down Expand Up @@ -33,7 +34,48 @@ describe('buildDbAuthResponse', () => {
},
}

const result = buildDbAuthResponse(response, corsHeaders)
const createResponse = getDbAuthResponseBuilder({} as Request)
const result = createResponse(response, corsHeaders)

expect(result).toEqual(expectedResponse)
})

it('should add cors headers and set-cookie as multiValueHeaders array to the response to APIGatewayProxyEvent', () => {
const resHeaders = new Headers({
header1: 'value1',
header2: 'value2',
})

resHeaders.append('set-cookie', 'cookie1=value1')
resHeaders.append('set-cookie', 'cookie2=value2')

const response = {
statusCode: 200,
headers: resHeaders,
}

const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
}

const expectedResponse = {
statusCode: 200,
headers: {
header1: 'value1',
header2: 'value2',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
},
multiValueHeaders: {
'Set-Cookie': ['cookie1=value1', 'cookie2=value2'],
},
}

const createResponse = getDbAuthResponseBuilder({
multiValueHeaders: {},
} as unknown as APIGatewayProxyEvent)
const result = createResponse(response, corsHeaders)

expect(result).toEqual(expectedResponse)
})
Expand Down Expand Up @@ -62,7 +104,8 @@ describe('buildDbAuthResponse', () => {
},
}

const result = buildDbAuthResponse(response, corsHeaders)
const createResponse = getDbAuthResponseBuilder({} as Request)
const result = createResponse(response, corsHeaders)

expect(result).toEqual(expectedResponse)
})
Expand Down
90 changes: 23 additions & 67 deletions packages/auth-providers/dbAuth/api/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ export const cookieName = (name: string | undefined) => {
return cookieName
}

/**
* Returns a builder for a lambda response
*
* This is used as the final call to return a response from the dbAuth handler
*
* Converts "Set-Cookie" headers to an array of strings or a multiValueHeaders
* object
*/
export function getDbAuthResponseBuilder(
event: APIGatewayProxyEvent | Request,
) {
Expand All @@ -274,85 +282,33 @@ export function getDbAuthResponseBuilder(
...corsHeaders,
}

const multiValueHeaders: Record<string, Array<string>> = {}
const dbAuthResponse: {
statusCode: number
headers: Record<string, string | Array<string>>
multiValueHeaders?: Record<string, Array<string>>
body?: string
} = {
...response,
headers,
}

const setCookieHeaders = response.headers?.getSetCookie() || []

if (setCookieHeaders.length > 0) {
if ((event as any).multiValueHeaders) {
console.log('getDbAuthResponseBuilder: multiValueHeaders')
multiValueHeaders['Set-Cookie'] = setCookieHeaders
if ('multiValueHeaders' in event) {
dbAuthResponse.multiValueHeaders = {
'Set-Cookie': setCookieHeaders,
}
delete headers['set-cookie']
} else {
headers['set-cookie'] = setCookieHeaders
}
}

const dbAuthResponse = {
...response,
headers,
}

console.log('')
console.log('------')
console.log('')
console.log('dbAuthResponse from builder', dbAuthResponse)
console.log('')
console.log('------')
console.log('')

return dbAuthResponse
}
}

/**
* Returns a lambda response
*
* This is used as the final call to return a response from the handler.
*
* Converts "Set-Cookie" headers to an array of strings or a multiValueHeaders
* object
*/
export const buildDbAuthResponse = (
response: {
body?: string
statusCode: number
headers?: Headers
},
corsHeaders: CorsHeaders,
) => {
const setCookieHeaders = response.headers?.getSetCookie() || []

console.log('')
console.log('------')
console.log('')
console.log('setCookieHeaders', setCookieHeaders)
console.log('')
console.log('------')
console.log('')

const dbAuthResponse = {
...response,
headers: {
...Object.fromEntries(response.headers?.entries() || []),
...(setCookieHeaders.length > 0
? {
'set-cookie': setCookieHeaders,
}
: {}),
...corsHeaders,
},
}

console.log('')
console.log('------')
console.log('')
console.log('dbAuthResponse', dbAuthResponse)
console.log('')
console.log('------')
console.log('')

return dbAuthResponse
}

export const extractHashingOptions = (text: string): ScryptOptions => {
const [_hash, ...options] = text.split('|')

Expand Down

0 comments on commit 17b6706

Please sign in to comment.