private void StartGoogleAuth(object sender, EventArgs e) { Xamarin.Auth.OAuth2Authenticator authentificator = GoogleAuthenticator.GetAuthenticator(); UIViewController viewController = authentificator.GetUI(); PresentViewController(viewController, true, null); }
private void StartFacebookAuth(object sender, EventArgs e) { _facebookAuthenticator = new FacebookAuthenticator(Configuration.ClientId, Configuration.Scope, ViewModel); Xamarin.Auth.OAuth2Authenticator authenticator = _facebookAuthenticator.GetAuthenticator(); UIViewController view = authenticator.GetUI(); PresentViewController(view, true, null); }
private void Button_Login_Fitbit_Clicked(object sender, EventArgs e) { authenticator = new Xamarin.Auth.OAuth2Authenticator ( /* * clientId: "185391188679-9pa23l08ein4m4nmqccr9jm01udf3oup.apps.googleusercontent.com", * scope: "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.login", * authorizeUrl: new Uri("https://accounts.google.com/o/oauth2/auth"), * redirectUrl: new Uri * ( * "comauthenticationapp://localhost" * //"com.authentication.app://localhost" * //"com-authentication-app://localhost" * ), */ clientId: new Func <string> ( () => { string retval_client_id = "oops something is wrong!"; // some people are sending the same AppID for google and other providers // not sure, but google (and others) might check AppID for Native/Installed apps // Android and iOS against UserAgent in request from // CustomTabs and SFSafariViewContorller // TODO: send deliberately wrong AppID and note behaviour for the future // fitbit does not care - server side setup is quite liberal switch (Xamarin.Forms.Device.RuntimePlatform) { case "iOS": retval_client_id = "228CVW"; break; case "Android": retval_client_id = "228CVW"; break; } return(retval_client_id); } ).Invoke(), authorizeUrl: new Uri("https://www.fitbit.com/oauth2/authorize"), redirectUrl: new Uri("xamarin-auth://localhost"), scope: "profile", getUsernameAsync: null, isUsingNativeUI: false ) { AllowCancel = true, }; NavigateLoginPage(); return; }
private void OnFacebookLoginButtonClicked(object sender, EventArgs e) { _authFacebook = new FacebookAuthenticator(Configuration.ClientId, Configuration.Scope, ViewModel); Xamarin.Auth.OAuth2Authenticator authenticator = _authFacebook.GetAuthenticator(); Intent intent = authenticator.GetUI(Activity); StartActivity(intent); }
public static void Initilize(string clientId) { if (clientId.IsEmpty()) { Log.For(typeof(Google)).Error("Please set the ClientId by calling Initilize method first!"); return; } Auth = new Xamarin.Auth.OAuth2Authenticator( clientId, "", "openid profile email", new Uri(AUTH_END_POINT), new Uri($"com.googleusercontent.apps.{clientId.Remove(".apps.googleusercontent.com")}:/oauth2redirect"), new Uri(TOKEN_END_POINT), null, true) { AllowCancel = true }; Auth.Completed += async(s, args) => { if (args.IsAuthenticated) { var request = new Xamarin.Auth.OAuth2Request("GET", new Uri(USER_INFO_END_POINT), null, args.Account); var response = await request.GetResponseAsync(); var accountStr = await response.GetResponseTextAsync(); var account = JsonConvert.DeserializeObject <JObject>(accountStr); await UserSignedIn.Raise(new Google.User { FamilyName = account["family_name"].Value <string>(), GivenName = account["given_name"].Value <string>(), Email = account["email"].Value <string>(), Name = account["name"].Value <string>(), Id = account["sub"].Value <string>(), Picture = account["picture"].Value <string>(), Token = args.Account.Properties["id_token"] ?? "" }); } }; }
// IOAuthAuthorizeHandler.AuthorizeAsync implementation. public Task <IDictionary <string, string> > AuthorizeAsync(Uri serviceUri, Uri authorizeUri, Uri callbackUri) { // If the TaskCompletionSource is not null, authorization is in progress. if (_taskCompletionSource != null) { // Allow only one authorization process at a time. throw new Exception(); } // Create a task completion source. _taskCompletionSource = new TaskCompletionSource <IDictionary <string, string> >(); // Create a new Xamarin.Auth.OAuth2Authenticator using the information passed in. Xamarin.Auth.OAuth2Authenticator authenticator = new Xamarin.Auth.OAuth2Authenticator( clientId: AppClientId, scope: "", authorizeUrl: authorizeUri, redirectUrl: callbackUri) { // Allow the user to cancel the OAuth attempt. AllowCancel = true }; // Define a handler for the OAuth2Authenticator.Completed event. authenticator.Completed += (sender, authArgs) => { try { // Check if the user is authenticated. if (authArgs.IsAuthenticated) { // If authorization was successful, get the user's account. Xamarin.Auth.Account authenticatedAccount = authArgs.Account; // Set the result (Credential) for the TaskCompletionSource. _taskCompletionSource.SetResult(authenticatedAccount.Properties); } } catch (Exception ex) { // If authentication failed, set the exception on the TaskCompletionSource. _taskCompletionSource.SetException(ex); } finally { // End the OAuth login activity. this.FinishActivity(99); } }; // If an error was encountered when authenticating, set the exception on the TaskCompletionSource. authenticator.Error += (sender, errArgs) => { // If the user cancels, the Error event is raised but there is no exception ... best to check first. if (errArgs.Exception != null) { _taskCompletionSource.SetException(errArgs.Exception); } else { // Login canceled: end the OAuth login activity. if (_taskCompletionSource != null) { _taskCompletionSource.TrySetCanceled(); this.FinishActivity(99); } } }; // Present the OAuth UI (Activity) so the user can enter user name and password. Intent intent = authenticator.GetUI(this); this.StartActivityForResult(intent, 99); // Return completion source task so the caller can await completion. return(_taskCompletionSource.Task); }
// IOAuthAuthorizeHandler.AuthorizeAsync implementation public Task <IDictionary <string, string> > AuthorizeAsync(Uri serviceUri, Uri authorizeUri, Uri callbackUri) { // If the TaskCompletionSource is not null, authorization may already be in progress and should be cancelled if (_taskCompletionSource != null) { // Try to cancel any existing authentication task _taskCompletionSource.TrySetCanceled(); } // Create a task completion source _taskCompletionSource = new TaskCompletionSource <IDictionary <string, string> >(); // Create a new Xamarin.Auth.OAuth2Authenticator using the information passed in Xamarin.Auth.OAuth2Authenticator authenticator = new Xamarin.Auth.OAuth2Authenticator( clientId: AppClientId, scope: "", authorizeUrl: authorizeUri, redirectUrl: callbackUri) { ShowErrors = false }; // Allow the user to cancel the OAuth attempt authenticator.AllowCancel = true; // Define a handler for the OAuth2Authenticator.Completed event authenticator.Completed += (sender, authArgs) => { try { // Check if the user is authenticated if (authArgs.IsAuthenticated) { // If authorization was successful, get the user's account Xamarin.Auth.Account authenticatedAccount = authArgs.Account; // Set the result (Credential) for the TaskCompletionSource _taskCompletionSource.SetResult(authenticatedAccount.Properties); } } catch (Exception ex) { // If authentication failed, set the exception on the TaskCompletionSource _taskCompletionSource.TrySetException(ex); // Cancel authentication authenticator.OnCancelled(); } finally { // Dismiss the OAuth login FinishActivity(99); } }; // If an error was encountered when authenticating, set the exception on the TaskCompletionSource authenticator.Error += (sndr, errArgs) => { // If the user cancels, the Error event is raised but there is no exception ... best to check first if (errArgs.Exception != null) { _taskCompletionSource.TrySetException(errArgs.Exception); } else { // Login canceled: dismiss the OAuth login if (_taskCompletionSource != null) { _taskCompletionSource.TrySetCanceled(); FinishActivity(99); } } // Cancel authentication authenticator.OnCancelled(); }; // Present the OAuth UI so the user can enter user name and password var intent = authenticator.GetUI(this); StartActivityForResult(intent, 99); // Return completion source task so the caller can await completion return(_taskCompletionSource.Task); }
// IOAuthAuthorizeHandler.AuthorizeAsync implementation public Task <IDictionary <string, string> > AuthorizeAsync(Uri serviceUri, Uri authorizeUri, Uri callbackUri) { // If the TaskCompletionSource is not null, authorization is in progress if (_taskCompletionSource != null) { // Allow only one authorization process at a time throw new Exception(); } // Create a task completion source _taskCompletionSource = new TaskCompletionSource <IDictionary <string, string> >(); // Create a new Xamarin.Auth.OAuth2Authenticator using the information passed in Xamarin.Auth.OAuth2Authenticator authenticator = new Xamarin.Auth.OAuth2Authenticator( clientId: AppClientId, scope: "", authorizeUrl: authorizeUri, redirectUrl: callbackUri); // Allow the user to cancel the OAuth attempt authenticator.AllowCancel = true; // Define a handler for the OAuth2Authenticator.Completed event authenticator.Completed += (sender, authArgs) => { try { // Check if the user is authenticated if (authArgs.IsAuthenticated) { // If authorization was successful, get the user's account Xamarin.Auth.Account authenticatedAccount = authArgs.Account; // Set the result (Credential) for the TaskCompletionSource _taskCompletionSource.SetResult(authenticatedAccount.Properties); } } catch (Exception ex) { // If authentication failed, set the exception on the TaskCompletionSource _taskCompletionSource.SetException(ex); } finally { // Dismiss the OAuth login this.DismissViewController(true, null); } }; // If an error was encountered when authenticating, set the exception on the TaskCompletionSource authenticator.Error += (sndr, errArgs) => { // If the user cancels, the Error event is raised but there is no exception ... best to check first if (errArgs.Exception != null) { _taskCompletionSource.SetException(errArgs.Exception); } else { // Login canceled: dismiss the OAuth login if (_taskCompletionSource != null) { _taskCompletionSource.TrySetCanceled(); this.DismissViewController(true, null); } } }; // Present the OAuth UI so the user can enter user name and password InvokeOnMainThread(() => { this.PresentViewController(authenticator.GetUI(), true, null); }); // Return completion source task so the caller can await completion return(_taskCompletionSource != null ? _taskCompletionSource.Task : null); }
public void StartActivityInAndroid(Xamarin.Auth.OAuth2Authenticator authenticator) { var intent = authenticator.GetUI(MainActivity.mainactivity); Forms.Context.StartActivity(intent); }
// IOAuthAuthorizeHandler.AuthorizeAsync implementation public Task<IDictionary<string, string>> AuthorizeAsync(Uri serviceUri, Uri authorizeUri, Uri callbackUri) { // If the TaskCompletionSource is not null, authorization is in progress if (_taskCompletionSource != null) { // Allow only one authorization process at a time throw new Exception(); } // Create a task completion source _taskCompletionSource = new TaskCompletionSource<IDictionary<string, string>>(); // Create a new Xamarin.Auth.OAuth2Authenticator using the information passed in Xamarin.Auth.OAuth2Authenticator authenticator = new Xamarin.Auth.OAuth2Authenticator( clientId: AppClientId, scope: "", authorizeUrl: authorizeUri, redirectUrl: callbackUri); // Allow the user to cancel the OAuth attempt authenticator.AllowCancel = true; // Define a handler for the OAuth2Authenticator.Completed event authenticator.Completed += (sender, authArgs) => { try { // Check if the user is authenticated if (authArgs.IsAuthenticated) { // If authorization was successful, get the user's account Xamarin.Auth.Account authenticatedAccount = authArgs.Account; // Set the result (Credential) for the TaskCompletionSource _taskCompletionSource.SetResult(authenticatedAccount.Properties); } } catch (Exception ex) { // If authentication failed, set the exception on the TaskCompletionSource _taskCompletionSource.SetException(ex); } finally { // Dismiss the OAuth login this.FinishActivity(99); } }; // If an error was encountered when authenticating, set the exception on the TaskCompletionSource authenticator.Error += (sndr, errArgs) => { // If the user cancels, the Error event is raised but there is no exception ... best to check first if (errArgs.Exception != null) { _taskCompletionSource.SetException(errArgs.Exception); } else { // Login canceled: dismiss the OAuth login if (_taskCompletionSource != null) { _taskCompletionSource.TrySetCanceled(); this.FinishActivity(99); } } }; // Present the OAuth UI so the user can enter user name and password var intent = authenticator.GetUI(this); this.StartActivityForResult(intent, 99); // Return completion source task so the caller can await completion return _taskCompletionSource.Task; }
public static System.Threading.Tasks.Task <int> RequestRefreshTokenAsync(this Xamarin.Auth.OAuth2Authenticator authenticator, string refreshToken) { // Unable to resolve assembly 'Assembly(Name=System.Threading.Tasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)' referenced by 'Assembly(Name=Xamarin.Auth.Extensions, Version=1.5.0.0, Culture=neutral, PublicKeyToken=null, Location=./source/Extensions/Xamarin.Auth.Extensions.Portable/bin/Debug/Xamarin.Auth.Extensions.dll)'. return(default(System.Threading.Tasks.Task <int>)); }
public AuthService() { //var authenticator = new Xamarin.Auth.OAuth2Authenticator( //"778099304554-5l1bgb9aq3o34dg40ak3pgll1547q19c.apps.googleusercontent.com", //null, //"profile", //new Uri("https://accounts.google.com/o/oauth2/v2/auth"), //new Uri("com.seattlemafiaclub.SeattleMafiaClub:/oauth2redirect"), //new Uri("https://www.googleapis.com/oauth2/v4/token"), //null, //true); //authenticator.Completed += (object s, Xamarin.Auth.AuthenticatorCompletedEventArgs e) => { // if (e.IsAuthenticated) // { // // The user is authenticated // // Extract the OAuth token // var token = new GoogleOAuthToken // { // TokenType = // e.Account.Properties["token_type"], // AccessToken = // e.Account.Properties["access_token"] // }; // // Do something // } // else // { // // The user is not authenticated // } //}; //Xamarin.Auth.Account account = getFacebookToken(); //if (account != null) //{ // queryUserInfo(account); // return; //} var authenticator = new Xamarin.Auth.OAuth2Authenticator( clientId: "1965686263705363", scope: "email", authorizeUrl: new Uri("https://m.facebook.com/dialog/oauth/"), //redirectUrl: new Uri("http://com.seattlemafiaclub.SeattleMafiaClub/oauth2redirect")); redirectUrl: new Uri("http://www.facebook.com/connect/login_success.html")); System.Diagnostics.Debug.WriteLine("--- start auth"); authenticator.Completed += (object s, Xamarin.Auth.AuthenticatorCompletedEventArgs e) => { System.Diagnostics.Debug.WriteLine("--- auth completed:" + e.IsAuthenticated); if (e.IsAuthenticated) { setFacebookToken(e.Account); // Do something //queryUserInfo(e.Account, (bool obj) => OnCompletedListener(obj)); OnCompletedListener(true); } else { // The user is not authenticated OnCompletedListener(false); } }; authenticator.Error += (object s, Xamarin.Auth.AuthenticatorErrorEventArgs e) => { System.Diagnostics.Debug.WriteLine("--- auth error:" + e.Message); if (OnCompletedListener != null) { OnCompletedListener(false); } }; this.authenticator = authenticator; }