Ejemplo n.º 1
0
        async void OnAuthCompleted_Google(object sender, AuthenticatorCompletedEventArgs e)
        {
            var authenticator = sender as OAuth2Authenticator;

            if (authenticator != null)
            {
                authenticator.Completed -= OnAuthCompleted_Google;
                authenticator.Error     -= OnAuthError;
            }

            Elmah.XamarinForms.Authentication.GoogleUser googleUser = null;
            if (e.IsAuthenticated)
            {
                var account = await Framework.Xamariner.Helpers.SecureStorageAccountStoreHelper.FindAccountsForServiceAsync(Elmah.XamarinForms.Authentication.GoogleAuthenticationConstants.AppName);

                // If the user is authenticated, request their basic user data from Google
                // UserInfoUrl = https://www.googleapis.com/oauth2/v2/userinfo
                var request  = new OAuth2Request("GET", new Uri(Elmah.XamarinForms.Authentication.GoogleAuthenticationConstants.UserInfoUrl), null, e.Account);
                var response = await request.GetResponseAsync();

                if (response != null)
                {
                    // Deserialize the data and store it in the account store
                    // The users email address will be used to identify data in SimpleDB
                    string userJson = await response.GetResponseTextAsync();

                    googleUser = JsonConvert.DeserializeObject <Elmah.XamarinForms.Authentication.GoogleUser>(userJson);
                    var userTokenIdModel = new Framework.WebApi.UserTokenIdModel
                    {
                        AuthenticationProvider = Framework.Models.AuthenticationProvider.Google,
                        Name          = googleUser.Name,
                        FamilyName    = googleUser.FamilyName,
                        Email         = googleUser.Email,
                        Gender        = googleUser.Gender,
                        GivenName     = googleUser.GivenName,
                        Id            = googleUser.Id,
                        JwtToken      = e.Account.Properties["id_token"],
                        Picture       = googleUser.Picture,
                        VerifiedEmail = googleUser.VerifiedEmail
                    };

                    var authenticationApiClient = Elmah.MVVMLightViewModels.WebApiClientFactory.CreateAuthenticationApiClient();
                    var apiResponse             = await authenticationApiClient.SignInWithOAuth2(userTokenIdModel, Framework.Models.AuthenticationProvider.Google);
                }

                if (account != null)
                {
                    await Framework.Xamariner.Helpers.SecureStorageAccountStoreHelper.DeleteAsync(account.FirstOrDefault(), Elmah.XamarinForms.Authentication.GoogleAuthenticationConstants.AppName);
                }

                await Framework.Xamariner.Helpers.SecureStorageAccountStoreHelper.SaveAsync(e.Account, Elmah.XamarinForms.Authentication.GoogleAuthenticationConstants.AppName);
            }
        }
Ejemplo n.º 2
0
        public async Task <Elmah.MvcCore.Models.ApplicationUser> Authenticate(Framework.WebApi.UserTokenIdModel token)
        {
            //NB: These config files are loaded from the user secrets.
            var appToken = await GenerateAppAccessToken(_configuration["Facebook:AppId"], _configuration["Facebook:AppSecret"]);

            var isValid = await DebugUserAccessToken(appToken, token.JwtToken);

            if (isValid)
            {
                var user = await CreateOrGetUser(token);

                return(user);
            }

            throw new Exception("Invalid Token");
        }
Ejemplo n.º 3
0
        private async Task <Elmah.MvcCore.Models.ApplicationUser> CreateOrGetUser(Framework.WebApi.UserTokenIdModel userToken)
        {
            var user = await _userManager.FindByEmailAsync(userToken.Email);

            if (user == null)
            {
                var appUser = new Elmah.MvcCore.Models.ApplicationUser
                {
                    UserName = userToken.Email,
                    Email    = userToken.Email,
                };
                var identityUser = await _userManager.CreateAsync(appUser);

                return(appUser);
            }
            return(user);
        }
        public async Task <Framework.WebApi.AuthenticationResponse> Google([FromBody] Framework.WebApi.UserTokenIdModel model)
        {
            try
            {
                var user = await _googleAuthService.Authenticate(model);

                if (user == null)
                {
                    Debug.WriteLine("ApplicationUser not created: {0}", model.Email);
                    return(new Framework.WebApi.AuthenticationResponse {
                        Succeeded = false
                    });
                }

                /*
                 * if(!user.EntityID.HasValue)
                 * {
                 *  var service1 = _serviceProvider.GetRequiredService<NTierOnTime.WcfContracts.IEntityService>();
                 *
                 *  var response = await NTierOnTime.CoreCommonBLL.Helpers.EntityHelper.CreateNewEntity(service1, model.Email, _logger);
                 *
                 *  if (response.BusinessLogicLayerResponseStatus == Framework.Services.BusinessLogicLayerResponseStatus.MessageOK || response.BusinessLogicLayerResponseStatus == Framework.Services.BusinessLogicLayerResponseStatus.UIProcessReady)
                 *  {
                 *      var applicationUser = await _userManager.FindByEmailAsync(model.Email);
                 *      if (applicationUser != null)
                 *      {
                 *          applicationUser.EntityID = response.Message[0].EntityID;
                 *          await _userManager.UpdateAsync(applicationUser);
                 *      }
                 *  }
                 * }
                 */
                await _signInManager.SignInAsync(user, true);

                return(await GetAuthenticationResponse(user));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return(new Framework.WebApi.AuthenticationResponse {
                    Succeeded = false
                });
            }
        }
Ejemplo n.º 5
0
        public async Task <Framework.WebApi.AuthenticationResponse> SignInWithOAuth2(Framework.WebApi.UserTokenIdModel model, Framework.Models.AuthenticationProvider authProvider)
        {
            string url = GetHttpRequestUrl(authProvider.ToString());

            return(await PostCommon <Framework.WebApi.UserTokenIdModel, Framework.WebApi.AuthenticationResponse>(url, model));
        }
Ejemplo n.º 6
0
        public async Task <Elmah.MvcCore.Models.ApplicationUser> Authenticate(Framework.WebApi.UserTokenIdModel userTokenModel)
        {
            var payload = await GoogleJsonWebSignature.ValidateAsync(userTokenModel.JwtToken, new GoogleJsonWebSignature.ValidationSettings());

            return(await CreateOrGetUser(payload, userTokenModel));
        }