static async Task BeginAuthentication(WebAuthenticator authenticator) { try { var uri = (await authenticator.GetInitialUrl()); string redirectUrl = uri.GetParameter("redirect_uri"); var scheme = new Uri(redirectUrl).Scheme; if (!VerifyHasUrlScheme(scheme)) { authenticator.OnError($"Unable to redirect {redirectUrl}, Please add the Url Scheme to the info.plist"); return; } var url = new NSUrl(uri.AbsoluteUri); var opened = NSWorkspace.SharedWorkspace.OpenUrl(url); if (!opened) { authenticator.OnError("Error opening Safari"); } else { authenticators [scheme] = authenticator; } } catch (Exception ex) { authenticator.OnError(ex.Message); } }
static async Task BeginAuthentication(UIViewController presentingController, WebAuthenticator authenticator) { try { var uri = (await authenticator.GetInitialUrl()); string redirectUrl = uri.GetParameter("redirect_uri"); var scheme = new Uri(redirectUrl).Scheme; if (!VerifyHasUrlSchemeOrDoesntRequire(scheme)) { authenticator.OnError($"Unable to redirect {scheme}, Please add the Url Scheme to the info.plist"); return; } var url = new NSUrl(uri.AbsoluteUri); if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0)) { var AuthTask = new TaskCompletionSource <object>(); authenticators[scheme] = authenticator; var sf = new SFAuthenticationSession(url, scheme, (callbackUrl, Error) => { if (Error == null) { ResumeAuth(callbackUrl.AbsoluteString); AuthTask.SetResult(null); } else { AuthTask.SetException(new Exception($"SFAuthenticationSession Error: {Error.ToString()}")); } } ); sf.Start(); await AuthTask.Task; return; } if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0)) { var controller = new SFSafariViewController(url, false) { Delegate = new NativeSFSafariViewControllerDelegate(authenticator), }; authenticators [scheme] = authenticator; CurrentController = controller; await presentingController.PresentViewControllerAsync(controller, true); return; } var opened = UIApplication.SharedApplication.OpenUrl(url); if (!opened) { authenticator.OnError("Error opening Safari"); } else { authenticators [scheme] = authenticator; } } catch (Exception ex) { authenticator.OnError(ex.Message); } }
protected virtual async Task <OAuthAccount> GetAccountFromAuthCode(WebAuthenticator authenticator, string identifier) { var postData = await authenticator.GetTokenPostData(ClientSecret); if (string.IsNullOrWhiteSpace(TokenUrl)) { throw new Exception("Invalid TokenURL"); } var reply = await Client.PostAsync(TokenUrl, new FormUrlEncodedContent(postData)); var resp = await reply.Content.ReadAsStringAsync(); var result = Deserialize <OauthResponse>(resp); if (!string.IsNullOrEmpty(result.Error)) { throw new Exception(result.ErrorDescription); } var account = new OAuthAccount() { ExpiresIn = result.ExpiresIn, Created = DateTime.UtcNow, RefreshToken = result.RefreshToken, Scope = authenticator.Scope.ToArray(), TokenType = result.TokenType, Token = result.AccessToken, ClientId = ClientId, Identifier = identifier, }; return(account); }
protected override async Task <Account> PerformAuthenticate() { if (ScopesRequired && (Scopes?.Length ?? 0) == 0) { throw new Exception("Scopes must be set on the API or passed into Authenticate"); } var account = CurrentOAuthAccount ?? GetAccount <OAuthAccount> (Identifier); if (account != null && (!string.IsNullOrWhiteSpace(account.RefreshToken) || account.ExpiresIn <= 0)) { var valid = account.IsValid(); if (!valid || ForceRefresh) { if (!(await Ping(TokenUrl))) { return(account); } if (await RefreshAccount(account)) { account = CurrentOAuthAccount ?? GetAccount <OAuthAccount> (Identifier); } } if (account.IsValid()) { SaveAccount(account); CurrentAccount = account; return(account); } } authenticator = CreateAuthenticator(); authenticator.Cookies = account?.Cookies; await authenticator.PrepareAuthenticator(); if (CurrentShowAuthenticator != null) { CurrentShowAuthenticator(authenticator); } else { ShowAuthenticator(authenticator); } var token = await authenticator.GetAuthCode(); if (string.IsNullOrEmpty(token)) { throw new Exception("Null token"); } account = await GetAccountFromAuthCode(authenticator, Identifier); account.Identifier = Identifier; SaveAccount(account); CurrentAccount = account; return(account); }
protected override Task<OAuthAccount> GetAccountFromAuthCode (WebAuthenticator authenticator, string identifier) { var auth = authenticator as OAuthPasswordAuthenticator; var account = new OAuthAccount () { ExpiresIn = auth.Token.ExpiresIn, Created = DateTime.UtcNow, RefreshToken = auth.Token.RefreshToken, Scope = authenticator.Scope?.ToArray (), TokenType = auth.Token.TokenType, Token = auth.Token.AccessToken, ClientId = ClientId, Identifier = identifier, }; return Task.FromResult (account); }
public static void ShowAuthenticator(UIViewController presentingController, WebAuthenticator authenticator) { var urls = GetCFBundleURLSchemes(); if (!urls.Any()) { authenticator.OnError(CFBundleUrlError); return; } //TODO: validate the proper url is in there var invoker = new Foundation.NSObject(); invoker.BeginInvokeOnMainThread(async() => await BeginAuthentication(presentingController, authenticator)); }
protected override Task <OAuthAccount> GetAccountFromAuthCode(WebAuthenticator authenticator, string identifier) { var auth = authenticator as OAuthPasswordAuthenticator; var account = new OAuthAccount() { ExpiresIn = auth.Token.ExpiresIn, Created = DateTime.UtcNow, RefreshToken = auth.Token.RefreshToken, Scope = authenticator.Scope?.ToArray(), TokenType = auth.Token.TokenType, Token = auth.Token.AccessToken, ClientId = ClientId, Identifier = identifier, }; return(Task.FromResult(account)); }
public static void ShowAuthenticator(WebAuthenticator authenticator) { var invoker = new Foundation.NSObject(); invoker.BeginInvokeOnMainThread(() => { var window = UIKit.UIApplication.SharedApplication.KeyWindow; var root = window.RootViewController; if (root != null) { var current = root; while (current.PresentedViewController != null) { current = current.PresentedViewController; } ShowAuthenticator(current, authenticator); } }); }
public static void ShowAuthenticator(UIViewController presentingController, WebAuthenticator authenticator) { //ios 11 uses sfAuthenticationSession which doesn't require registered URL in info.plist if (!UIDevice.CurrentDevice.CheckSystemVersion(11, 0)) { var urls = GetCFBundleURLSchemes(); if (!urls.Any()) { authenticator.OnError(CFBundleUrlError); return; } //TODO: validate the proper url is in there } var invoker = new Foundation.NSObject(); invoker.BeginInvokeOnMainThread(async() => await BeginAuthentication(presentingController, authenticator)); }
protected virtual async Task <OAuthAccount> GetAccountFromAuthCode(WebAuthenticator authenticator, string identifier) { var postData = await authenticator.GetTokenPostData(ClientSecret); if (string.IsNullOrWhiteSpace(TokenUrl)) { throw new Exception("Invalid TokenURL"); } var message = new HttpRequestMessage(HttpMethod.Post, TokenUrl) { Content = new FormUrlEncodedContent(postData), Headers = { { "Accept", "application/json" } } }; var reply = await Client.SendAsync(message); var resp = await reply.Content.ReadAsStringAsync(); var result = Deserialize <OauthResponse> (resp); if (!string.IsNullOrEmpty(result?.Error)) { throw new Exception($"{result.Error} : {result.ErrorDescription}"); } reply.EnsureSuccessStatusCode(); var account = new OAuthAccount() { ExpiresIn = result.ExpiresIn, Created = DateTime.UtcNow, RefreshToken = result.RefreshToken, Scope = authenticator.Scope?.ToArray(), TokenType = result.TokenType, Token = result.AccessToken, ClientId = ClientId, Identifier = identifier, Cookies = authenticator.Cookies, IdToken = result.Id }; return(account); }
static async Task BeginAuthentication(WebAuthenticator authenticator) { try { var uri = (await authenticator.GetInitialUrl()); string redirectUrl = uri.GetParameter("redirect_uri"); var scheme = new Uri(redirectUrl).Scheme; var authSession = new CustomTabsAuthSession { Authenticator = authenticator, ParentActivity = activityLifecycleManager.CurrentActivity, }; authenticators[scheme] = authSession; authSession.CustomTabsActivityManager = new CustomTabsActivityManager(authSession.ParentActivity); authSession.CustomTabsActivityManager.CustomTabsServiceConnected += delegate { var builder = new CustomTabsIntent.Builder(authSession.CustomTabsActivityManager.Session) .SetShowTitle(true); var customTabsIntent = builder.Build(); customTabsIntent.Intent.AddFlags(Android.Content.ActivityFlags.SingleTop | ActivityFlags.NoHistory | ActivityFlags.NewTask); CustomTabsHelper.AddKeepAliveExtra(authSession.ParentActivity, customTabsIntent.Intent); customTabsIntent.LaunchUrl(authSession.ParentActivity, Android.Net.Uri.Parse(uri.AbsoluteUri)); }; if (!authSession.CustomTabsActivityManager.BindService()) { authenticator.OnError("CustomTabs not supported."); authenticators.Remove(scheme); } } catch (Exception ex) { authenticator.OnError(ex.Message); } }
static async Task BeginAuthentication(UIViewController presentingController, WebAuthenticator authenticator) { try { var uri = (await authenticator.GetInitialUrl()); string redirectUrl = uri.GetParameter("redirect_uri"); var scheme = new Uri(redirectUrl).Scheme; if (!VerifyHasUrlScheme(scheme)) { authenticator.OnError($"Unable to redirect {redirectUrl}, Please add the Url Scheme to the info.plist"); return; } var url = new NSUrl(uri.AbsoluteUri); if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0)) { var controller = new SFSafariViewController(url, false) { Delegate = new NativeSFSafariViewControllerDelegate(authenticator), }; authenticators [scheme] = authenticator; CurrentController = controller; await presentingController.PresentViewControllerAsync(controller, true); return; } var opened = UIApplication.SharedApplication.OpenUrl(url); if (!opened) { authenticator.OnError("Error opening Safari"); } else { authenticators [scheme] = authenticator; } } catch (Exception ex) { authenticator.OnError(ex.Message); } }
public static async void ShowAuthenticator(WebAuthenticator authenticator) { await BeginAuthentication(authenticator); }
public OAuthController(WebAuthenticator authenticator) { this.authenticator = authenticator; }
protected override async Task<Account> PerformAuthenticate() { if (ScopesRequired && (Scopes?.Length ?? 0) == 0) throw new Exception("Scopes must be set on the API or passed into Authenticate"); var account = CurrentOAuthAccount ?? GetAccount<OAuthAccount>(Identifier); if (account != null && (!string.IsNullOrWhiteSpace(account.RefreshToken) || account.ExpiresIn <= 0)) { var valid = account.IsValid(); if (!valid || ForceRefresh) { if (!(await Ping(TokenUrl))) return account; await RefreshAccount(account); } if (account.IsValid()) { SaveAccount(account); CurrentAccount = account; return account; } } authenticator = CreateAuthenticator(); authenticator.Cookies = account?.Cookies; if (CurrentShowAuthenticator != null) CurrentShowAuthenticator(authenticator); else ShowAuthenticator(authenticator); var token = await authenticator.GetAuthCode(); if (string.IsNullOrEmpty(token)) { throw new Exception("Null token"); } account = await GetAccountFromAuthCode(authenticator, Identifier); account.Identifier = Identifier; SaveAccount(account); CurrentAccount = account; return account; }
protected virtual async Task<OAuthAccount> GetAccountFromAuthCode(WebAuthenticator authenticator, string identifier) { var postData = await authenticator.GetTokenPostData(ClientSecret); if (string.IsNullOrWhiteSpace(TokenUrl)) throw new Exception("Invalid TokenURL"); var message = new HttpRequestMessage (HttpMethod.Post, TokenUrl) { Content = new FormUrlEncodedContent (postData), Headers = { {"Accept","application/json"} } }; var reply = await Client.SendAsync (message); var resp = await reply.Content.ReadAsStringAsync(); var result = Deserialize<OauthResponse>(resp); if (!string.IsNullOrEmpty(result?.Error)) throw new Exception(result.ErrorDescription); var account = new OAuthAccount () { ExpiresIn = result.ExpiresIn, Created = DateTime.UtcNow, RefreshToken = result.RefreshToken, Scope = authenticator.Scope?.ToArray (), TokenType = result.TokenType, Token = result.AccessToken, ClientId = ClientId, Identifier = identifier, Cookies = authenticator.Cookies, }; return account; }
public NativeSFSafariViewControllerDelegate(WebAuthenticator authenticator) { this.authenticator = new WeakReference(authenticator); }