<commit_before>﻿using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using HOLMS.Types.IAM;
using Microsoft.IdentityModel.Tokens;

namespace HOLMS.Support.Security {
    public class ValidatedJWToken {
        public string RawTokenData { get; protected set; }
        public AuthenticatedClientClaims ACC { get; protected set; }

        protected ValidatedJWToken() { }

        private ValidatedJWToken(JwtSecurityToken t) {
            RawTokenData = t.RawData;
            var claims = t.Claims.ToDictionary(x => x.Type);
            if (!claims.ContainsKey(JWToken.ClientIdKey) || !claims.ContainsKey(JWToken.TenancyIdKey) ||
                !claims.ContainsKey(JWToken.UserIdKey)) {
                throw new Exception("Missing keys in token");
            }

            ACC = new AuthenticatedClientClaims {
                Client = new ClientInstanceIndicator(Guid.Parse(claims[JWToken.ClientIdKey].Value)),
                Tenancy = new TenancyIndicator(Guid.Parse(claims[JWToken.TenancyIdKey].Value)),
                User = new StaffMemberIndicator(Guid.Parse(claims[JWToken.UserIdKey].Value)),
            };
        }

        public static ValidatedJWToken CreateWithValidation(string rawData, SecurityKey key) {
            var tokenValidationParameters = new TokenValidationParameters() {
                ValidAudiences = new [] { JWToken.Audience },
                ValidIssuers = new [] { JWToken.Issuer },
                IssuerSigningKey = key,
                ValidateLifetime = true,
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            SecurityToken validatedToken;
            tokenHandler.ValidateToken(rawData, tokenValidationParameters, out validatedToken);

            return new ValidatedJWToken(tokenHandler.ReadToken(rawData) as JwtSecurityToken);
        }
    }
}<commit_msg>Add date-validation code, for tokens with date validation<commit_after>﻿using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security;
using HOLMS.Types.IAM;
using Microsoft.IdentityModel.Tokens;

namespace HOLMS.Support.Security {
    public class ValidatedJWToken {
        public string RawTokenData { get; protected set; }
        public AuthenticatedClientClaims ACC { get; protected set; }

        protected ValidatedJWToken() { }

        private ValidatedJWToken(JwtSecurityToken t) {
            RawTokenData = t.RawData;
            var claims = t.Claims.ToDictionary(x => x.Type);
            if (!claims.ContainsKey(JWToken.ClientIdKey) || !claims.ContainsKey(JWToken.TenancyIdKey) ||
                !claims.ContainsKey(JWToken.UserIdKey)) {
                throw new Exception("Missing keys in token");
            }

            ACC = new AuthenticatedClientClaims {
                Client = new ClientInstanceIndicator(Guid.Parse(claims[JWToken.ClientIdKey].Value)),
                Tenancy = new TenancyIndicator(Guid.Parse(claims[JWToken.TenancyIdKey].Value)),
                User = new StaffMemberIndicator(Guid.Parse(claims[JWToken.UserIdKey].Value)),
            };
        }

        public static ValidatedJWToken CreateWithValidation(string rawData, SecurityKey key) {
            var tokenValidationParameters = new TokenValidationParameters() {
                ValidAudiences = new [] { JWToken.Audience },
                ValidIssuers = new [] { JWToken.Issuer },
                IssuerSigningKey = key,
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            SecurityToken validatedToken;
            tokenHandler.ValidateToken(rawData, tokenValidationParameters, out validatedToken);

            var tok = tokenHandler.ReadToken(rawData) as JwtSecurityToken;

            var expirationClaim = tok.Claims.FirstOrDefault(x => x.Type == JwtRegisteredClaimNames.Exp);
            if (expirationClaim != null) {
                if (tok.ValidTo < DateTime.Now) {
                    throw new SecurityException("Presented token is expired");
                }
            }

            return new ValidatedJWToken(tok);
        }
    }
}
{'fraction_non_alphanumeric': 0.08735968765251342, 'fraction_numerical': 0.0, 'mean_word_length': 2.4532434709351305, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7393618', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1061, 'n_words': 219}