/// <summary>
        ///     Initializes a new instance of the <see cref="CreateUpdateAuthorizationUserInfoAction" /> class.
        /// </summary>
        /// <param name="authorization">The authorization.</param>
        /// <param name="userInfo">The user info.</param>
        /// <param name="provider">The provider.</param>
        public CreateUpdateAuthorizationUserInfoAction(AuthorizationInformation authorization, UserInformation userInfo,
		                                               MembershipProviderBase provider)
            : base(provider)
        {
            this.authorization = authorization;
            this.userInfo = userInfo;
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="RefreshAccessTokenAction" /> class.
 /// </summary>
 /// <param name="user">The user.</param>
 /// <param name="membershipProvider">The membership provider.</param>
 public RefreshAccessTokenAction(UserInformation user, MembershipProviderBase membershipProvider)
     : base(membershipProvider)
 {
     this.user = user;
     this.clientId = this.Provider.GoogleClientId;
     this.clientSecret = this.Provider.GoogleClientSecret;
     this.authorization = this.user.Authorization;
 }
 /// <summary>
 /// Creates the authorization information.
 /// </summary>
 /// <param name="authorization">The authorization.</param>
 /// <returns></returns>
 public override bool CreateAuthorizationInformation(AuthorizationInformation authorization)
 {
     bool isCreated = false;
     using (this.dbContext = new BuildMotionDb(this.connectionStringName))
     {
         this.dbContext.AuthorizationStates.Add(authorization);
         int result = this.dbContext.SaveChanges();
         isCreated = result > 0;
     }
     return isCreated;
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="AuthorizationInformationIsValidRule" /> class.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="message">The message.</param>
 /// <param name="target">The target.</param>
 public AuthorizationInformationIsValidRule(string name, string message, AuthorizationInformation target)
     : base(name, message)
 {
     this.Rules.Add(new IsNotNullRule("AuthorizationInformationIsNotNull", "AuthorizationInformation cannot be null.", target));
     if (target != null)
     {
         this.Rules.Add(new StringIsNotEmptySpace("AccessTokenIsValid", "The AccessToken is not valid.", target.AccessToken));
         this.Rules.Add(new StringIsNotEmptySpace("RefreshTokenIsValid", "The RefreshToken token is not valid.", target.RefreshToken));
         this.Rules.Add(new StringIsNotEmptySpace("RefreshTokenIsValid", "The Email value is not valid.", target.Email));
     }
 }
 /// <summary>
 /// Updates the authorization information.
 /// </summary>
 /// <param name="authorization">The authorization.</param>
 /// <returns></returns>
 public override bool UpdateAuthorizationInformation(AuthorizationInformation authorization)
 {
     bool isUpdated = false;
     using (this.dbContext = new BuildMotionDb(this.connectionStringName))
     {
         this.dbContext.AuthorizationStates.Attach(authorization);
         this.dbContext.Entry(authorization).State = EntityState.Modified;
         int result = this.dbContext.SaveChanges();
         isUpdated = result > 0;
     }
     return isUpdated;
 }
 /// <summary>
 /// Creates the authorization information.
 /// </summary>
 /// <param name="authorization">The authorization.</param>
 public bool CreateAuthorizationInformation(AuthorizationInformation authorization)
 {
     return this.dataAccessContext.EntityFrameworkDataAccess.CreateAuthorizationInformation(authorization);
 }
        public void AttemptCreateUpdateAuthorizationUserInfoWithInvalidEmailAddress()
        {
            // test setup;
            UserInformation userInformation = new UserInformation("1234", "*****@*****.**", true, "ana", "valencia", "ana valencia", "buildmotion.com", string.Empty);
            AuthorizationInformation authorization = new AuthorizationInformation("abc", DateTime.Now.ToUniversalTime().AddHours(1), DateTime.Now.ToUniversalTime(), "xyz");
            authorization.Email = string.Empty; //remove this to fail the test;
            userInformation.Email = string.Empty;

            this.repositoryMock.Setup(r => r.RetrieveUserInformation(It.IsAny<string>()));
            this.repositoryMock.Setup(r => r.CreateUserInformation(It.IsAny<UserInformation>())).Returns(userInformation);
            this.repositoryMock.Setup(c => c.CreateAuthorizationInformation(It.IsAny<AuthorizationInformation>())).Returns(true);

            // test;
            var action = new CreateUpdateAuthorizationUserInfoAction(authorization, userInformation, this.providerMock.Object);
            action.Execute();
            UserInformation user = action.UserInformation;

            // Assertions;
            WriteValidationContextRuleResults(action.ValidationContext);
            Assert.IsFalse(action.ValidationContext.IsValid);
            Assert.IsNull(user);
        }
        public void CanUpdateUserAuthorizationInformation()
        {
            // test setup;
            UserInformation userInformation = new UserInformation("1234", "*****@*****.**", true, "ana", "valencia", "ana valencia", "buildmotion.com", string.Empty);
            AuthorizationInformation authorization = new AuthorizationInformation("abc", DateTime.Now.ToUniversalTime().AddHours(1), DateTime.Now.ToUniversalTime(), "xyz");
            authorization.Email = userInformation.Email; //remove this to fail the test;
            userInformation.Id = Guid.NewGuid();

            this.repositoryMock.Setup(r => r.RetrieveUserInformation(It.IsAny<string>())).Returns(userInformation);
            this.repositoryMock.Setup(u => u.UpdateUserInformation(It.IsAny<UserInformation>())).Returns(userInformation);
            this.repositoryMock.Setup(a => a.UpdateAuthorizationInformation(It.IsAny<AuthorizationInformation>())).Returns(true);

            // test;
            var action = new CreateUpdateAuthorizationUserInfoAction(authorization, userInformation, this.providerMock.Object);
            action.Execute();
            UserInformation user = action.UserInformation;

            // Assertions;
            WriteValidationContextRuleResults(action.ValidationContext);
            Assert.IsTrue(action.ValidationContext.IsValid);
            Assert.IsNotNull(user);
            Assert.AreNotEqual(Guid.Empty, user.Id);
            Assert.IsNotNullOrEmpty(user.Email);
        }
        private ActionResult OAuthCallback(string redirectUrl)
        {
            var authorizationState = GoogleClient.ProcessUserAuthorization(this.Request); // retrieve [AuthToken] using Request;
            if (authorizationState != null && !string.IsNullOrEmpty(authorizationState.AccessToken) && !string.IsNullOrEmpty(authorizationState.RefreshToken))
            {
                bool tokenIsValid = this.MembershipService.ValidateToken(authorizationState.AccessToken);

                if (tokenIsValid)
                {
                    UserInformation userInformation = this.MembershipService.RetrieveGoogleUserInformation(authorizationState.AccessToken);
                    if (this.MembershipService.UserDomainIsValid(userInformation.Domain))
                    {
                        AuthorizationInformation authInfo = new AuthorizationInformation
                        {
                            AccessToken = authorizationState.AccessToken,
                            AccessTokenExpirationUtc = authorizationState.AccessTokenExpirationUtc.GetValueOrDefault(),
                            AccessTokenIssueDateUtc = authorizationState.AccessTokenIssueDateUtc.GetValueOrDefault(),
                            Email = userInformation.Email,
                            RefreshToken = authorizationState.RefreshToken
                        };

                        UserInformation user = this.MembershipService.CreateUpdateAuthorizationUserInfo(authInfo, userInformation);
                        if(user != null)
                        {
                            this.CreateFormsAuthenticationTicket(user);
                        }
                        else
                        {
                            return UnableToValidateRedirectToAction();
                        }
                    }
                    else
                    {
                        return UnableToValidateRedirectToAction();
                    }
                }
            }
            else
            {
                return UnableToValidateRedirectToAction();
            }

            // If you get here; the user is validated; redirecto to speicified url;
            #region Handle the redirection; retrieve the redirect URL information for current request;
            string formsAuthenticationRedirectUrl = string.Empty;
            if (Session["ReturnUrl"] != null)
            {
                formsAuthenticationRedirectUrl = Session["ReturnUrl"].ToString();
            }
            string url = !string.IsNullOrEmpty(redirectUrl) ? redirectUrl : formsAuthenticationRedirectUrl;
            if (string.IsNullOrEmpty(url))
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return Redirect(url);
            }
            #endregion;
        }
        /// <summary>
        ///     Creates the update authorization user info.
        /// </summary>
        /// <param name="authorization">The authorization.</param>
        /// <param name="userInfo">The user info.</param>
        /// <returns></returns>
        public UserInformation CreateUpdateAuthorizationUserInfo(AuthorizationInformation authorization,
		                                                         UserInformation userInfo)
        {
            return this.provider.CreateUpdateAuthorizationUserInfo(authorization, userInfo);
        }
        /// <summary>
        ///     Creates the update authorization user info.
        /// </summary>
        /// <param name="authorization">The authorization.</param>
        /// <param name="userInfo">The user info.</param>
        /// <returns></returns>
        public override UserInformation CreateUpdateAuthorizationUserInfo(AuthorizationInformation authorization,
		                                                                  UserInformation userInfo)
        {
            UserInformation userInformation = null;
            CreateUpdateAuthorizationUserInfoAction action = new CreateUpdateAuthorizationUserInfoAction(authorization, userInfo,
                                                                                                         this);
            action.Execute();
            if (action.Result == ActionResult.Success)
            {
                userInformation = action.UserInformation;
            }
            return userInformation;
        }
 /// <summary>
 /// Creates the authorization information.
 /// </summary>
 /// <param name="authorization">The authorization.</param>
 public bool CreateAuthorizationInformation(AuthorizationInformation authorization)
 {
     return this.adaptor.CreateAuthorizationInformation(authorization);
 }
 /// <summary>
 /// Updates the authorization information.
 /// </summary>
 /// <param name="authorization">The authorization.</param>
 /// <returns></returns>
 public abstract bool UpdateAuthorizationInformation(AuthorizationInformation authorization);
        /// <summary>
        ///     Creates the update authorization user info.
        /// </summary>
        /// <param name="authorization">The authorization.</param>
        /// <param name="userInfo">The user info.</param>
        /// <returns></returns>
        public abstract UserInformation CreateUpdateAuthorizationUserInfo(AuthorizationInformation authorization,
		                                                                  UserInformation userInfo);