public JsonResult GetGoogleAuth()
        {
            GoogleAuthUtil _util     = new GoogleAuthUtil(_config);
            var            setupInfo = _util.GetScanCode();

            return(Json(setupInfo));
        }
Example #2
0
            protected internal override string doInBackground(params Void[] @params)
            {
                string token = null;

                try
                {
                    string scope = string.Format("oauth2:{0}", Scopes.PLUS_LOGIN);
                    token = GoogleAuthUtil.getToken(outerInstance, Plus.AccountApi.getAccountName(outerInstance.mGoogleApiClient), scope);
                }
                catch (IOException transientEx)
                {
                    /* Network or server error */
                    Log.e(TAG, "Error authenticating with Google: " + transientEx);
                    errorMessage = "Network error: " + transientEx.Message;
                }
                catch (UserRecoverableAuthException e)
                {
                    Log.w(TAG, "Recoverable Google OAuth error: " + e.ToString());
                    /* We probably need to ask for permissions, so start the intent if there is none pending */
                    if (!outerInstance.mGoogleIntentInProgress)
                    {
                        outerInstance.mGoogleIntentInProgress = true;
                        Intent recover = e.Intent;
                        startActivityForResult(recover, RC_GOOGLE_LOGIN);
                    }
                }
                catch (GoogleAuthException authEx)
                {
                    /* The call is not ever expected to succeed assuming you have already verified that
                     * Google Play services is installed. */
                    Log.e(TAG, "Error authenticating with Google: " + authEx.Message, authEx);
                    errorMessage = "Error authenticating with Google: " + authEx.Message;
                }
                return(token);
            }
        private void onGoogleSignInResult(Intent data)
        {
            lock (lockable)
            {
                var signInData = Auth.GoogleSignInApi.GetSignInResultFromIntent(data);
                if (!signInData.IsSuccess)
                {
                    loginSubject.OnError(new GoogleLoginException(signInData.Status.IsCanceled));
                    isLoggingIn = false;
                    return;
                }

                Task.Run(() =>
                {
                    try
                    {
                        var token = GoogleAuthUtil.GetToken(Application.Context, signInData.SignInAccount.Account, scope);
                        loginSubject.OnNext(token);
                        loginSubject.OnCompleted();
                    }
                    catch (Exception e)
                    {
                        loginSubject.OnError(e);
                    }
                    finally
                    {
                        isLoggingIn = false;
                    }
                });
            }
        }
Example #4
0
        public void OnAuthCompleted(GoogleSignInResult result)
        {
            GoogleUser googleUser = new GoogleUser();

            if (result.IsSuccess)
            {
                Task.Factory.StartNew(() => {
                    if (result.SignInAccount.Account == null)
                    {
                        MessagingCenter.Send <string>("access token bilgisi alınamadı", "NoInternet");
                    }
                    var accessToken        = GoogleAuthUtil.GetToken(Android.App.Application.Context, result.SignInAccount.Email, $"oauth2:{Scopes.Email} {Scopes.Profile}");
                    googleUser.AccessToken = accessToken;
                    MessagingCenter.Send <string>(accessToken, "googleAndroidAccessToken");
                });
                GoogleSignInAccount accountt = result.SignInAccount;
                googleUser.Email       = accountt.Email;
                googleUser.Name        = accountt.GivenName;
                googleUser.Surname     = accountt.FamilyName;
                googleUser.AccessToken = accountt.IdToken;
                //googleUser.Token = accountt.IdToken;

                googleUser.Picture = new Uri((accountt.PhotoUrl != null ? $"{accountt.PhotoUrl}" : $"https://autisticdating.net/imgs/profile-placeholder.jpg"));
                _onLoginComplete?.Invoke(googleUser, string.Empty);
            }
            else
            {
                _onLoginComplete?.Invoke(null, "An error occured!");
            }
        }
        public JsonResult ValidateExistingAuthCode(string code, string memberNumber)
        {
            DataAccess      _da   = new DataAccess();
            GoogleAuthModel model = _da.GetGoogleAuthDetails(memberNumber);
            GoogleAuthUtil  ob    = new GoogleAuthUtil(_config);

            return(Json(ob.ValidateCode(code, model.PassCodeGoogleSecret)));
        }
