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

export class ImplicitGrantController {
  isImplicitGrantReq(req: Request) {
    const query = req.getQuery();
    // const raw = req.getRaw().toString();

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

    return false;
  }

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

    if (
      (query.includes("access_token=") && query.includes("state=")) ||
      path.includes("&access_token=") ||
      /access_token=%/i.test(query)
    ) {
      return true;
    }

    return false;
  }

  testReq(req: Request) {
    if (this.isImplicitGrantReq(req)) {
      return "isImplicitGrantReq";
    }
    if (this.isSendTokenToClient(req)) {
      return "isSendTokenToClient";
    }
    return false;
  }
}
