xamarin / Xamarin.Auth

Xamarin.Auth
Apache License 2.0
542 stars 350 forks source link

Android 1.3.2.2 GetUI() #147

Closed ajpinedam closed 6 years ago

ajpinedam commented 7 years ago

Xamarin.Auth version 1.3.2.2

GetUI method is returning object.

Previous code:

var intent = auth.GetUI(context);
StartActivity(intent);

not working anymore.

jgoelten commented 7 years ago

I'm facing the same issue, a cast throws an exception. The returned object from GetUI(...) is of type MainActivity.

DFerenczi commented 7 years ago

Tried casting it to an intent?

StartActivity((Intent)auth.GetUI(context));

jgoelten commented 7 years ago

that throws an exception in my case (will provide further exception information later on)

Jake-Barrow commented 7 years ago

I'm also having problems with this, no exception is thrown but it doesn't do anything.

I tried the following code : StartActivity(((Android.Support.CustomTabs.CustomTabsIntent)ui).Intent); And that gives me a context menu to select the app to open it in, but it gives me a strange selection of apps, none of which is a Browser

sergioacortes commented 7 years ago

I am having the same problem, does anyone know or have a solution for this issue?

Kind regards.

Sergio

ammarMheir commented 7 years ago

Seems that the only solution for me was to downgrade back to 1.3.2.1.

moljac commented 7 years ago

Breaking change for upcoming features!

See (for more details): https://github.com/xamarin/Xamarin.Auth/issues/150

This is breaking change for new feature using Native UI instead of embedded web browser controls/widgets/views.

-------------------- -------------------- ------------------------------ OS Old/deprecated new/secure
embedded browsers Native UI
-------------------- -------------------- ------------------------------
Android WebView [Chrome] Custom Tabs
iOS UIWebView SFSafariViewController
-------------------- -------------------- ------------------------------

New flag in Authenticator class

IsUsingNativeUI

https://github.com/xamarin/Xamarin.Auth/blob/portable-bait-and-switch/samples/Traditional.Standard/references01projects/Providers/Xamarin.Auth.Sample.XamarinIOS/TestProvidersController.cs#L92-L103

https://github.com/xamarin/Xamarin.Auth/blob/portable-bait-and-switch/samples/Traditional.Standard/references01projects/Providers/Xamarin.Auth.Sample.XamarinAndroid/MainActivity.cs#L83-L96

Android

System.Object ui_intent_as_object = auth.GetUI(this);
if (auth.IsUsingNativeUI == true)
{
    // Add Android.Support.CustomTabs package 
    global::Android.Support.CustomTabs.CustomTabsIntent cti = null;
    cti = (global::Android.Support.CustomTabs.CustomTabsIntent)ui_intent_as_object;

}
else 
{
    global::Android.Content.Intent i = null;
    i = (global::Android.Content.Intent)ui_intent_as_object;
    StartActivity(i);
}

IOS

System.Object ui_controller_as_object = auth.GetUI();
if (auth.IsUsingNativeUI == true)
{
    SafariServices.SFSafariViewController c = null;
    c = (SafariServices.SFSafariViewController) ui_controller_as_object;
    PresentViewController (c, true, null);
}
else
{
    UIViewController c = (UIViewController)ui_controller_as_object;
    PresentViewController (c, true, null);
}

See

Jake-Barrow commented 7 years ago

Hi moljac,

It now compiles and runs, however the Navigate event and the Auth.Completed event are never fired. Could we get some documentation on how to use this new functionality in Xamarin.Auth correctly?

moljac commented 7 years ago

@Jake-Barrow

I'm working on 3 things right now:

  1. Auth.Completed, Auth.Error, Auth.Canceled for UWP, WinRT (both Windows Store and Windows Phone)
  2. Auth.Completed for Chrome Custom Tabs (Android) and this is PITA beast. I'm using experience of other guys that did hit that problem (navigation with Custom Tabs) earlier.
  3. Adding support for custom uri schemas for all platforms (missing from Xamarin.Auth inception)
  4. documenting biggest changes

Now few questions:

  1. Do you use 1.3.2.4 version (latest) of the nuget?
  2. Did you set Native UI in OAuth2Authenticator ctor?
    If not, then this is a bug, because by default Xamarin.Auth should still use old embedded browser approach (UIWebView and WebView). The switch to Native UI (SFSafariViewController and Custom Tabs) is planned for mid April.
moljac commented 7 years ago

The native UI will present providers' login page (FB, Google, ...) but I need to implement API for [Chrome] Custom tabs and SFSafariViewController how to raise events with minimal API breakage (from current API).

I hated myself for this "object" breaking change. I redesigned this part of APIs twice or three times, because CustomTabsIntent and CustomTabsActivity inherit directly from Java.Lang.Object and not Intent or Activity... So, switch to object was minimal damage solution.

Jake-Barrow commented 7 years ago

@moljac

Yes I was using 1.3.2.4 and yes I did set NativeUI = true in OAuth2Authenticator ctor. So are you basically saying this is partially implemented functionality? If so that's fine, it just seemed like it was done and I assumed I was using the package incorrectly :)

moljac commented 7 years ago

@Jake-Barrow

Yes, this is currently partially implemented functionality - you can navigate, but not subscribe to events.

I would like more than you guys to be done with this functionality, but Xamarin.Auth has grown a lot.

1.3.2.5 was pushed 10-15 minutes ago. UWP events implemented (OnCompleted, OnError,...)

https://www.nuget.org/packages/Xamarin.Auth/1.3.2.5

Brosten commented 7 years ago

So, following this guide doesn't work at all now? or? https://developer.xamarin.com/guides/xamarin-forms/cloud-services/authentication/oauth/

JakubHolovsky commented 7 years ago

cti = (global::Android.Support.CustomTabs.CustomTabsIntent)ui_intent_as_object;

what am I supposed to do with the cti object afterwards? as the code stands right now it does nothing, do I launch some Url?

moljac commented 7 years ago

@JakubHolovsky

OK. Did you set IsUsingNativeUI in the constructor? If yes why? The sample above with if/then was simply to illustrate upcoming feature.

Yes you or Xamarin.Auth (preferably) will launch some uri.

https://github.com/xamarin/Xamarin.Auth/blob/portable-bait-and-switch/samples/Traditional.Standard/references01projects/Providers/Xamarin.Auth.Sample.XamarinAndroid/MainActivity.cs#L134-L145

moljac commented 7 years ago

@Brosten

  1. Guides are intended to guide you through implementing some functionality
    in this case OAuth authentication. Did you try to implement OAuth according to
    that guide?
  2. If answer to 1 is No, then where does it say it does not work (at all)?

To help you with answers:

  1. You would get one compile error with the code from guide in link you provided.
    It works with API changes!

Original code from that link:

    rootViewController.PresentViewController(authenticator.GetUI(), true, null);

New API

    rootViewController.PresentViewController( (UIViewController) authenticator.GetUI(), true, null);

So only cast will be needed.

  1. Jake-Barrow was using new API by turning on boolean switch in constructor of OAuth2Authenticator.
JakubHolovsky commented 7 years ago

@moljac thank you for your reaction, however I am a bit confused:

What I don't get if this is an "upcoming" feature, why is it in published nuget package when it basically does nothing ("work in progress" - does it work? does it not work? should we be using it?)?

https://github.com/xamarin/Xamarin.Auth/blob/portable-bait-and-switch/samples/Traditional.Standard/references01projects/Providers/Xamarin.Auth.Sample.XamarinAndroid/MainActivity.cs#L75-L75

This line says

// Default - false

but if I have a look at the constructor: public OAuth1Authenticator(string consumerKey, string consumerSecret, Uri requestTokenUrl, Uri authorizeUrl, Uri accessTokenUrl, Uri callbackUrl, GetUsernameAsyncFunc getUsernameAsync = null, bool isUsingNativeUI = true)

it is "true" by default, is it referring to some other default then?

Brosten commented 7 years ago

@moljac Sorry, I didn't tell... I'm on Android. Not iOS. I'm stuck in AndroidOAuthLoginPresenter, the "Login" method. I do the "GetUI()", returning a CustomTabsIntent. Trying to pass this intent to StartActivity() doesn't work. Nor does passing the "Intent"-property of the CustomTabsIntent.

Can't really see where to go from here. Or should i not use the method described in my link above, using the OAuthLoginPresenter?

Jake-Barrow commented 7 years ago

@JakubHolovsky @Brosten The functionality was accidentally committed to the Nuget package, so it is unfinished code published. If you follow the current documentation as normal, but change the constructor of the OAuthAuthenticator to have isUsingNativeUI = false it will follow the old functionality as normal.

There are some slight changes in the fact that GetUI() now returns an object. So follow the code here to use the old functionality for Authentication UI

Android:

global::Android.Content.Intent i = null;
i = (global::Android.Content.Intent)ui_intent_as_object;
StartActivity(i);

iOS:

UIViewController c = (UIViewController)ui_controller_as_object;
PresentViewController (c, true, null);
Jake-Barrow commented 7 years ago

The other option is to downgrade the Xamarin.Auth package to 1.3.2.1 until the new functionality is finished so you can use the new NativeUI options

moljac commented 7 years ago

@JakubHolovsky

You re right fixed. The default was true. Sorry. Just to fix "path to long" on Windows bot on our CI servers and 1.3.5.6 will be published

JakubHolovsky commented 7 years ago

@Jake-Barrow @moljac thanks, even though I guessed that it was incorrect and passed false in the constructor (basically followed the example) none of the Error / Completed / BrowsingCompleted events were being triggered (was using 1.3.2.1 version before). Reverted back to 1.3.2.1 for now.

moljac commented 7 years ago

@Jake-Barrow The "only" accident was - wrongly set variable.

Nuget is published through Ci servers and setting up everything (new lanes/jobs) for alpha/beta testing was too much trouble. I decided to push silently those new features so I can test them.

As soon as I fix building Windows bits 1.5.2.6 will be published

moljac commented 7 years ago

@JakubHolovsky

none of the Error / Completed / BrowsingCompleted events were being triggered

This was for 1.3.2.5?

I will separate my sample projects that use nuget dependencies into separate repo. They have 16-17 OAuth providers. Then you can test them with your API keys.

I retested Android and iOS samples yesterday with nuget 1.3.2.5 and was able to get auth_token. On Android there are Error events raised for localhost and 127.0.0.1 redirect_urls. IOS code was cleaned up last week, so I catch exception and parse url properly. Android tries to open it.

JakubHolovsky commented 7 years ago

@moljac yep, correct, that was for 1.3.2.5. It did re-direct me to Twitter login page, I logged in successfully and then nothing was triggered (my URLs remained the same as I as using for version 1.3.2.1).

moljac commented 7 years ago

Twitter is OAuth1?

I have to check that inheritance chain. I am (actually we are - 2 interns testing) are mostly wokring on OAuth2.

JakubHolovsky commented 7 years ago

@moljac yep, correct OAuth1

Brosten commented 7 years ago

@moljac Ahh, setting the bool to false solved my problem.... Thanks for your fast feedback

moljac commented 7 years ago

@Brosten You re welcome (BTW no need to thank me)

I managed to compile 1.3.2.6 on CI server, just few tests and then it will be like it used to be.

For next few weeks (I hope week or two depending on other stuff) you will not need this boolean set to false, just continue use old constructors.

Brosten commented 7 years ago

Hmm, just got stuck on my next step: // UserInfoUrl = https://www.googleapis.com/oauth2/v2/userinfo var request = new OAuth2Request ("GET", new Uri (Constants.UserInfoUrl), null, e.Account); var response = await request.GetResponseAsync ();

Doing that returns "Error authenticating: invalid_grant". Any idéas?

moljac commented 7 years ago

@Brosten

Is this new OAuth app? Read: did you create this app with API key etc on google portal recently?

Brosten commented 7 years ago

@moljac Well, created it a mounth or two ago.

moljac commented 7 years ago

@Brosten OK. This is why new API must be implemented. Current (OLD) API uses "embedded" WebBrowsers (WebView and UIWebView) and this is considered security hole. So, they banned all apps created after 2016-10-27 (27 - not sure, 2X where X=something) and this is why you get invalid _grant. Older apps will work until 2017-04-20 and then all apps using embedded browsers will not be able to use google as OAuth provider.

Currently I use old apps and new created for Facebook (they have posibility to control usage of embedded browser on their backend) and github for explicit flow.

I have 16-17 providers, but re-tested most of them like week or two ago. most of them worked.

JakubHolovsky commented 7 years ago

I tried version 1.3.2.6 and even though the default for OAuth1Authenticator isUsingNativeUI is false it still returns OAuth1Authenticator that has isUsingNativeUI true.

I believe it's because of https://github.com/xamarin/Xamarin.Auth/blob/portable-bait-and-switch/source/Xamarin.Auth.LinkSource/UINativeNonIntegratedBrowsers/WebAuthenticator.Native.cs

which is using "true" for it's default value while OAuth1Authenticator constructor (even though it's false) is not using the value from constructor anywhere.

shayo commented 7 years ago

I'm stuck on this issue as well. Can anyone help? I'm using 1.3.2.6 and trying to get Twitter to work. It crashes on the Start Activity with invalid cast. How can this be fixed? The "IsUsingNativeUI" Doesn't appear to be a member of Authenticator.

moljac commented 7 years ago

@shayo

See above Jake-Barrow explained in short what I explained 2 weeks ago - you need to cast.

Android:

global::Android.Content.Intent i = null;
i = (global::Android.Content.Intent)ui_intent_as_object;
StartActivity(i);

iOS:

UIViewController c = (UIViewController)ui_controller_as_object; PresentViewController (c, true, null);

In the future Xamarin.Auth GetUI() will return CustomTabs which derive from Java.Lang.Object and it was least-damage breaking change to return System.Object and leave (force) user to cast to proper class.

HTH

shayo commented 7 years ago

@moljac . It only works partially (in Android). It does work for OAuth2 (say, facebook), but crashing on that cast when trying Twitter (I believe it is OAuth1). [MonoDroid] System.InvalidCastException: Specified cast is not valid. [MonoDroid] at ClimbTag.Droid.OAuthLoginPresenter.Login (Xamarin.Auth.Authenticator authenticator) [0x0000d] in :0

shayo commented 7 years ago

OK. Clean & rebuild solved it, but I sill don't get the callback. It just continues to show the twitter web page.

shayo commented 7 years ago

To summarize - problem on Android: 1.3.2.6 - crashing on casting from OAuth1 1.3.2.1 - no crash, but no callbacks for Oauth1.

Can someone help?

shayo commented 7 years ago

Just saw that 1.3.2.7 is out. Good news - Oauth1 doesn't crash on casting anymore. Callbacks are still not working.

ads90 commented 7 years ago

for who facing casing issue in ios set auth.IsUsingNativeUI = true;

moljac commented 7 years ago

@ads What is "casing issue"?

ads90 commented 7 years ago

when instantiating the OAuth1Authenticator and calling auth.GetUI() app is crash and gives can't cast exception but when setting IsUsingNativeUI to true its working

newky2k commented 6 years ago

Can you try this with the latest version of Xamarin.Auth please.

Regards

newky2k commented 6 years ago

no response after two weeks