Example #6
0
            private void ClearGoogleToken(Context ctx, string token)
            {
                var log = ServiceContainer.Resolve <ILogger> ();

                ThreadPool.QueueUserWorkItem(delegate {
                    try {
                        GoogleAuthUtil.ClearToken(ctx, token);
                    } catch (Exception ex) {
                        log.Warning(LogTag, ex, "Failed to authenticate user with Google login.");
                    }
                });
            }
        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (requestCode == 1000 && resultCode == Result.Ok)
            {
                var     accountName    = data.GetStringExtra(AccountManager.KeyAccountName);
                Account accountDetails = new Account(accountName, GoogleAuthUtil.GoogleAccountType);

                //var accountId = GoogleAuthUtil.GetAccountId(this, accountName);
                var token = GoogleAuthUtil.GetToken(this, accountDetails, "oauth2:mail");//https://www.googleapis.com/auth/drive.file
            }

            if (requestCode == 500 && resultCode == Result.Ok)
            {
                GoogleSignInResult result = Auth.GoogleSignInApi.GetSignInResultFromIntent(data);
                GoogleManager.Drive(result.SignInAccount);
            }
        }
Example #8
0
        protected override void ProcessMvxIntentResult(MvxIntentResultEventArgs result)
        {
            base.ProcessMvxIntentResult(result);

            if (result.RequestCode != googleSignInResult)
            {
                return;
            }

            lock (lockable)
            {
                var signInData = Auth.GoogleSignInApi.GetSignInResultFromIntent(result.Data);
                if (signInData.IsSuccess)
                {
                    var activity = Mvx.Resolve <IMvxAndroidCurrentTopActivity>().Activity as FragmentActivity;

                    Task.Run(() =>
                    {
                        try
                        {
                            var token = GoogleAuthUtil.GetToken(activity, signInData.SignInAccount.Account, scope);
                            subject.OnNext(token);
                            subject.OnCompleted();
                        }
                        catch (Exception e)
                        {
                            subject.OnError(e);
                        }
                        finally
                        {
                            isLoggingIn = false;
                        }
                    });
                }
                else
                {
                    subject.OnError(new GoogleLoginException(signInData.Status.IsCanceled));
                    isLoggingIn = false;
                }
            }
        }
        protected override void ProcessMvxIntentResult(MvxIntentResultEventArgs result)
        {
            base.ProcessMvxIntentResult(result);

            if (result.RequestCode != googleSignInResult)
            {
                return;
            }

            lock (lockable)
            {
                var signInData = Auth.GoogleSignInApi.GetSignInResultFromIntent(result.Data);
                if (signInData.IsSuccess)
                {
                    Task.Run(() =>
                    {
                        try
                        {
                            var token = GoogleAuthUtil.GetToken(Application.Context, signInData.SignInAccount.Account, scope);
                            loginSubject.OnNext(token);
                            loginSubject.OnCompleted();
                        }
                        catch (Exception e)
                        {
                            loginSubject.OnError(e);
                        }
                        finally
                        {
                            isLoggingIn = false;
                        }
                    });
                }
                else
                {
                    loginSubject.OnError(new GoogleLoginException(signInData.Status.IsCanceled));
                    isLoggingIn = false;
                }
            }
        }
