Ejemplo n.º 1
0
        public void TestSyncUsersWithExistingUser()
        {
            List<AppUser> users = CreateTestUsers();
            var mock = new Mock<IPersistanceFramework>();
            mock.Setup(p => p.CreateQueryContext<AppUser>()).Returns(users.AsQueryable());
            mock.Setup(p => p.SaveChanges());

            var claimedUser = new AppUser() { GoogleId = "testid1", FirstName="googleName" };
            AppUser syncedUser = (AppUser)new PrivateType(typeof(GoogleOAuth)).InvokeStatic("SyncUserWithHOAHome", claimedUser, mock.Object);
            //var syncedUser = GoogleOAuth_Accessor.SyncUserWithHOAHome(claimedUser, mock.Object);

            Assert.AreEqual("HoaName", syncedUser.FirstName);
            Assert.AreEqual("HoaName", users[0].FirstName);

            mock.VerifyAll();
        }
Ejemplo n.º 2
0
 public Identity(AppUser appUser)
 {
     Contract.Requires(appUser !=null);
     this.appUser = appUser;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Deprecated Method for adding a new object to the AppUsers EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToAppUsers(AppUser appUser)
 {
     base.AddObject("AppUsers", appUser);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Create a new AppUser object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="googleId">Initial value of the GoogleId property.</param>
 /// <param name="completedRegistration">Initial value of the CompletedRegistration property.</param>
 /// <param name="createdDate">Initial value of the CreatedDate property.</param>
 /// <param name="modifiedDate">Initial value of the ModifiedDate property.</param>
 /// <param name="displayName">Initial value of the DisplayName property.</param>
 public static AppUser CreateAppUser(global::System.Guid id, global::System.String googleId, global::System.Boolean completedRegistration, global::System.DateTime createdDate, global::System.DateTime modifiedDate, global::System.String displayName)
 {
     AppUser appUser = new AppUser();
     appUser.Id = id;
     appUser.GoogleId = googleId;
     appUser.CompletedRegistration = completedRegistration;
     appUser.CreatedDate = createdDate;
     appUser.ModifiedDate = modifiedDate;
     appUser.DisplayName = displayName;
     return appUser;
 }
Ejemplo n.º 5
0
        public static AppUser HandleAuthenticationResponse(IAuthenticationResponse response, IPersistanceFramework persistance)
        {
            Contract.Requires(response != null);
            Contract.Requires(persistance != null);

            var attibuteExtension = response.GetExtension<FetchResponse>();
            var oAuth = response.GetExtension<AuthorizationApprovedResponse>();
            Contract.Assume(MvcApplication.GoogleWebConsumer.TokenManager is IOpenIdOAuthTokenManager);
            AuthorizedTokenResponse accessToken = MvcApplication.GoogleWebConsumer.ProcessUserAuthorization(response);

            if (accessToken == null)
            {
                throw new ApplicationException("User did not authorize access through google");
            }

            State.GoogleAccessToken = accessToken.AccessToken;

            AppUser claimedUser = new AppUser();
            claimedUser.AccessToken = accessToken.AccessToken;
            Contract.Assume(!string.IsNullOrEmpty(claimedUser.AccessToken));
            claimedUser.AccessTokenSecret = MvcApplication.GoogleTokenManager.GetTokenSecret(accessToken.AccessToken);
            claimedUser.GoogleId = response.ClaimedIdentifier;
            claimedUser.Email = attibuteExtension.Attributes[WellKnownAttributes.Contact.Email].Values.First();
            claimedUser.FirstName = attibuteExtension.Attributes[WellKnownAttributes.Name.First].Values.First();
            claimedUser.LastName = attibuteExtension.Attributes[WellKnownAttributes.Name.Last].Values.First();

            AppUser appUser = SyncUserWithHOAHome(claimedUser, persistance);

            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,appUser.Id.ToString(), DateTime.Now,DateTime.Now.AddDays(10), false, appUser.ToCookieString(), FormsAuthentication.FormsCookiePath);
            System.Web.HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)));

            return appUser;
        }
Ejemplo n.º 6
0
        private static AppUser SyncUserWithHOAHome(AppUser claimedUser, IPersistanceFramework persistance)
        {
            Contract.Requires(claimedUser != null);
            Contract.Requires(persistance != null);
            var user = persistance.CreateQueryContext<AppUser>().Where(u => u.GoogleId == claimedUser.GoogleId).FirstOrDefault();
                if (user != null)
                {

                    user.LastLogin = DateTime.Now;
                    user.AccessToken = claimedUser.AccessToken;
                    user.AccessTokenSecret = claimedUser.AccessTokenSecret;
                    persistance.SaveChanges();
                    return user;
                }
                else
                {
                    claimedUser.LastLogin = DateTime.Now;
                    claimedUser.DisplayName = claimedUser.LastName + ", " + claimedUser.FirstName;
                    persistance.AttachNew(claimedUser);
                    persistance.SaveChanges();
                }

            return claimedUser;
        }