Exemplo n.º 1
0
        public void CreateToken_ShouldReturnAuthToken()
        {
            var token = BaseAuth.CreateToken("standart:u1");

            Debug.WriteLine("# Token : ");
            Debug.WriteLine(token);
        }
Exemplo n.º 2
0
        public HttpResponseMessage Login(StandartAuthRequest request)
        {
            try
            {
                // Check request and request props is not null
                CheckVal.IsNull(request, nameof(request));
                CheckVal.IsNull(request.Password, nameof(request.Password));
                // Find User
                var user = CheckData.UserExist(_context, false, email: request.Email, name: request.Name);
                if (user == null)
                {
                    throw new ApiException(ApiResult.Validation, ErrorType.UserWithEmailorNameNotFound,
                                           request.Email ?? request.Name);
                }
                // Check if User is Blocked
                CheckData.IsUserBlocked(_context, null, user);
                // Check if User Account Exist
                var account = CheckData.AccountExist(_context, Provider.Standart, user.Id);
                // Check password
                BaseAuth.CheckPassword(user, request.Password);


                var token = BaseAuth.CreateToken(user.Id);

                var userDto = _mapper.Map <User, UserDTO>(user);

                // Generate AuthenticationToken
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new Response <LoginResult>(ApiResult.Ok, new LoginResult(token, userDto))));
            }
            catch (ApiException ex)
            {
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new Response <LoginResult>(ex.ApiResult, null, new ErrorResponse(ex.ErrorType, ex.Message, ex))));
            }
            catch (EntitySqlException ex)
            {
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new Response <LoginResult>(ApiResult.Sql, null, new ErrorResponse(ErrorType.None, ex.Message, ex))));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new Response <LoginResult>(ApiResult.Unknown, null,
                                                                         new ErrorResponse(ErrorType.Internal, ex.Message, ex))));
            }
        }
Exemplo n.º 3
0
        public void PostApiControllerTestInitialize()
        {
            _config = Mapping.Mapping.GetConfiguration();
            _mapper = _config.CreateMapper();
            TestDbPopulator.PopulateDb(2, 1);
            string token   = BaseAuth.CreateToken("u1");
            var    config  = new HttpConfiguration();
            var    request = new HttpRequestMessage {
                RequestUri = new Uri("http://localhost:50268/api/Post")
            };

            request.Headers.Add("x-zumo-auth", token);
            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
            _controller = new PostApiController(TestContext)
            {
                Request = request,
                User    = TestHelper.GetUser("u1")
            };
        }
Exemplo n.º 4
0
        public void UserController_ShouldReturnCurrentUser()
        {
            TestDbPopulator.PopulateUsers(2);
            TestDbPopulator.PopulateStandartAccounts(2);
            _user = TestDbPopulator.GetUser(1);
            var req = new StandartAuthRequest
            {
                Email    = _user.Email,
                Password = "******"
            };

            string token   = BaseAuth.CreateToken("u1");
            var    config  = new HttpConfiguration();
            var    request = new HttpRequestMessage {
                RequestUri = new Uri("http://localhost:50268/api/User")
            };

            request.Headers.Add("x-zumo-auth", token);
            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
            var username            = "******";
            var identity            = new GenericIdentity(username, "");
            var nameIdentifierClaim = new Claim(ClaimTypes.NameIdentifier, username);

            identity.AddClaim(nameIdentifierClaim);
            var principal = new GenericPrincipal(identity, new string[] {});
            var user      = new ClaimsPrincipal(principal);

            _controller = new UserApiController(TestContext)
            {
                Request = request,
                User    = user
            };
            var response = _controller.GetCurrentUser();
            var result   = TestHelper.ParseUserResponse(response);

            Assert.AreEqual("OK", result.StatusCode);
        }