Skip to content

Properly implements openUri for implicit flow #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2019

Conversation

nhance
Copy link
Contributor

@nhance nhance commented Jun 27, 2018

When using implicitFlow, this change allows you to control how the browser opens the uri for login and logout.

You can pass a function for openUri on AuthConfig that accepts a parameter uri and will open the uri.

This can allow you to use an in app browser on a mobile device so you can capture the redirecturi if it doesn't exist.

Here's an example:

        authConfig.openUri = (uri) => {
          let browser = this.iab.create(uri, '_blank', 'location=no');

          browser.on('close').subscribe((event) => {
            if (window['oAuthService']) {
              window['oAuthService'].inImplicitFlow = false;
            }
          });

          browser.on('loadstart').subscribe((event) => {
            let url = event.url;

            if (url.startsWith(ENV.oauth.redirect_uri)) {
              const hashFragment = url.substr(ENV.oauth.redirect_uri.length);

              console.log("login from url scheme with token " + hashFragment);

              this.oauthService.tryLogin({
                customHashFragment: hashFragment,
                onTokenReceived: () => {
                  console.log("Login complete, should go to list page");
                  browser.close();
                  this.initializeLoggedIn();
                },
                onLoginError: () => {
                  console.log("Login error");
                  browser.close();
                  this.initializeUnauthenticated();
                }
              });

              return false;
            } else {
              console.log(url, ' did not start with ', ENV.oauth.redirect_uri);
            }
          });
this.oauthService.configure(authConfig);
this.oauthService.initImplicitFlow();

@nhance
Copy link
Contributor Author

nhance commented Jun 27, 2018

@manfredsteyer This pull request is necessary because #235 didn't get fully integrated during merge. The move into projects/lib lost the change from 235 that actually implemented openUri

@manfredsteyer
Copy link
Owner

What happens when openUri is not set? Will this lead to an error?

@neilsb
Copy link

neilsb commented Oct 16, 2018

Isn't the default value for openUri in the AuthConfig set to update the location.href?

@matt-psaltis
Copy link

Would love to see this go in. Currently fighting test-ability with the current hardcoded location.href call. The openUri config would make sinon stubbing possible/not relevant. Anything we can do to help get the missing changes from the merge in?

weronikasabiniewicz added a commit to weronikasabiniewicz/angular-oauth2-oidc that referenced this pull request Oct 30, 2018
@manfredsteyer manfredsteyer merged commit 9ed3cd6 into manfredsteyer:master Jul 17, 2019
@marcelonnunes
Copy link

Hello @nhance

Very interesting this new configuration option to allow external control over how the login page is opened. I searched all over the internet. It was the only place that I found such a solution. :-)

I need this feature exactly in my IONIC 5 application. I tried to implement it as suggested above, but I'm not getting success. You would have an example project with this feature implemented as it would greatly facilitate my understanding.

This library is fantastic and making it work with inappbrowser / ionic would be wonderful. Apparently, you were the only person who achieved such a feat. If possible, share an example of implementation with the community.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants