public async Task UnitTest1() { var context = new CalDavContext(new DbContextOptions <CalDavContext>()); var prinRepository = new PrincipalRepository(context); var user = prinRepository.CreateUserInSystem(_email, _fullName, _password); Assert.NotNull(user); context.SaveChanges(); Assert.True(context.Users.Any()); var dbUser = context.Users.FirstOrDefault(x => x.Email == _email); Assert.NotNull(dbUser); Assert.True(await prinRepository.ExistByStringIs(_email)); }
/// <summary> /// Takes the necessary content from the UH's authentication API response. /// Check if the user exist in the system, if does then check if the authentication /// credential are OK. /// If dont then take the user data from UH apis and create the user in the /// system with this data. /// </summary> /// <param name="httpContext"></param> /// <returns></returns> public async Task <Principal> AuthenticateRequestAsync(HttpContext httpContext) { var username = ""; Principal principal = null; string cookieValue; //take the creadentials from the request string authHeader = httpContext.Request.Headers["Authorization"]; if (!string.IsNullOrEmpty(authHeader)) { var credentials = TakeCreadential(authHeader); username = credentials.Key; var password = credentials.Value; principal = _principalRepository.GetByIdentifier(username); //check if the user exist in our DB if (principal != null) { // if does then check if can authenticate //if the username and password doesnt match then return 401 - Unauthorized if (!_principalRepository.VerifyPassword(principal, password)) { SetUnauthorizedRequest(httpContext); return(null); } } //if the user is new in our system then create him //TODO: change this if dont want the new user automatic creation behavior //Temporaly if the WCF services doesnt work we are gonna create // the users automatically in the system. // TODO: check if is a student or teacher principal = _principalRepository.CreateUserInSystem(username, username, password); Console.WriteLine($"------Created user with username: {username}"); //TODO: change to this when work the WCF service //var userData = GetUserDataFromUhApi(username); } if (principal != null) { return(principal); } #region checking cookies //if the request doesn't have an Authorization header then //ckeck the session cookies. //else //{ // //if the request doens't comes with a authorization header // // then check if has the cookie provided by us // // // if (!httpContext.Request.Cookies.ContainsKey(SystemProperties._cookieSessionName)) // { // /* // | if the request neither contains the session cookie nor the // | Authorization header then the client needs to request // | the credential to the user. So send a 401 // */ // await SetUnauthorizedRequest(httpContext); // return null; // } // //take the cookie that the client send us in the request // cookieValue = httpContext.Request.Cookies[SystemProperties._cookieSessionName]; // principal =await _principalRepository.GetByCookie(cookieValue); // if(principal == null) // { // await SetUnauthorizedRequest(httpContext); // return null; // } //} //set the cookie for the response. //cookieValue = Guid.NewGuid().ToString(); //httpContext.Response.Cookies.Append(SystemProperties._cookieSessionName, cookieValue); //await _principalRepository.SetCookie(username, cookieValue); #endregion SetUnauthorizedRequest(httpContext); return(null); //return await Task.FromResult(principal); }