Exemplo n.º 1
0
        public IActionResult AddAuth([FromBody] Token auth)
        {
            try
            {
                TokenService AuthsService = new TokenService();

                AuthsService.AddToken(auth);

                var aut = AuthsService.GetTokenDisp(auth.Numero);


                //Caso achar retorna 200 e o usuario
                if (aut != null)
                {
                    IdDTO autId = new IdDTO(aut.Id);
                    return(Ok(autId));
                }
                else
                {
                    return(StatusCode(422));
                    //caso contrario retorna 412
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 2
0
        public void AddTokenValidUser()
        {
            //Arrange
            DataBaseContext db           = new DataBaseContext();
            TokenService    tokenService = new TokenService(db);
            Guid            userID       = new Guid("44361F37-036B-E911-AA03-021598E9EC9E");
            string          jwt          = "TokenServiceTest3";

            //Act
            bool success = tokenService.AddToken(jwt, userID);

            //Assert
            Assert.IsTrue(success);
        }
Exemplo n.º 3
0
        public async void AddToken_AddExistsToken_ReturnsThrowArgumentException()
        {
            string strToken = "1";
            Guid   userId   = Guid.NewGuid();
            var    token    = new Token()
            {
                StrToken = strToken, UserId = userId
            };

            var tokenRepo = new Mock <ITokenRepository>();

            tokenRepo.Setup(a => a.CheckUserByToken(token)).Returns(Task.Run(() => true));
            TokenService tokenService = new TokenService(tokenRepo.Object);

            var result1 = tokenService.AddToken(strToken, userId);

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await result1);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a JWT and adds it to the database
        /// </summary>
        /// <param name="user">UserObject</param>
        /// <returns>The JWT created as a string</returns>
        public string CreateToken(User user)
        {
            //var hmac = new HMACSHA256();
            //string key = Convert.ToBase64String(hmac.Key);
            var now     = DateTime.UtcNow;
            var handler = new JwtSecurityTokenHandler();
            //using key basically says we are the only ones that can create key
            var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
            var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);

            //user info which will go in the payload
            //add userID
            List <Claim> userInfo = new List <Claim>()
            {
                new Claim("userID", user.userID.ToString()),
                new Claim("email", user.userEmail),
                new Claim("client", user.clientID.ToString()),
                new Claim("height", user.height.ToString()),
            };


            var header  = new JwtHeader(credentials);
            var payload = new JwtPayload(
                issuer: "CheckIt.gq",//may change depending on our sites name
                audience: null,
                claims: userInfo,
                notBefore: null,
                expires: now.AddMinutes(15)//sets expiration at 15 minutes from now
                );



            var securityToken = new JwtSecurityToken(header, payload);

            string jwt = handler.WriteToken(securityToken);

            //add to db
            tokenService.AddToken(jwt, user.userID);

            return(jwt);
        }