Exemplo n.º 1
0
    public async Task <bool> Create(IdentityEntryRequest identityEntryRequest, CancellationToken token)
    {
        identityEntryRequest.Verify();
        DocumentId documentId = new DocumentId(identityEntryRequest.DirectoryId);

        IdentityEntry?exist = await Get(documentId, token : token, bypassCache : true);

        if (exist != null)
        {
            return(false);
        }

        RSA rsa = RSA.Create();

        var document = new IdentityEntry
        {
            DirectoryId = identityEntryRequest.DirectoryId,
            Subject     = identityEntryRequest.Issuer,
            PublicKey   = rsa.ExportRSAPublicKey(),
            PrivateKey  = rsa.ExportRSAPrivateKey(),
        };

        await _documentStorage.Set(documentId, document, token : token);

        return(true);
    }
Exemplo n.º 2
0
    public void GivenNewIdentity_RSAParameters_Match()
    {
        const string issuer     = "*****@*****.**";
        var          documentId = new DocumentId("test/unit-tests-identity/identity1");

        RSA           rsa    = RSA.Create();
        RSAParameters source = rsa.ExportParameters(true);

        var entry = new IdentityEntry
        {
            DirectoryId = (string)documentId,
            Subject     = issuer,
            PublicKey   = rsa.ExportRSAPublicKey(),
            PrivateKey  = rsa.ExportRSAPrivateKey(),
        };

        RSAParameters result = entry.GetRsaParameters();

        source.D !.Length.Should().Be(result.D !.Length);
        source.DP !.Length.Should().Be(source.DP !.Length);
        source.DQ !.Length.Should().Be(source.DQ !.Length);
        source.Exponent !.Length.Should().Be(source.Exponent !.Length);
        source.InverseQ !.Length.Should().Be(source.InverseQ !.Length);
        source.Modulus !.Length.Should().Be(source.Modulus !.Length);
        source.P !.Length.Should().Be(source.P !.Length);
        source.Q !.Length.Should().Be(source.Q !.Length);

        Enumerable.SequenceEqual(source.D, result.D !);
        Enumerable.SequenceEqual(source.DP, result.DP !);
        Enumerable.SequenceEqual(source.Exponent, result.Exponent !);
        Enumerable.SequenceEqual(source.InverseQ, result.InverseQ !);
        Enumerable.SequenceEqual(source.Modulus, result.Modulus !);
        Enumerable.SequenceEqual(source.P, result.P !);
        Enumerable.SequenceEqual(source.Q, result.Q !);
    }
Exemplo n.º 3
0
        public async Task Set(IdentityEntry entry, CancellationToken token = default)
        {
            _logger.LogTrace($"Putting entry directoryId={entry.DirectoryId}");

            HttpResponseMessage response = await _httpClient.PostAsJsonAsync($"api/identity", entry, token);

            response.EnsureSuccessStatusCode();
        }
Exemplo n.º 4
0
    public async Task <IdentityEntry> Set(IdentityEntry entry, CancellationToken token = default)
    {
        ETag eTag = await _documentStorage.Set((DocumentId)entry.DirectoryId, entry, entry.ETag, token);

        return(entry with {
            ETag = eTag
        });
    }
Exemplo n.º 5
0
 public static void Verify(this IdentityEntry subject)
 {
     subject.VerifyNotNull(nameof(subject));
     subject.DirectoryId.VerifyDocumentId();
     subject.Subject.VerifyNotEmpty(nameof(subject.Subject));
     subject.ClassType.VerifyNotEmpty(nameof(subject.ClassType));
     subject.PublicKey.VerifyNotNull(nameof(subject.PublicKey));
     subject.Properties.VerifyNotNull(nameof(subject.Properties));
 }
Exemplo n.º 6
0
    private RSAParameters Create(DocumentId documentId)
    {
        RSA           rsa    = RSA.Create();
        RSAParameters source = rsa.ExportParameters(true);

        var entry = new IdentityEntry
        {
            DirectoryId = (string)documentId,
            Subject     = "<subject>",
            PublicKey   = rsa.ExportRSAPublicKey(),
            PrivateKey  = rsa.ExportRSAPrivateKey(),
        };

        return(entry.GetRsaParameters());
    }
Exemplo n.º 7
0
    public static RSAParameters GetRsaParameters(this IdentityEntry identityEntry)
    {
        identityEntry.VerifyNotNull(nameof(identityEntry));
        identityEntry.PublicKey.VerifyNotNull(nameof(identityEntry.PublicKey));

        RSA rsa = RSA.Create();

        rsa.ImportRSAPublicKey(identityEntry.PublicKey, out int publicReadSize);

        if (identityEntry.PrivateKey != null)
        {
            rsa.ImportRSAPrivateKey(identityEntry.PrivateKey, out int privateReadSize);
        }

        return(rsa.ExportParameters(identityEntry.PrivateKey != null));
    }
Exemplo n.º 8
0
    public async Task Set(string file, CancellationToken token)
    {
        file.VerifyNotEmpty(nameof(file));

        using IDisposable scope = _logger.BeginScope(new { Command = nameof(Set), File = file });

        if (!File.Exists(file))
        {
            _logger.LogError($"{file} does not exit");
            return;
        }

        _logger.LogInformation($"Reading {file} to directory");
        string json = File.ReadAllText(file);

        IdentityEntry entry = Json.Default.Deserialize <IdentityEntry>(json) !;
        await _identityClient.Set(entry, token);
    }
Exemplo n.º 9
0
 public static void VerifyWithPrivateKey(this IdentityEntry subject)
 {
     subject.Verify();
     subject.PrivateKey.VerifyNotNull(nameof(subject.PrivateKey));
 }
Exemplo n.º 10
0
    public async Task <IActionResult> Set([FromBody] IdentityEntry entry, CancellationToken token)
    {
        await _identityService.Set(entry, token);

        return(Ok());
    }