Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン
#557 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
aws, typescript

調査の方向性

$connect の WebSocket 設定と authHandler の戻り値から始め、それらを API Gateway の authorizer コントラクトおよび観測された 500 レスポンスと比較します。この例でサポートされるエラーステータスと TypeScript のハンドラー型が確定し、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分
マージ済み PR(30日)
3

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

serverless/examples のほかの issue

serverless/examples の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。