예제 #1
0
        public static Token GetTokenFromResponse(GetSharedSecretResponse response)
        {
            var unixTime = UnixTimestampHelper.ConvertToUnixTimeStamp(DateTime.UtcNow);

            var token = new Token()
            {
                TimeSkew       = unixTime - long.Parse(response.UTCTimestamp),
                Salt           = Base64Helper.Base64DecodeBytes(response.SecretContainer.EncryptionMethod.PBESalt),
                IterationCount = int.Parse(response.SecretContainer.EncryptionMethod.PBEIterationCount),
                Iv             = Base64Helper.Base64DecodeBytes(response.SecretContainer.EncryptionMethod.IV),
                Id             = response.SecretContainer.Device.Secret.Id,
                Cipher         = Base64Helper.Base64DecodeBytes(response.SecretContainer.Device.Secret.Data.Cipher),
                Digest         = Base64Helper.Base64DecodeBytes(response.SecretContainer.Device.Secret.Data.Digest.Text),
                Expiry         = response.SecretContainer.Device.Secret.Expiry,
                Period         = int.Parse(response.SecretContainer.Device.Secret.Usage.TimeStep),
                Counter        = int.Parse(response.SecretContainer.Device.Secret.Usage.Counter ?? "0")
            };

            var alg         = response.SecretContainer.Device.Secret.Usage.AI.Type;
            var algSplitted = alg.Split('-');

            if (algSplitted.Length == 4 && algSplitted[0] == "HMAC" && algSplitted[2] == "TRUNC" && algSplitted[3].EndsWith("DIGITS"))
            {
                token.Algorithm = algSplitted[1].ToLowerInvariant();
                token.Digits    = int.Parse(algSplitted[3].Substring(0, algSplitted[3].Length - 6));
            }
            else
            {
                throw new InvalidOperationException($"Unknown algorithm: {alg}");
            }

            return(token);
        }