import type { Request } from "caido:utils";

export class AuthZCodeGrantController {
  constructor() {}

  isAuthZReq(req: Request) {
    // const path = req.getPath();
    const query = req.getQuery();
    // const raw = req.getRaw().toString();

    // 쿼리에 client_id and response_type=code 포함 확인
    if (query.includes("client_id=") && query.includes("response_type=code")) {
      return true;
    }

    return false;
  }

  isSendCodeToClient(req: Request) {
    const path = req.getPath();
    const query = req.getQuery();

    // code & state 쌍 또는 path에 &code= 또는 쿼리로 code= 유사 일치 확인
    if (
      (query.includes("code=") && query.includes("state=")) ||
      path.includes("&code=") ||
      /code=%/i.test(query)
    ) {
      return true;
    }

    return false;
  }

  testReq(req: Request) {
    if (this.isAuthZReq(req)) {
      return "isAuthZReq";
    }
    if (this.isSendCodeToClient(req)) {
      return "isSendCodeToClient";
    }
    return false;
  }

  // isAccessTokenReq(req: Response) {

  // }
}
