public async Task <Client> Get(KeyId clientId)
        {
            if (clientId == KeyId.Empty)
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(clientId));
            }

            await _migrator.Migrate().ConfigureAwait(continueOnCapturedContext: false);

            if (IsProhibitedId(clientId))
            {
                return(null);
            }

            var collection = _lazyCollection.Value;

            var findResult = await collection.FindAsync(r => r.Id == clientId).ConfigureAwait(continueOnCapturedContext: false);

            var matches = await findResult.ToListAsync().ConfigureAwait(continueOnCapturedContext: false);

            if (!matches.Any())
            {
                return(null);
            }

            var match = matches.Single();

            var nonceLifetime = !match.NonceLifetime.HasValue || match.NonceLifetime.Value <= 0.0
                ? ClientOptions.Default.NonceLifetime
                : TimeSpan.FromSeconds(match.NonceLifetime.Value);

            var clockSkew = !match.ClockSkew.HasValue || match.ClockSkew.Value <= 0.0
                ? ClientOptions.Default.ClockSkew
                : TimeSpan.FromSeconds(match.ClockSkew.Value);

            var requestTargetEscaping = RequestTargetEscaping.RFC3986;

            if (!string.IsNullOrEmpty(match.RequestTargetEscaping))
            {
                if (Enum.TryParse <RequestTargetEscaping>(match.RequestTargetEscaping, ignoreCase: true, out var parsed))
                {
                    requestTargetEscaping = parsed;
                }
            }

            var signatureAlgorithm = _signatureAlgorithmDataRecordConverter.ToSignatureAlgorithm(match.SignatureAlgorithm, _encryptionKey, match.V);

            return(new Client(
                       match.Id,
                       match.Name,
                       signatureAlgorithm,
                       nonceLifetime,
                       clockSkew,
                       requestTargetEscaping,
                       match.Claims?.Select(c => c.ToClaim())?.ToArray()));
        }
Exemplo n.º 2
0
        public async Task <Client> Get(KeyId clientId)
        {
            if (clientId == KeyId.Empty)
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(clientId));
            }

            var currentClients = await _fileManager.Read();

            var matches = currentClients.Where(c => c.Id == clientId).ToList();

            if (!matches.Any())
            {
                return(null);
            }
            var match = matches.Single();

            var nonceLifetime = !match.NonceLifetime.HasValue || match.NonceLifetime.Value <= 0.0
                ? ClientOptions.Default.NonceLifetime
                : TimeSpan.FromSeconds(match.NonceLifetime.Value);

            var clockSkew = !match.ClockSkew.HasValue || match.ClockSkew.Value <= 0.0
                ? ClientOptions.Default.ClockSkew
                : TimeSpan.FromSeconds(match.ClockSkew.Value);

            var requestTargetEscaping = RequestTargetEscaping.RFC3986;

            if (!string.IsNullOrEmpty(match.Escaping))
            {
                if (Enum.TryParse <RequestTargetEscaping>(match.Escaping, ignoreCase: true, out var parsed))
                {
                    requestTargetEscaping = parsed;
                }
            }

            var signatureAlgorithm = _signatureAlgorithmDataRecordConverter.ToSignatureAlgorithm(match.SigAlg, _encryptionKey, match.V);

            return(new Client(
                       match.Id,
                       match.Name,
                       signatureAlgorithm,
                       nonceLifetime,
                       clockSkew,
                       requestTargetEscaping,
                       match.Claims?.Select(c => c.ToClaim())?.ToArray()));
        }