2-2. code を発行する
OP の認可ページから code を発行します。
実装するファイル
| ファイル | 関数 |
|---|---|
templates/packages/openid-provider/server/routes/consent.post.ts | consentLogic |
templates/packages/openid-provider/app/pages/authorize.vue | grantAccess |
consent.post.ts の consentLogic を実装する
認可ページ(GET /authorize)が authorization endpoint の役割を果たし、POST /consent はそのサーバー側ヘルパーです。ユーザーが選択した user_id に対して code を発行し、RP の callback にリダイレクトします。
実装手順
input.responseTypeが"code"でなければ、unsupported_response_typeをthrow new Error()するinput.clientIdが"rp-demo"でなければ、invalid_clientをthrow new Error()するinput.scopeをスペース区切りで分割し、example以外が含まれていればinvalid_scopeをthrow new Error()するinput.userIdが空文字列なら、invalid_requestをthrow new Error()するfindUserでユーザーを取得し、randomString(32)で code を作るsetAuthCodeで code とclientId/scope/idを保存するhttp://localhost:3000/callbackにcodeを付けて、redirectToとして返す
使うユーティリティ関数
| 関数 | 役割 |
|---|---|
findUser(userId) | ユーザーを取得する |
randomString(length) | ランダムな文字列を生成する |
setAuthCode(code, payload) | 認可コードを保存する (Redis) |
ヒント
ヒント1
手順3の scope 分割は、以下のように書きます。
ts
const scopes = input.scope.split(" ").filter((scope) => scope !== "");
if (scopes.some((scope) => !["example"].includes(scope))) {
throw new Error("invalid_scope");
}ヒント2
手順6の setAuthCode は、以下の引数で呼び出します。
ts
await setAuthCode(code, {
clientId: input.clientId,
scope: scopes,
id: user.id,
});ヒント3
手順7のリダイレクト URL 構築は、以下のように書きます。
ts
const redirect = new URL("http://localhost:3000/callback");
redirect.searchParams.set("code", code);
return { redirectTo: redirect.toString() };想定解答
想定解答
ts
// TODO: Phase 1: OAuth
// TODO: Phase 2: state
const consentLogic = async (input: ConsentInput): Promise<ConsentResponse> => {
return { redirectTo: "" };
if (input.responseType !== "code") {
throw new Error("unsupported_response_type");
}
if (input.clientId !== "rp-demo") {
throw new Error("invalid_client");
}
const scopes = input.scope.split(" ").filter((scope) => scope !== "");
if (scopes.some((scope) => !["example"].includes(scope))) {
throw new Error("invalid_scope");
}
if (input.userId === "") {
throw new Error("invalid_request");
}
const user = await findUser(input.userId);
const code = randomString(32);
await setAuthCode(code, {
clientId: input.clientId,
scope: scopes,
id: user.id,
});
const redirect = new URL("http://localhost:3000/callback");
redirect.searchParams.set("code", code);
return { redirectTo: redirect.toString() };
}; authorize.vue の grantAccess を実装する
/consent に POST して、リダイレクト先を取得します。
実装手順
postRequestで/consentに POST するresponse_type、client_id、scope、user_idを送る
redirectToでresult.redirectToにリダイレクトする
使うユーティリティ関数
| 関数 | 役割 |
|---|---|
postRequest<Response>(url, body, form?) | POST リクエストを送る |
redirectTo(url) | 指定した URL にリダイレクトする |
ヒント
ヒント1
手順1の postRequest は、以下の引数で呼び出します。
ts
postRequest<ConsentResponse>("/consent", {
response_type: input.responseType,
client_id: input.clientId,
scope: input.scope,
user_id: userId,
});想定解答
想定解答
ts
// TODO: Phase 1: OAuth
// TODO: Phase 2: state
const grantAccess = async (userId: string, input: ConsentInput): Promise<void> => {
return;
const result = await postRequest<ConsentResponse>("/consent", {
response_type: input.responseType,
client_id: input.clientId,
scope: input.scope,
user_id: userId,
});
redirectTo(result.redirectTo);
};