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

fix: don't require context (#1471) #1472

Merged
merged 1 commit into from
Jun 14, 2022
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
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