Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Websocket authorzation handler - return something that isn't `500`?

未关闭
#557 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
aws, typescript

调研方向

先从 $connect 的 WebSocket 配置和 authHandler 的返回值入手,然后将它们与 API Gateway authorizer 契约以及观察到的 500 响应进行比较。对于此示例,确定支持的错误状态和 TypeScript handler 类型,并为缺少 secret 的请求提供可复现的检查,即视为完成。

由索引模型根据 Issue 内容生成。

描述

Question / help needed

I'm trying to get authorization working before connecting to websockets, which works, but I'm struggling to return an error message that isn't 500.

I've managed to make an auth handler, like so:


    connectHandler: {
      handler: 'handler.connectHandler',
      events: [
        {
          "websocket": {
            route: "$connect",
            authorizer: {
              name: "authHandler",
              identitySource: [
                // No identity source, leave it up to the handler
              ],
            },
          },
        },
      ],
      environment: {
        // ..
      },
    },

Any my authHandler:

export const authHandler = async (event, _context) => {
  console.log('🔑 Auth request received', event.requestContext)

  const token = event.headers.Authorization ?? event.queryStringParameters.Authorization;
  console.log('Token:', token)

  if (token !== 'secret') {
    return {
      statusCode: 401,
    }
  }

  return {
    "principalId": "user",
    "policyDocument": {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Action": "execute-api:Invoke",
          "Effect": "Allow",
          "Resource": event.methodArn
        }
      ]
    }
  };
}

Questions:

  • what should I return in case of an error? Any request to my websocket-server that doesn't have the secret header/querystring returns 500 right now, and I'd prefer it to return a 4xx status code.

  • what is the correct type for the authorization handler? I'm using APIGatewayProxyHandler on the other fn handlers which gives me nice inline errors.

    How my other handlers look like, which gives me pleasant TypeScript-errors when I do something wrong ```ts

    export const connectHandler: APIGatewayProxyHandler = async (event, _context) => {
    const connectionId = event.requestContext.connectionId!

    console.log('➕ Adding connection ', connectionId)

    await client.query('INSERT INTO "public"."Connection"("id") VALUES($1) RETURNING *', [connectionId])
    return {
    statusCode: 200,
    body: '',
    };
    }

    </details>
主要语言
JavaScript
星标
11.5k
派生
4.4k
平均合并
2 小时 55 分钟
30 天内合并 PR
3

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

serverless/examples 的其他 Issue

查看 serverless/examples 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。