import fetch from "node-fetch"; import { ScrapMemberConfig } from "../config"; import * as fw from "../fw"; import { URL } from "url"; /** * XRDSのtextを取得する。本関数は以下の例外が発生し得る * - InternalServerError: OpenID Discovery Requestに問題が発生した場合に返却される。errorCodeは500 * @param url APIのURL * @param config APIの環境情報 * @returns XRDSのtext情報 */ export async function getXrds(url: string, config: ScrapMemberConfig) { const res = await fetch(`${url}&openid.ns=${config.openIdAuthParams["openid.ns"]}`); if (res.status != 200) { throw new fw.types.InternalServerError("OpenID Discovery Requestの実行に失敗しました。"); } return await res.text(); } /** * OpenID Check Authentication Request を実行しOpenID承認応答の検証をする。 * - InternalServerError: OpenID Check Authentication Requestに問題が発生した場合に返却される。errorCodeは500 * @param xrdsUrl 検証用APIのURL * @param resUrl OpenID Check Authentication ResponseのURL * @returns 検証結果 */ export async function openIdCheckAuth(xrdsUrl: string, resUrl: string) { const resUrlObj = new URL(resUrl); const xrdsUrlObj = new URL(xrdsUrl); resUrlObj.searchParams.set("openid.mode", "check_authentication"); for (const [key, value] of resUrlObj.searchParams) { xrdsUrlObj.searchParams.append(key, value); } const res = await fetch(xrdsUrlObj.href); if (res.status != 200) { throw new fw.types.InternalServerError("OpenID Check Authentication Request の実行に失敗しました。"); } const data = await res.text(); return data.indexOf("is_valid:true") !== -1 ? true : false; }