public void When_LoginUserWithCorrectPassword_Then_ReturnUser() { //Arrange var userEmail = "*****@*****.**"; var password = "******"; var passwordHased = password + "hased"; var mockedUsers = new List <User> { new User() { Id = Guid.NewGuid(), UserEmail = "*****@*****.**", PasswordHash = "" }, new User() { Id = Guid.NewGuid(), UserEmail = userEmail, PasswordHash = passwordHased }, new User() { Id = Guid.NewGuid(), UserEmail = "*****@*****.**", PasswordHash = "" } }; _hasher.Setup(e => e.CalculateHash(password)).Returns(passwordHased); _repository.Setup(e => e.List()).Returns(mockedUsers); IAuthenticationDomain sut = new AuthenticationDomain(_repository.Object, _hasher.Object); //Action var user = sut.LogInUser(userEmail, password); //Assert Assert.IsNotNull(user); }
/// <summary> /// Asynchronously get a Couchbase user using it's username. /// </summary> /// <param name="domain">The authentication domain.</param> public async Task <IResult <User> > GetUserAsync(AuthenticationDomain domain, string username) { var uri = GetUserManagementUri(domain, username); using (var request = new HttpRequestMessage(HttpMethod.Get, uri)) { SetHeaders(request, uri); using (var response = await _httpClient.SendAsync(request).ContinueOnAnyContext()) { if (response.IsSuccessStatusCode) { using (var stream = await response.Content.ReadAsStreamAsync().ContinueOnAnyContext()) { return(new DefaultResult <User> { Success = true, Value = Mapper.Map <User>(stream) ?? new User() }); } } return(new DefaultResult <User> { Success = false }); } } }
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes <DontValidate>().Any()) { // The controller action is decorated with the [DontValidate] // custom attribute => don't do anything. return; } var token = actionContext.Request.Headers.FirstOrDefault(i => i.Key == "token"); if (token.Value == null) { throw new Exception("Token Required"); } Guid?validateToken = null; try { validateToken = Guid.Parse(token.Value.FirstOrDefault()); } catch (Exception) { throw new Exception("Token format invalid"); } AuthenticationDomain authDomain = new AuthenticationDomain(); authDomain.ValidateToken(validateToken.Value); base.OnActionExecuting(actionContext); }
/// <summary> /// Get a list of Couchbase users. /// </summary> /// <param name="domain">The authentication domain.</param> public IResult <IEnumerable <User> > GetUsers(AuthenticationDomain domain) { using (new SynchronizationContextExclusion()) { return(GetUsersAsync(domain).Result); } }
/// <summary> /// Get a Couchbase user using it's username. /// </summary> /// <param name="domain">The authentication domain.</param> public IResult <User> GetUser(AuthenticationDomain domain, string username) { using (new SynchronizationContextExclusion()) { return(GetUserAsync(domain, username).Result); } }
/// <summary> /// Asynchronously adds or replaces an existing Couchbase user with the provided <see cref="username" />, <see cref="password" />, <see cref="name" /> and <see cref="roles" />. /// </summary> /// <param name="domain">The authentication domain.</param> /// <param name="username">The username.</param> /// <param name="password">The password.</param> /// <param name="name">The full name for the user.</param> /// <param name="roles">The roles.</param> public async Task <IResult> UpsertUserAsync(AuthenticationDomain domain, string username, string password = null, string name = null, params Role[] roles) { if (string.IsNullOrWhiteSpace(username)) { throw new ArgumentException("username cannot be null or empty"); } if (string.IsNullOrWhiteSpace(password)) { throw new ArgumentException("password cannot be null or empty"); } if (roles == null || !roles.Any()) { throw new ArgumentException("roles cannot be null or empty"); } if (domain == AuthenticationDomain.External && !string.IsNullOrWhiteSpace(password)) { Log.Warn("Unable to update external user's password"); } var uri = GetUserManagementUri(domain, username); var formValues = GetUserFormValues(password, name, roles); using (var request = new HttpRequestMessage(HttpMethod.Put, uri)) { request.Content = new FormUrlEncodedContent(formValues); SetHeaders(request, uri); using (var response = await _httpClient.SendAsync(request).ContinueOnAnyContext()) { return(new DefaultResult <bool> { Success = response.IsSuccessStatusCode }); } } }
public IHttpActionResult GetToken(GetToken getToken) { AuthenticationDomain authDomain = new AuthenticationDomain(); Guid token = authDomain.GenerateDeviceToken(getToken); return(Ok <Guid>(token)); }
/// <summary> /// Adds or replaces an existing Couchbase user with the provided <see cref="username" />, <see cref="password" />, <see cref="name" /> and <see cref="roles" />. /// </summary> /// <param name="domain">The authentication domain.</param> /// <param name="username">The username.</param> /// <param name="password">The password.</param> /// <param name="name">The full name for the user.</param> /// <param name="roles">The list of roles for the user.</param> public IResult UpsertUser(AuthenticationDomain domain, string username, string password = null, string name = null, params Role[] roles) { using (new SynchronizationContextExclusion()) { return(UpsertUserAsync(domain, username, password, name, roles).Result); } }
public void GetUser_Builds_Correct_Uri(AuthenticationDomain domain) { var expextedRequestPath = string.Format( "http://localhost:8091/settings/rbac/users/{0}/alice", domain == AuthenticationDomain.Local ? "local" : "external" ); var handler = FakeHttpMessageHandler.Create(request => { Assert.AreEqual(expextedRequestPath, request.RequestUri.OriginalString); Assert.AreEqual(HttpMethod.Get, request.Method); var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("") }; return(response); }); var client = new HttpClient(handler); var clientConfig = new ClientConfiguration(); var serverConfigMock = new Mock <IServerConfig>(); var dataMapper = new JsonDataMapper(clientConfig); var manager = new ClusterManager(clientConfig, serverConfigMock.Object, dataMapper, client, "username", "password"); manager.GetUser(domain, "alice"); }
public void AuthenticationDomain_Authenticate_Exists() { var authenticationModel = new AuthenticationModel { Login = "******", Password = "******" }; var result = AuthenticationDomain.Authenticate(authenticationModel); Assert.IsNotNull(result); }
public void When_EmailIsEmpty_Then_ReturnNull() { //Arrange var userEmail = ""; var password = "******"; IAuthenticationDomain sut = new AuthenticationDomain(_repository.Object, _hasher.Object); //Action var user = sut.LogInUser(userEmail, password); //Assert Assert.IsNull(user); }
private Uri GetUserManagementUri(AuthenticationDomain domain, string username = null) { var scheme = _clientConfig.UseSsl ? "https" : "http"; var host = _clientConfig.Servers.Shuffle().First().Host; var port = _clientConfig.UseSsl ? _clientConfig.HttpsMgmtPort : _clientConfig.MgmtPort; var userManagementPath = "settings/rbac/users/" + (domain == AuthenticationDomain.Local ? "local" : "external"); var builder = new UriBuilder(scheme, host, port, userManagementPath); if (!string.IsNullOrWhiteSpace(username)) { builder.Path = string.Format("{0}/{1}", builder.Path, username); } return(builder.Uri); }
private Uri GetUserManagementUri(AuthenticationDomain domain, string username = null) { var builder = new UriBuilder { Scheme = _configuration.UseSsl ? "https" : "http", Host = _configuration.Servers.GetRandom().Host, Port = _configuration.UseSsl ? 18091 : 8091, //TODO: use configured ports Path = $"settings/rbac/users/{domain.GetDescription()}" }; if (!string.IsNullOrWhiteSpace(username)) { builder.Path += $"/{username}"; } return(builder.Uri); }
public void When_LoginUserDoesntExist_Then_ReturnNull() { //Arrange var userEmail = "*****@*****.**"; var password = "******"; var passwordHased = password + "hased"; var mockedUsers = new List <User>(); _hasher.Setup(e => e.CalculateHash(password)).Returns(passwordHased + "old"); _repository.Setup(e => e.List()).Returns(mockedUsers); IAuthenticationDomain sut = new AuthenticationDomain(_repository.Object, _hasher.Object); //Action var user = sut.LogInUser(userEmail, password); //Assert Assert.IsNull(user); }
/// <summary> /// Asynchronously removes a Couchbase user with the <see cref="username" />. /// </summary> /// <param name="domain">The authentication domain.</param> /// <param name="username">The username.</param> public async Task <IResult> RemoveUserAsync(AuthenticationDomain domain, string username) { if (string.IsNullOrWhiteSpace(username)) { throw new ArgumentException("username cannot be null or empty"); } var uri = GetUserManagementUri(domain, username); using (var request = new HttpRequestMessage(HttpMethod.Delete, uri)) { SetHeaders(request, uri); using (var response = await _httpClient.SendAsync(request).ContinueOnAnyContext()) { return(new DefaultResult <bool> { Success = response.IsSuccessStatusCode }); } } }
public GetUserOptions WithAuthenticationDomain(AuthenticationDomain authenticationDomain) { AuthenticationDomain = authenticationDomain; return(this); }
public void AuthenticationDomain_Authenticate_Inexists() { var result = AuthenticationDomain.Authenticate(new AuthenticationModel()); Assert.IsNull(result); }