Search in sources :

Example 1 with SignRequest

use of com.microsoft.azure.sdk.iot.device.hsm.parser.SignRequest in project azure-iot-sdk-java by Azure.

the class HttpHsmSignatureProviderTest method signSuccess.

// Codes_SRS_HTTPHSMSIGNATUREPROVIDER_34_006: [This function shall create a signRequest for the hsm http client to sign, and shall return the utf-8 encoded result of that signing.]
@Test
public void signSuccess(@Mocked URLEncoder mockedURLEncoder) throws NoSuchAlgorithmException, TransportException, IOException, URISyntaxException, HsmException {
    // arrange
    final String keyName = "keyName";
    final String data = "some data";
    final String expectedDigest = "some digest";
    final String expectedDigestEncoded = "some encoded digest";
    new NonStrictExpectations() {

        {
            new HttpsHsmClient(expectedProviderUri);
            result = mockedHttpsHsmClient;
            new SignRequest();
            result = mockedSignRequest;
            mockedHttpsHsmClient.sign(expectedApiVersion, keyName, mockedSignRequest, expectedGenId);
            result = mockedSignResponse;
            mockedSignResponse.getDigest();
            result = expectedDigest;
            URLEncoder.encode(expectedDigest, "UTF-8");
            result = expectedDigestEncoded;
        }
    };
    final HttpHsmSignatureProvider signatureProvider = new HttpHsmSignatureProvider(expectedProviderUri, expectedApiVersion);
    // act
    String actualDigest = signatureProvider.sign(keyName, data, expectedGenId);
    // assert
    assertEquals(expectedDigestEncoded, actualDigest);
    new Verifications() {

        {
            mockedSignRequest.setData(data.getBytes(StandardCharsets.UTF_8));
            mockedSignRequest.setKeyId("primary");
            mockedSignRequest.setAlgo((Mac) Deencapsulation.getField(signatureProvider, "defaultSignRequestAlgo"));
            mockedHttpsHsmClient.sign(expectedApiVersion, keyName, mockedSignRequest, expectedGenId);
            mockedSignResponse.getDigest();
        }
    };
}
Also used : HttpsHsmClient(com.microsoft.azure.sdk.iot.device.hsm.HttpsHsmClient) SignRequest(com.microsoft.azure.sdk.iot.device.hsm.parser.SignRequest) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpHsmSignatureProvider(com.microsoft.azure.sdk.iot.device.hsm.HttpHsmSignatureProvider) Test(org.junit.Test)

Example 2 with SignRequest

use of com.microsoft.azure.sdk.iot.device.hsm.parser.SignRequest in project azure-iot-sdk-java by Azure.

the class SignRequestTest method gettersAndSettersWork.

// Tests_SRS_HTTPHSMSIGNREQUEST_34_001: [This function shall save the provided keyId.]
// Tests_SRS_HTTPHSMSIGNREQUEST_34_002: [This function shall return the saved data.]
// Tests_SRS_HTTPHSMSIGNREQUEST_34_003: [This function shall save the provided data after base64 encoding it.]
// Tests_SRS_HTTPHSMSIGNREQUEST_34_004: [This function shall save the provided algo.]
@Test
public void gettersAndSettersWork() {
    // arrange
    final String expectedAlgoString = "some algorithm";
    final String expectedKeyId = "some key id";
    final byte[] expectedData = "some data".getBytes(StandardCharsets.UTF_8);
    final String expectedEncodedData = encodeBase64String(expectedData);
    new NonStrictExpectations() {

        {
            mockedMac.getAlgorithm();
            result = expectedAlgoString;
        }
    };
    SignRequest request = new SignRequest();
    // act
    request.setAlgo(mockedMac);
    request.setKeyId(expectedKeyId);
    request.setData(expectedData);
    // assert
    assertEquals(mockedMac, Deencapsulation.getField(request, "algo"));
    assertEquals(expectedKeyId, Deencapsulation.getField(request, "keyId"));
    assertArrayEquals(expectedEncodedData.getBytes(StandardCharsets.UTF_8), request.getData());
}
Also used : SignRequest(com.microsoft.azure.sdk.iot.device.hsm.parser.SignRequest) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 3 with SignRequest

use of com.microsoft.azure.sdk.iot.device.hsm.parser.SignRequest in project azure-iot-sdk-java by Azure.

the class HttpHsmSignatureProvider method sign.

/**
 * Sign the provided data using the provided key name
 * @param keyName the key used for signing
 * @param data the data to be signed
 * @param generationId the generation id
 * @return the signed data
 * @throws IOException If the http client cannot reach the signing party
 * @throws TransportException If the http client cannot reach the signing party
 * @throws URISyntaxException If the url for the signing party cannot be parsed
 */
public String sign(String keyName, String data, String generationId) throws IOException, TransportException, URISyntaxException, HsmException {
    if (data == null || data.isEmpty()) {
        // Codes_SRS_HTTPHSMSIGNATUREPROVIDER_34_007: [If the provided data is null or empty, this function shall throw an IllegalArgumentException.]
        throw new IllegalArgumentException("Data cannot be null or empty");
    }
    // Codes_SRS_HTTPHSMSIGNATUREPROVIDER_34_006: [This function shall create a signRequest for the hsm http client to sign, and shall return the utf-8 encoded result of that signing.]
    SignRequest signRequest = new SignRequest();
    signRequest.setAlgo(defaultSignRequestAlgo);
    signRequest.setData(data.getBytes(ENCODING_CHARSET));
    signRequest.setKeyId(DEFAULT_KEY_ID);
    SignResponse response = this.httpClient.sign(this.apiVersion, keyName, signRequest, generationId);
    return URLEncoder.encode(response.getDigest(), ENCODING_CHARSET);
}
Also used : SignRequest(com.microsoft.azure.sdk.iot.device.hsm.parser.SignRequest) SignResponse(com.microsoft.azure.sdk.iot.device.hsm.parser.SignResponse)

Aggregations

SignRequest (com.microsoft.azure.sdk.iot.device.hsm.parser.SignRequest)3 NonStrictExpectations (mockit.NonStrictExpectations)2 Test (org.junit.Test)2 HttpHsmSignatureProvider (com.microsoft.azure.sdk.iot.device.hsm.HttpHsmSignatureProvider)1 HttpsHsmClient (com.microsoft.azure.sdk.iot.device.hsm.HttpsHsmClient)1 SignResponse (com.microsoft.azure.sdk.iot.device.hsm.parser.SignResponse)1 Verifications (mockit.Verifications)1 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)1