Example #10
0
            private async void StartAuth()
            {
                if (IsAuthenticating)
                {
                    return;
                }
                IsAuthenticating = true;

                LoginActivity activity;

                try {
                    var log         = ServiceContainer.Resolve <Logger> ();
                    var authManager = ServiceContainer.Resolve <AuthManager> ();
                    var ctx         = Activity;

                    // Workaround for Android linker bug which forgets to register JNI types
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/GoogleAuthException", typeof(GoogleAuthException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/GooglePlayServicesAvailabilityException", typeof(GooglePlayServicesAvailabilityException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/UserRecoverableAuthException", typeof(UserRecoverableAuthException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/UserRecoverableNotifiedException", typeof(UserRecoverableNotifiedException));

                    String token = null;
                    try {
                        token = await Task.Factory.StartNew(() => GoogleAuthUtil.GetToken(ctx, Email, GoogleOAuthScope));
                    } catch (GooglePlayServicesAvailabilityException exc) {
                        var dia = GooglePlayServicesUtil.GetErrorDialog(
                            exc.ConnectionStatusCode, ctx, GoogleAuthRequestCode);
                        dia.Show();
                    } catch (UserRecoverableAuthException exc) {
                        StartActivityForResult(exc.Intent, GoogleAuthRequestCode);
                    } catch (Java.IO.IOException exc) {
                        // Connectivity error.. nothing to do really?
                        log.Info(Tag, exc, "Failed to login with Google due to network issues.");
                    } catch (Exception exc) {
                        log.Error(Tag, exc, "Failed to get access token for '{0}'.", Email);
                    }

                    // Failed to get token
                    if (token == null)
                    {
                        return;
                    }

                    activity = Activity as LoginActivity;
                    if (activity != null && activity.CurrentMode == Mode.Signup)
                    {
                        // Signup with Google
                        var success = await authManager.SignupWithGoogle(token);

                        if (!success)
                        {
                            GoogleAuthUtil.InvalidateToken(ctx, token);

                            new SignupFailedDialogFragment().Show(FragmentManager, "invalid_credentials_dialog");
                        }
                    }
                    else
                    {
                        // Authenticate client
                        var success = await authManager.AuthenticateWithGoogle(token);

                        if (!success)
                        {
                            GoogleAuthUtil.InvalidateToken(ctx, token);

                            new NoAccountDialogFragment().Show(FragmentManager, "invalid_credentials_dialog");
                        }
                    }
                } finally {
                    IsAuthenticating = false;
                }

                // Clean up self:
                if (Activity != null)
                {
                    FragmentManager.BeginTransaction()
                    .Remove(this)
                    .Commit();
                }

                // Try to make the activity recheck auth status
                activity = Activity as LoginActivity;
                if (activity != null)
                {
                    activity.StartAuthActivity();
                }
            }
Example #11
0
            private async Task StartAuthAsync()
            {
                if (IsAuthenticating)
                {
                    return;
                }
                IsAuthenticating = true;

                try {
                    var log         = ServiceContainer.Resolve <ILogger> ();
                    var authManager = ServiceContainer.Resolve <AuthManager> ();
                    var ctx         = Activity;

                    // No point in trying to reauth when old authentication is still running.
                    if (authManager.IsAuthenticating)
                    {
                        return;
                    }

                    // Workaround for Android linker bug which forgets to register JNI types
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/GoogleAuthException", typeof(GoogleAuthException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/GooglePlayServicesAvailabilityException", typeof(GooglePlayServicesAvailabilityException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/UserRecoverableAuthException", typeof(UserRecoverableAuthException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/UserRecoverableNotifiedException", typeof(UserRecoverableNotifiedException));

                    String token = null;
                    try {
                        token = await Task.Factory.StartNew(() => GoogleAuthUtil.GetToken(ctx, Email, GoogleOAuthScope));
                    } catch (GooglePlayServicesAvailabilityException exc) {
                        var dia = GooglePlayServicesUtil.GetErrorDialog(
                            exc.ConnectionStatusCode, ctx, GoogleAuthRequestCode);
                        dia.Show();
                    } catch (UserRecoverableAuthException exc) {
                        StartActivityForResult(exc.Intent, GoogleAuthRequestCode);
                    } catch (Java.IO.IOException exc) {
                        // Connectivity error.. nothing to do really?
                        log.Info(LogTag, exc, "Failed to login with Google due to network issues.");
                    } catch (Exception exc) {
                        log.Error(LogTag, exc, "Failed to get access token for '{0}'.", Email);
                    }

                    // Failed to get token
                    if (token == null)
                    {
                        return;
                    }

                    try {
                        var authRes = await viewModel.TrySignupGoogleAsync(token);

                        if (authRes != AuthResult.Success)
                        {
                            ClearGoogleToken(ctx, token);
                            var dia = new ErrorDialogFragment(authRes);
                            dia.Show(FragmentManager, "auth_result_dialog");
                        }
                    } catch (InvalidOperationException ex) {
                        log.Info(LogTag, ex, "Failed to authenticate user with Google login.");
                    }
                } finally {
                    IsAuthenticating = false;
                }

                // Clean up self:
                if (Activity != null)
                {
                    FragmentManager.BeginTransaction()
                    .Remove(this)
                    .Commit();
                }
            }
        public JsonResult ValidateCode(string code)
        {
            GoogleAuthUtil ob = new GoogleAuthUtil(_config);

            return(Json(ob.ValidateCode(code)));
        }
