openid / AppAuth-JS

JavaScript client SDK for communicating with OAuth 2.0 and OpenID Connect providers.
Apache License 2.0
975 stars 162 forks source link

xhr.js:53 Uncaught (in promise) ReferenceError: $ is not defined #192

Closed SlowLow32 closed 3 years ago

SlowLow32 commented 3 years ago

Expected Behavior

[REQUIRED] Describe expected behavior

should return some data

Describe the problem

The code always seems to fail on fetchFromIssuer(openIdConnectUrl) not sure why

[REQUIRED] Actual Behavior

image

[REQUIRED] Steps to reproduce the behaviour

[REQUIRED] Environment

export class AuthFlow {
  private notifier: AuthorizationNotifier;
  private authorizationHandler: AuthorizationRequestHandler;
  private tokenHandler: TokenRequestHandler;
  readonly authStateEmitter: AuthStateEmitter;

  // state
  private configuration: AuthorizationServiceConfiguration | undefined;

  private refreshToken: string | undefined;
  private accessTokenResponse: TokenResponse | undefined;

    constructor() {
        this.notifier = new AuthorizationNotifier();
        this.authStateEmitter = new AuthStateEmitter();
        this.authorizationHandler = new NodeBasedHandler();
        this.tokenHandler = new BaseTokenRequestHandler(requestor);
        // set notifier to deliver responses
        this.authorizationHandler.setAuthorizationNotifier(this.notifier);
        // set a listener to listen for authorization responses
        // make refresh and access token requests.
        this.notifier.setAuthorizationListener((request, response, error) => {
          log("Authorization request complete ", request, response, error);
          if (response) {
            let codeVerifier: string | undefined;
            if (request.internal && request.internal.code_verifier) {
              codeVerifier = request.internal.code_verifier;
            }

            this.auth(response.code)
          }
        });
    }

  fetchServiceConfiguration(): Promise<void> {
    return AuthorizationServiceConfiguration.fetchFromIssuer(
      openIdConnectUrl,
      requestor
    ).then(response => {
      log("Fetched service configuration", response);
      this.configuration = response;
    });
  }

  makeAuthorizationRequest(username?: string) {

    if (!this.configuration!) {
      log("Unknown service configuration");
      return;
    }

    const extras: StringMap = { prompt: "consent", access_type: "offline" };
    if (username) {
      extras["login_hint"] = username;
    }

    // create a request
    const request = new AuthorizationRequest({
      client_id: clientId,
      redirect_uri: redirectUri,
      scope: scope,
      response_type: AuthorizationRequest.RESPONSE_TYPE_CODE,
      state: undefined,
      extras: extras
    }, new NodeCrypto());

    log("Making authorization request ", this.configuration, request);

    this.authorizationHandler.performAuthorizationRequest(
      this.configuration!,
      request
    );
  }

    private auth(code: string | undefined){
        AuthorizationServiceConfiguration.fetchFromIssuer(openIdConnectUrl)
        .then(response => {
          log('Fetched service configuration', response);
          this.configuration = response;

        }).catch(error => {
          log('Something bad happened', error);
        });

        // use the code to make the token request.
        let request = new TokenRequest({
            client_id: clientId,
            redirect_uri: redirectUri,
            grant_type: GRANT_TYPE_AUTHORIZATION_CODE,
            code: code
        });

        this.tokenHandler.performTokenRequest(this.configuration!, request)
        .then(response => {
            console.log('Get tokenHandler Response',response)
        }).catch(error => {
            log('Something bad happened', error);
        });
    }

  loggedIn(): boolean {
      console.log(`accessTokenResponse`, this.accessTokenResponse)
    return !!this.accessTokenResponse && this.accessTokenResponse.isValid();
  }

  signOut() {
    // forget all cached token state
    this.accessTokenResponse = undefined;
  }
}