public UserInfo Login(string attributes, Guid sessionGuidToAuthenticate) { if (!Request.User.HasPermission(SystemPermissons.Manage)) throw new InsufficientPermissionsException("Only managers can authenticate sessions"); var attributesObject = JsonConvert.DeserializeObject<IDictionary<string, IList<string>>>(attributes); if (!attributesObject.ContainsKey("eduPersonTargetedID") || attributesObject["eduPersonTargetedID"].Count == 0) throw new LoginException("Missing eduPersonTargetedID from Wayf attributes"); if (attributesObject["eduPersonTargetedID"][0] == null) throw new LoginException(string.Format("First value in eduPersonTargetedID is null (contained {0} value(s))", attributesObject["eduPersonTargetedID"].Count)); if (!_wayfFilter.Validate(attributesObject)) throw new WayfUserNotAllowedException(attributes); var wayfId = attributesObject["eduPersonTargetedID"][0]; var wayfUser = AuthenticationRepository.WayfProfileGet(wayfId); if (wayfUser == null) { wayfUser = new WayfUser(); var email = attributesObject.ContainsKey("mail") && attributesObject["mail"].Count != 0 && !string.IsNullOrWhiteSpace(attributesObject["mail"][0]) ? attributesObject["mail"][0] : wayfId; var existingUser = PortalRepository.UserInfoGet(null, null, email, null).FirstOrDefault(); if (existingUser == null) { wayfUser.UserGuid = Guid.NewGuid(); if (PortalRepository.UserCreate(wayfUser.UserGuid, email) != 1) throw new LoginException("Failed to create new user"); } else wayfUser.UserGuid = existingUser.Guid; AuthenticationRepository.WayfProfileUpdate(wayfUser.UserGuid, wayfId); } var result = PortalRepository.SessionUpdate(sessionGuidToAuthenticate, wayfUser.UserGuid); if (result == null) throw new LoginException("Session could not be updated"); AuthenticationModule.OnOnUserLoggedIn(new RequestDelegate.PortalRequestArgs(Request)); AuthenticationModule.OnOnWayfUserLoggedIn(new WayfProfileArgs(wayfUser.UserGuid, attributesObject)); return PortalRepository.UserInfoGet(null, sessionGuidToAuthenticate, null, null).First(); }
public void Login_GivenExistingWayfId_ReturnUserInfoAndUpdateProfileAndAuthenticateSession() { var extension = Make_Wayf(); var wayfId = "somerandomletters"; var email = "*****@*****.**"; var attributeData = string.Format("{{'eduPersonTargetedID': ['{0}'], 'mail': ['{1}']}}", wayfId, email); var expected = new UserInfo { Guid = new Guid("10000000-0000-0000-0000-000000000001"), Email = "*****@*****.**" }; var callingUser = new UserInfo { Guid = new Guid("10000000-0000-0000-0000-000000000002"), Email = "*****@*****.**", SystemPermissonsEnum = SystemPermissons.All }; var profile = new WayfUser() { UserGuid = expected.Guid, WayfId = wayfId }; var sessionToAuthenticate = new Session { Guid = new Guid("12000000-0000-0000-0000-000000000021") }; PortalRepository.Setup(m => m.SessionUpdate(sessionToAuthenticate.Guid, expected.Guid)).Returns(new Session()).Verifiable(); PortalRepository.Setup(m => m.UserInfoGet(null, sessionToAuthenticate.Guid, null, null)).Returns(new[] { expected }).Verifiable(); PortalRequest.SetupGet(p => p.User).Returns(callingUser).Verifiable(); AuthenticationRepository.Setup(m => m.WayfProfileGet(wayfId)).Returns(profile).Verifiable(); WayfFilter.Setup(f => f.Validate(It.IsAny<IDictionary<string, IList<string>>>())).Returns(true).Verifiable(); var result = extension.Login(attributeData, sessionToAuthenticate.Guid); PortalRepository.Verify(); PortalRequest.Verify(); AuthenticationRepository.Verify(); WayfFilter.Verify(); Assert.That(result, Is.EqualTo(expected)); }