ラッパーと cookie

ラッパー#

ラッパーは、リクエストとレスポンスを包む共通処理です。init.comat で宣言し(init / defaults / ラッパー)、各言語のコードで実装を書いて、init に渡します。

名前は、init.comat の wrapper 名前 と同じにします。生成された Wrappers の型が、足りないラッパーや型の違いを教えます(Go を除く)。

TypeScript#

TypeScript
// Implement a wrapper as a value of type ComatypeWrap and export it (corresponds to the `wrapper auth` of init.comat)
import type { ComatypeWrap } from '@comatype/typescript'
// Generated from init.comat: the ctx type (AuthCtx) and the statuses a wrapper can return (s401)
import { s401, type AuthCtx } from '@/comatype/init'
import { db } from '../db'

// ComatypeWrap<AuthCtx>: the type of values that can be added to ctx. On endpoints with `wrap auth` on, a handler can read req.ctx.user, typed
export const auth: ComatypeWrap<AuthCtx> = async (req, next) => {
  // A session cookie (browsers) or Authorization: Bearer (other clients)
  const bearer = req.headers.authorization?.replace(/^Bearer /i, '')
  const user = db.bySession(req.cookies.session ?? bearer)
  if (!user) {
    return s401({ error: { code: 'unauthorized', message: 'login required' } })
  }
  return next({ ...req, ctx: { ...req.ctx, user } })
}
  • サーバでは next({ ...req, ctx: { ...req.ctx, user } }) と ctx を足して、後続とハンドラに渡します。
  • s401(...) のように、ラッパーが宣言したステータスを返して、そこで打ち切ることができます。
  • クライアントでは、リクエストのヘッダを書き換えて next(req) を呼びます。
TypeScript
// Implement a wrapper as a value of type ComatypeWrap and export it (corresponds to the `wrapper auth` of init.comat)
import type { ComatypeWrap } from '@comatype/typescript'

export const CLIENT_VERSION = '1.0.0'

// A wrapper receives the request and `next` and returns a response (both can be rewritten)
// The session travels in a cookie (kept and sent by the cookie.jar of init), so only headers common to every request are added here
export const auth: ComatypeWrap = async (req, next) => {
  // headerReq / header (X-Client-Version / X-Request-Id) of defaults.comat
  req.headers['x-client-version'] = CLIENT_VERSION
  req.headers['x-request-id'] ??= crypto.randomUUID()
  return next(req)
}

Go#

Go
var Auth s.CoatWrap[comatype_init.AuthCtx] = func(req *s.RawRequest[comatype_init.AuthCtx], next s.Next[comatype_init.AuthCtx]) s.RawResponse {
    user, ok := verifySession(req.Cookie("session"))
    if !ok {
        return comatype_init.S401(&comatype_init.S401Res{Code: "unauthorized", Message: "login required"})
    }
    req.Ctx = comatype_init.AuthCtx{User: user}
    return next(req)
}

クライアントは c.ComatypeWrap(func(req *c.RawRequest, next c.Next) (*c.RawResponse, error))です。

Python#

Python
async def _auth(req, next):
    user = await verify_session(req.cookies.get("session"))
    if user is None:
        return S401(error=S401.Res(code="unauthorized", message="login required"))
    req.ctx = AuthCtx(user=user)
    return await next(req)

auth = ComatypeWrap[AuthCtx](_auth)

動き方#

  • ラッパーは、外側から内側へ重なります。外側が先に動き、レスポンスは内側から外側へ戻ります。
  • ラッパーが見るのは、エンドポイントごとの型を持たない生のリクエスト・レスポンスです(通信上の形)。
  • ラッパーが s401() で作ったレスポンスは、その場で検証・変換されて、外側のラッパーからも同じ形で見えます。
  • wrap されているのに init に渡していないラッパーがあると、クライアントは通信前にエラー、サーバは 500 です。

発行(サーバ)#

cookie on のエンドポイントで、レスポンスを作るときに渡します。属性(httpOnly secure sameSite)は init.comat の設定です。ヘッダと cookie に、3 言語の書き方があります。

  • 文字列 → その値で Set-Cookie
  • maxAge expires path domain → その発行だけ変える
  • 削除 → Max-Age=0 と過去の Expires
  • 宣言に無い cookie や、cookie off のエンドポイントで渡すと 500

受信(サーバ)#

ハンドラの req.cookies、ラッパーの req.cookies で読みます。

保持(クライアント)#

ブラウザは credentials: 'include' で、ブラウザに任せます。Node など cookie を保持しない環境では cookie jar を渡します。

言語渡し方
TypeScriptc.init({ cookie: { jar: createCookieJar() } })
Goc.Init(c.Config{CookieJar: c.NewCookieJar()})
Pythonc.init(cookies=True)(または CookieJar())

jar は、cookie on のエンドポイントのレスポンスの Set-Cookie を保持して、次のリクエストに Cookie ヘッダで送ります。API が 1 つの origin である前提で、Domain は無視し、Path と期限だけを見ます。HttpOnly の cookie も保持します。