예제 #1
0
        private static TokenState CreateInvalidToken(Jwk signingKey, TokenValidationStatus status, JwsDescriptor descriptor, string claim = null)
        {
            descriptor.SigningKey = signingKey;
            descriptor.Algorithm = signingKey.SignatureAlgorithm;

            return CreateInvalidToken(status, descriptor);
        }
예제 #2
0
파일: Tokens.cs 프로젝트: watfordgnf/Jwt
        private static TokenState CreateInvalidToken(TokenValidationStatus status, JwtDescriptor descriptor, string?claim = null)
        {
            switch (status)
            {
            case TokenValidationStatus.SignatureKeyNotFound:
                descriptor.Header.Add(JwtHeaderParameterNames.Kid, "x");
                break;

            case TokenValidationStatus.MissingEncryptionAlgorithm:
                descriptor.Header.Add(JwtHeaderParameterNames.Enc, (object)null !);
                break;
            }

            var token  = descriptor;
            var writer = new JwtWriter();

            writer.IgnoreTokenValidation = true;
            var jwt = writer.WriteTokenString(token);

            switch (status)
            {
            case TokenValidationStatus.MalformedToken:
                jwt = "/" + jwt.Substring(0, jwt.Length - 1);
                break;

            case TokenValidationStatus.InvalidSignature:
                var parts = jwt.Split('.');
                parts[2] = new string(parts[2].Reverse().ToArray());
                jwt      = parts[0] + "." + parts[1] + "." + parts[2];
                break;

            case TokenValidationStatus.MalformedSignature:
                jwt = jwt.Substring(0, jwt.Length - 2);
                break;

            case TokenValidationStatus.MissingSignature:
                parts = jwt.Split('.');
                jwt   = parts[0] + "." + parts[1] + ".";
                break;

            default:
                break;
            }

            return(new TokenState(jwt, status));
        }
예제 #3
0
        public ActionResult ValidateToken([FromBody] UserDetails data)
        {
            try
            {
                using (var db = new ApplicationDbContext())
                {
                    if (data == null)
                    {
                        return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status400BadRequest));
                    }
                    else
                    {
                        AppHistoryService history = new AppHistoryService(_db);
                        var tokenInfo             = history.MatchedById(data.TokenId, data.AppName);
                        var isValid = history.ValidateUser(data);

                        if (isValid != null)
                        {
                            var userTokenDeviceDetails = new UserTokenDeviceDetails
                            {
                                UserTokenDetailsId = tokenInfo.Id,
                                DeviceId           = data.DeviceId,
                            };

                            var validUser = history.SaveUserLog(userTokenDeviceDetails);
                        }
                        else
                        {
                            TokenValidationStatus failureStatus = new TokenValidationStatus();
                            failureStatus.success = false;
                            failureStatus.message = "No such token found";
                            return(Ok(failureStatus));
                        }
                        TokenValidationStatus successStatus = new TokenValidationStatus();
                        successStatus.success = true;
                        successStatus.message = "Token validated successfully";
                        return(Ok(successStatus));
                    }
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status204NoContent));
            }
        }
예제 #4
0
        private static TokenState CreateInvalidToken(TokenValidationStatus status, JwtDescriptor descriptor)
        {
            switch (status)
            {
                case TokenValidationStatus.SignatureKeyNotFound:
                    descriptor.Header.Replace(new JwtProperty(HeaderParameters.KidUtf8, (string)descriptor.Header[HeaderParameters.KidUtf8].Value + "x"));
                    break;
                case TokenValidationStatus.MissingEncryptionAlgorithm:
                    descriptor.Header.Replace(new JwtProperty(HeaderParameters.EncUtf8));
                    break;
            }

            var token = descriptor;
            var writer = new JwtWriter();
            //writer.IgnoreTokenValidation = true;
            var jwt = writer.WriteTokenString(token);

            switch (status)
            {
                case TokenValidationStatus.MalformedToken:
                    jwt = "/" + jwt.Substring(0, jwt.Length - 1);
                    break;
                case TokenValidationStatus.InvalidSignature:
                    var parts = jwt.Split('.');
                    parts[2] = new string(parts[2].Reverse().ToArray());
                    jwt = parts[0] + "." + parts[1] + "." + parts[2];
                    break;
                case TokenValidationStatus.MalformedSignature:
                    jwt = jwt.Substring(0, jwt.Length - 2);
                    break;
                case TokenValidationStatus.MissingSignature:
                    parts = jwt.Split('.');
                    jwt = parts[0] + "." + parts[1] + ".";
                    break;
                default:
                    break;
            }

            return new TokenState(jwt, status);
        }