Example #13
0
            private async Task StartAuthAsync()
            {
                LoginActivity activity;

                try {
                    var log         = ServiceContainer.Resolve <ILogger> ();
                    var authManager = ServiceContainer.Resolve <AuthManager> ();
                    var ctx         = Activity;

                    // No point in trying to reauth when old authentication is still running.
                    if (authManager.IsAuthenticating)
                    {
                        return;
                    }

                    // Workaround for Android linker bug which forgets to register JNI types
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/GoogleAuthException", typeof(GoogleAuthException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/GooglePlayServicesAvailabilityException", typeof(GooglePlayServicesAvailabilityException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/UserRecoverableAuthException", typeof(UserRecoverableAuthException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/UserRecoverableNotifiedException", typeof(UserRecoverableNotifiedException));

                    String token = null;
                    try {
                        var account = AccountManager.Get(ctx).GetAccounts()
                                      .Where(x => x.Type == GoogleAuthUtil.GoogleAccountType && x.Name == Email)
                                      .FirstOrDefault();

                        token = await Task.Factory.StartNew(() => GoogleAuthUtil.GetToken(ctx, account, GoogleOAuthScope));
                    } catch (GooglePlayServicesAvailabilityException exc) {
                        var dia = GooglePlayServicesUtil.GetErrorDialog(
                            exc.ConnectionStatusCode, ctx, GoogleAuthRequestCode);
                        dia.Show();
                    } catch (UserRecoverableAuthException exc) {
                        StartActivityForResult(exc.Intent, GoogleAuthRequestCode);
                    } catch (Java.IO.IOException exc) {
                        // Connectivity error.. nothing to do really?
                        log.Info(LogTag, exc, "Failed to login with Google due to network issues.");
                    } catch (Exception exc) {
                        log.Error(LogTag, exc, "Failed to get access token for '{0}'.", Email);
                    }

                    // Failed to get token
                    if (token == null)
                    {
                        return;
                    }

                    try {
                        activity = Activity as LoginActivity;
                        if (activity != null && activity.CurrentMode == Mode.Signup)
                        {
                            // Signup with Google
                            var authRes = await activity.SignupWithGoogle(token);

                            if (authRes != AuthResult.Success)
                            {
                                ClearGoogleToken(ctx, token);
                                activity.ShowAuthError(Email, authRes, Mode.Signup, true);
                            }
                        }
                        else
                        {
                            // Authenticate client
                            var authRes = await authManager.AuthenticateWithGoogleAsync(token);

                            if (authRes != AuthResult.Success)
                            {
                                ClearGoogleToken(ctx, token);
                                activity.ShowAuthError(Email, authRes, Mode.Login, true);
                            }
                        }
                    } catch (InvalidOperationException ex) {
                        log.Info(LogTag, ex, "Failed to authenticate user with Google login.");
                    }
                } catch (Exception e) {
                }

                // Clean up self:
                if (Activity != null)
                {
                    FragmentManager.BeginTransaction()
                    .Remove(this)
                    .Commit();
                }
            }