Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbAuth Vercel Fix #3309

Merged
merged 6 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/api/src/functions/dbAuth/DbAuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,16 @@ export class DbAuthHandler {

// parses the event body into JSON, whether it's base64 encoded or not
_parseBody() {
if (this.event.isBase64Encoded) {
return JSON.parse(
Buffer.from(this.event.body || '', 'base64').toString('utf-8')
)
if (this.event.body) {
if (this.event.isBase64Encoded) {
return JSON.parse(
Buffer.from(this.event.body || '', 'base64').toString('utf-8')
)
} else {
return JSON.parse(this.event.body)
}
} else {
return this.event.body && JSON.parse(this.event.body)
return {}
}
}

Expand Down Expand Up @@ -481,7 +485,7 @@ export class DbAuthHandler {
_ok(body: string, headers = {}, options = { statusCode: 200 }) {
return {
statusCode: options.statusCode,
body,
body: typeof body === 'string' ? body : JSON.stringify(body),
headers: { 'Content-Type': 'application/json', ...headers },
}
}
Expand Down
64 changes: 62 additions & 2 deletions packages/api/src/functions/dbAuth/__tests__/DbAuthHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('dbAuth', () => {
expect(dbAuth.options).toEqual(options)
})

it('parses a plain text body', () => {
it('parses params from a plain text body', () => {
event = { headers: {}, body: `{"foo":"bar", "baz":123}` }
context = { foo: 'bar' }
options = { db: db }
Expand All @@ -183,7 +183,28 @@ describe('dbAuth', () => {
expect(dbAuth.params).toEqual({ foo: 'bar', baz: 123 })
})

it('parses a base64 encoded body', () => {
it('parses an empty plain text body and still sets params', () => {
event = { isBase64Encoded: false, headers: {}, body: '' }
context = { foo: 'bar' }
options = { db: db }
const dbAuth = new DbAuthHandler(event, context, options)

expect(dbAuth.params).toEqual({})
})

it('parses params from an undefined body when isBase64Encoded == false', () => {
event = {
isBase64Encoded: false,
headers: {},
}
context = { foo: 'bar' }
options = { db: db }
const dbAuth = new DbAuthHandler(event, context, options)

expect(dbAuth.params).toEqual({})
})

it('parses params from a base64 encoded body', () => {
event = {
isBase64Encoded: true,
headers: {},
Expand All @@ -196,6 +217,31 @@ describe('dbAuth', () => {
expect(dbAuth.params).toEqual({ foo: 'bar', baz: 123 })
})

it('parses params from an undefined body when isBase64Encoded == true', () => {
event = {
isBase64Encoded: true,
headers: {},
}
context = { foo: 'bar' }
options = { db: db }
const dbAuth = new DbAuthHandler(event, context, options)

expect(dbAuth.params).toEqual({})
})

it('parses params from an empty body when isBase64Encoded == true', () => {
event = {
isBase64Encoded: true,
headers: {},
body: '',
}
context = { foo: 'bar' }
options = { db: db }
const dbAuth = new DbAuthHandler(event, context, options)

expect(dbAuth.params).toEqual({})
})

it('sets header-based CSRF token', () => {
event = { headers: { 'csrf-token': 'qwerty' } }
const dbAuth = new DbAuthHandler(event, context, options)
Expand Down Expand Up @@ -907,6 +953,20 @@ describe('dbAuth', () => {

expect(response.statusCode).toEqual(201)
})

it('stringifies a JSON body', () => {
const dbAuth = new DbAuthHandler(event, context, options)
const response = dbAuth._ok({ foo: 'bar' }, {}, { statusCode: 201 })

expect(response.body).toEqual('{"foo":"bar"}')
})

it('does not stringify a body that is a string already', () => {
const dbAuth = new DbAuthHandler(event, context, options)
const response = dbAuth._ok('{"foo":"bar"}', {}, { statusCode: 201 })

expect(response.body).toEqual('{"foo":"bar"}')
})
})

describe('_notFound', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/auth/src/authClients/dbAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export const dbAuth = (): AuthClient => {
`${global.__REDWOOD__API_PROXY_PATH}/auth?method=getToken`
)
const token = await response.text()
return token

if (token.length === 0) {
return null
} else {
return token
}
}

const login = async (attributes: LoginAttributes) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ export const handler = async (event, context) => {
loginExpires: 60 * 60 * 24 * 365 * 10,
})

return authHandler.invoke()
return await authHandler.invoke()
}