예제 #5
0
 public TokenState(string jwt, TokenValidationStatus status)
 {
     Jwt = jwt;
     Status = status;
 }
예제 #6
0
        private static JwsDescriptor CreateJws(JObject descriptor, TokenValidationStatus status, string claim = null)
        {
            var payload = new JObject();
            foreach (var kvp in descriptor)
            {
                switch (status)
                {
                    case TokenValidationStatus.InvalidClaim:
                        if (kvp.Key == "aud" && claim == "aud")
                        {
                            payload.Add(kvp.Key, kvp.Value + "XXX");
                            continue;
                        }
                        if (kvp.Key == "iss" && claim == "iss")
                        {
                            payload.Add(kvp.Key, kvp.Value + "XXX");
                            continue;
                        }
                        break;
                    case TokenValidationStatus.MissingClaim:
                        if (kvp.Key == "exp" & claim == "exp")
                        {
                            continue;
                        }
                        if (kvp.Key == "aud" & claim == "aud")
                        {
                            continue;
                        }
                        if (kvp.Key == "iss" && claim == "iss")
                        {
                            continue;
                        }
                        break;
                    case TokenValidationStatus.Expired:
                        if (kvp.Key == "exp")
                        {
                            payload.Add(kvp.Key, 1500000000);
                            continue;
                        }
                        if (kvp.Key == "nbf")
                        {
                            payload.Add(kvp.Key, 1400000000);
                            continue;
                        }
                        break;
                    case TokenValidationStatus.NotYetValid:
                        if (kvp.Key == "exp")
                        {
                            payload.Add(kvp.Key, 2100000000);
                            continue;
                        }
                        if (kvp.Key == "nbf")
                        {
                            payload.Add(kvp.Key, 2000000000);
                            continue;
                        }
                        break;
                }

                payload.Add(kvp.Key, kvp.Value);
            }

            return new JwsDescriptor(new JwtObject(), ToJwtObject(payload));
        }
예제 #7
0
파일: Tokens.cs 프로젝트: watfordgnf/Jwt
        private static JwsDescriptor CreateJws(Jwk signingKey, JObject descriptor, TokenValidationStatus status, string?claim = null)
        {
            var payload = new JwtPayload();

            foreach (var kvp in descriptor)
            {
                switch (status)
                {
                case TokenValidationStatus.InvalidClaim:
                    if (kvp.Key == "aud" && claim == "aud")
                    {
                        payload.Add(kvp.Key, kvp.Value + "XXX");
                        continue;
                    }
                    if (kvp.Key == "iss" && claim == "iss")
                    {
                        payload.Add(kvp.Key, kvp.Value + "XXX");
                        continue;
                    }
                    break;

                case TokenValidationStatus.MissingClaim:
                    if (kvp.Key == "exp" & claim == "exp")
                    {
                        continue;
                    }
                    if (kvp.Key == "aud" & claim == "aud")
                    {
                        continue;
                    }
                    if (kvp.Key == "iss" && claim == "iss")
                    {
                        continue;
                    }
                    break;

                case TokenValidationStatus.Expired:
                    if (kvp.Key == "exp")
                    {
                        payload.Add(kvp.Key, 1500000000);
                        continue;
                    }
                    if (kvp.Key == "nbf")
                    {
                        payload.Add(kvp.Key, 1400000000);
                        continue;
                    }
                    break;

                case TokenValidationStatus.NotYetValid:
                    if (kvp.Key == "exp")
                    {
                        payload.Add(kvp.Key, 2100000000);
                        continue;
                    }
                    if (kvp.Key == "nbf")
                    {
                        payload.Add(kvp.Key, 2000000000);
                        continue;
                    }
                    break;
                }


                switch (kvp.Value.Type)
                {
                case JTokenType.Object:
                    payload.Add(kvp.Key, (object)kvp.Value);
                    break;

                case JTokenType.Array:
                    payload.Add(kvp.Key, (object[])(object)kvp.Value);
                    break;

                case JTokenType.Integer:
                    payload.Add(kvp.Key, (long)kvp.Value);
                    break;

                case JTokenType.Float:
                    payload.Add(kvp.Key, (double)kvp.Value);
                    break;

                case JTokenType.String:
                    payload.Add(kvp.Key, (string)kvp.Value);
                    break;

                case JTokenType.Boolean:
                    payload.Add(kvp.Key, (bool)kvp.Value);
                    break;

                case JTokenType.Null:
                    payload.Add(kvp.Key, (object)kvp.Value);
                    break;
                }
            }

            var d = new JwsDescriptor(signingKey, SignatureAlgorithm.HS256)
            {
                Payload = payload
            };

            return(d);
        }
