Skip to content

Commit

Permalink
fix: don't require context (#1471) (#1472)
Browse files Browse the repository at this point in the history
  • Loading branch information
aardvarkk authored Jun 14, 2022
1 parent dc6c4d8 commit aaccf96
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/events/websocket/WebSocketClients.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,20 @@ export default class WebSocketClients {
`Authorization function returned a successful response: (λ: ${authFunName})`,
)

const validatedContext = authValidateContext(
policy.context,
authorizerFunction,
)
if (validatedContext instanceof Error) throw validatedContext
if (policy.context) {
const validatedContext = authValidateContext(
policy.context,
authorizerFunction,
)
if (validatedContext instanceof Error) throw validatedContext
policy.context = validatedContext
}

this.#webSocketAuthorizersCache.set(connectionId, {
authorizer: {
integrationLatency: '42',
principalId: policy.principalId,
...validatedContext,
...policy.context,
},
identity: {
apiKey: policy.usageIdentifierKey,
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/websocket-authorizer/src/authorizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ exports.authorizerAsyncFunction = async function authorizerAsyncFunction(
return generatePolicy('user123', 'Deny', event.methodArn)
}

if (credential === 'noContext') {
const policy = generatePolicy('user123', 'Allow', event.methodArn)
delete policy.context
return policy
}

if (credential === 'exception') {
throw new Error('Failed')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import websocketSend from '../_testHelpers/websocketPromise.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

describe.skip('websocket authorizer tests', function desc() {
describe('websocket authorizer tests', function desc() {
this.timeout(30000)

beforeEach(() =>
Expand Down Expand Up @@ -47,6 +47,20 @@ describe.skip('websocket authorizer tests', function desc() {
assert.equal(data, undefined)
})

it('websocket authorization without context', async () => {
const url = new URL(joinUrl(env.TEST_BASE_URL, '/dev'))
url.port = url.port ? '3001' : url.port
url.protocol = 'ws'
url.searchParams.append('credential', 'noContext')

const ws = new WebSocket(url.toString())
const { data, code, err } = await websocketSend(ws, '{}')

assert.equal(code, undefined)
assert.equal(err, undefined)
assert.equal(data, '{}')
})

it('websocket authorization with authorizer crash', async () => {
const url = new URL(joinUrl(env.TEST_BASE_URL, '/dev'))
url.port = url.port ? '3001' : url.port
Expand Down

0 comments on commit aaccf96

Please sign in to comment.