Exemplo n.º 1
0
        public async Task TestSignature_AlreadySigned()
        {
            var session = ICredentials.CreateBySessionIdKey("sessionid", "sessionkey", "u123456789", "123456789");

            var firstBeleg = new MachineReadableCode(TrustProvider.A_Trust, "BOX1", "1", DateTime.UtcNow, 0m, 0m, 0m, 0m, 0m, "00==", _certHexSerial, "prev===", "SIG");

            var signature = await _client.Sign(firstBeleg, session);

            signature.IsSuccessful.Should().BeFalse();
        }
Exemplo n.º 2
0
        public async Task TestSignature_UserPassword()
        {
            var firstBeleg = new MachineReadableCode(TrustProvider.A_Trust, "BOX1", "1", DateTime.UtcNow, 0m, 0m, 0m, 0m, 0m, "00==", _certHexSerial, "prev===");

            var signature = await _client.Sign(firstBeleg, ICredentials.CreateByUsernamePassword("u123456789", "123456789"));

            signature.IsSuccessful.Should().BeTrue(because: signature.ErrorMessage);

            signature.Payload.Should().NotBeNull();
            signature.Payload.Signature.Should().NotBeNullOrEmpty();
        }
Exemplo n.º 3
0
        private async Task <IResponse <JwsItem> > SignWithUser(MachineReadableCode data, User credentials)
        {
            var request = new RestRequest("{Benutzername}/Sign/JWS", Method.POST, DataFormat.Json);

            request.AddUrlSegment("Benutzername", credentials.Username);

            request.AddJsonBody(new SignWithUserRequest(credentials.Password, data.GetCode()));

            var response = await _client.ExecuteAsync <SignWithUserResponse>(request);

            if (!response.IsSuccessful)
            {
                return(new Response <JwsItem>(false, null, response.StatusDescription));
            }

            return(new Response <JwsItem>(true, new JwsItem(response.Data.result), null));
        }
Exemplo n.º 4
0
        public async Task <IResponse <JwsItem> > Sign(MachineReadableCode data, ICredentials credentials)
        {
            if (data.Signature is not null)
            {
                return(new Response <JwsItem>(false, null, "MachineReadableCode should not have a signature"));
            }

            if (credentials is Session session)
            {
                return(await SignWithSession(data, session));
            }

            if (credentials is User user)
            {
                return(await SignWithUser(data, user));
            }

            return(new Response <JwsItem>(false, null, "Credentials are not of type User or Session, create with ICredentials"));
        }
Exemplo n.º 5
0
        private async Task <IResponse <JwsItem> > SignWithSession(MachineReadableCode data, Session credentials)
        {
            var request = new RestRequest("Session/{sessionId}/Sign/JWS", Method.POST, DataFormat.Json);

            request.AddUrlSegment("sessionId", credentials.SessionId);

            request.AddJsonBody(new SignWithSessionRequest(credentials.SessionKey, data.GetCode()));

            var response = await _client.ExecuteAsync <SignWithSessionResponse>(request);

            if (!response.IsSuccessful)
            {
                //SESSION CAN BE INACTIVE -> SIGN WITH USER/PASSWORD
                if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    return(await SignWithUser(data, credentials));
                }

                return(new Response <JwsItem>(false, null, response.StatusDescription));
            }

            return(new Response <JwsItem>(true, new JwsItem(response.Data.result), null));
        }