예제 #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SignatureValidationResult"/> class.
 /// </summary>
 public SignatureValidationResult(TokenValidationStatus status, Jwk signingKey)
 {
     Status     = status;
     SigningKey = signingKey;
 }
예제 #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SignatureValidationResult"/> class.
 /// </summary>
 public SignatureValidationResult(TokenValidationStatus status)
 {
     Status = status;
 }
예제 #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SignatureValidationResult"/> class.
 /// </summary>
 public SignatureValidationResult(TokenValidationStatus status, Exception?exception)
 {
     Status    = status;
     Exception = exception;
 }
예제 #11
0
 private TokenValidationError(TokenValidationStatus status, string message)
 {
     Status   = status;
     _message = message;
 }
예제 #12
0
 private TokenValidationError(TokenValidationStatus status, Exception?exception)
 {
     Status    = status;
     Exception = exception;
     _message  = exception?.Message;
 }
예제 #13
0
 private TokenValidationError(TokenValidationStatus status)
 {
     Status = status;
 }
예제 #14
0
 /// <summary>Initializes a new instance of the <see cref="SignatureValidationError"/> class.</summary>
 public SignatureValidationError(TokenValidationStatus status)
 {
     Status = status;
 }
예제 #15
0
        private static JwsDescriptor CreateJws(Jwk signingKey, Dictionary <string, object> descriptor, TokenValidationStatus status, string claim = null)
        {
            var payload = new JwtPayload();

            foreach (var kvp in descriptor)
            {
                switch (status)
                {
                case TokenValidationStatus.InvalidClaim:
                    if (kvp.Key == "aud" && claim == "aud")
                    {
                        payload.Add(kvp.Key, kvp.Value + "XXX");
                        continue;
                    }
                    if (kvp.Key == "iss" && claim == "iss")
                    {
                        payload.Add(kvp.Key, kvp.Value + "XXX");
                        continue;
                    }
                    break;

                case TokenValidationStatus.MissingClaim:
                    if (kvp.Key == "exp" & claim == "exp")
                    {
                        continue;
                    }
                    if (kvp.Key == "aud" & claim == "aud")
                    {
                        continue;
                    }
                    if (kvp.Key == "iss" && claim == "iss")
                    {
                        continue;
                    }
                    break;

                case TokenValidationStatus.Expired:
                    if (kvp.Key == "exp")
                    {
                        payload.Add(kvp.Key, 1500000000);
                        continue;
                    }
                    if (kvp.Key == "nbf")
                    {
                        payload.Add(kvp.Key, 1400000000);
                        continue;
                    }
                    break;

                case TokenValidationStatus.NotYetValid:
                    if (kvp.Key == "exp")
                    {
                        payload.Add(kvp.Key, 2100000000);
                        continue;
                    }
                    if (kvp.Key == "nbf")
                    {
                        payload.Add(kvp.Key, 2000000000);
                        continue;
                    }
                    break;
                }

                payload.Add(kvp.Key, kvp.Value);
            }

            var d = new JwsDescriptor(signingKey, signingKey.SignatureAlgorithm);

            d.Payload = payload;
            return(d);
        }
예제 #16
0
 public static void ReadingTokenFileFailed(this ILogger logger, string filename, TokenValidationStatus status, Exception?e = null)
 {
     _readingTokenFileFailed(logger, filename, status, e);
 }