Skip to content
oidc.ts 1.21 KiB
Newer Older
import * as openid from "openid-client";
Grant's avatar
Grant committed

class OpenID_ {
  config: openid.Configuration = {} as any;
Grant's avatar
Grant committed

  async setup() {
    if (process.env.INHIBIT_LOGIN) {
      console.warn(
        "OpenID is not setup; INHIBIT_LOGIN environment variable set! Proceed with caution!"
      );
      return;
    }

Grant's avatar
Grant committed
    const { AUTH_ENDPOINT, AUTH_CLIENT, AUTH_SECRET } = process.env;

    this.config = await openid.discovery(new URL(AUTH_ENDPOINT), AUTH_CLIENT, {
Grant's avatar
Grant committed
      client_secret: AUTH_SECRET,
    });
  }

  getRedirectUrl() {
    return process.env.OIDC_CALLBACK_HOST + "/api/callback";
  }

  getAuthorizationURL() {
    return openid
      .buildAuthorizationUrl(this.config, {
        redirect_uri: this.getRedirectUrl(),
        prompt: "consent",
        scope: "openid instance",
      })
      .toString();
  }

  exchangeToken(relativePath: string) {
    return openid.authorizationCodeGrant(
      this.config,
      new URL(relativePath, process.env.OIDC_CALLBACK_HOST)
    );
  }

  userInfo<Data extends {} = {}>(
    accessToken: string,
    expectedSub: string
  ): Promise<openid.UserInfoResponse & Data> {
    return openid.fetchUserInfo(this.config, accessToken, expectedSub) as any;
  }
Grant's avatar
Grant committed
}

export const OpenID = new OpenID_();