Search in sources :

Example 1 with TpmRegistrationResultParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser in project azure-iot-sdk-java by Azure.

the class RegistrationOperationStatusParserTest method constructorForTPMRegistrationResultCreatesFromJsonThrowsOnEmpty.

@Test(expected = IllegalArgumentException.class)
public void constructorForTPMRegistrationResultCreatesFromJsonThrowsOnEmpty() throws IllegalArgumentException, JsonParseException {
    final String json = "";
    TpmRegistrationResultParser tpmRegistrationResultParser = TpmRegistrationResultParser.createFromJson(json);
}
Also used : TpmRegistrationResultParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser) Test(org.junit.Test)

Example 2 with TpmRegistrationResultParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser in project azure-iot-sdk-java by Azure.

the class RegistrationOperationStatusParserTest method constructorForTPMRegistrationResultCreatesFromJsonThrowsOnMalformedJson.

// SRS_TpmRegistrationResultParser_25_004: [ The constructor shall throw IllegalArgumentException if the provided Json could not be parsed. ]
@Test(expected = IllegalArgumentException.class)
public void constructorForTPMRegistrationResultCreatesFromJsonThrowsOnMalformedJson() throws IllegalArgumentException, JsonParseException {
    final String json = "{" + "\"authenticationKey\":\"" + TEST_AUTH_KEY + "\"";
    TpmRegistrationResultParser tpmRegistrationResultParser = TpmRegistrationResultParser.createFromJson(json);
}
Also used : TpmRegistrationResultParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser) Test(org.junit.Test)

Example 3 with TpmRegistrationResultParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser in project azure-iot-sdk-java by Azure.

the class ContractAPIHttp method requestNonceForTPM.

/**
 * Requests hub to provide a device key to begin authentication over HTTP (Only for TPM)
 * @param responseCallback A non {@code null} value for the callback
 * @param dpsAuthorizationCallbackContext An object for context. Can be {@code null}
 * @param requestData A non {@code null} value with all the required request data
 * @throws ProvisioningDeviceClientException If any of the parameters are invalid ({@code null} or empty)
 * @throws ProvisioningDeviceTransportException If any of the API calls to transport fail
 * @throws ProvisioningDeviceHubException If hub responds back with status other than 300 or less
 */
public synchronized void requestNonceForTPM(RequestData requestData, ResponseCallback responseCallback, Object dpsAuthorizationCallbackContext) throws ProvisioningDeviceClientException {
    // SRS_ContractAPIHttp_25_003: [If either registrationId, sslcontext or responseCallback is null or if registrationId is empty then this method shall throw ProvisioningDeviceClientException.]
    if (requestData.getRegistrationId() == null || requestData.getRegistrationId().isEmpty()) {
        throw new ProvisioningDeviceClientException(new IllegalArgumentException("registration Id cannot be null or empty"));
    }
    if (requestData.getEndorsementKey() == null) {
        throw new ProvisioningDeviceClientException(new IllegalArgumentException("Endorsement key cannot be null"));
    }
    if (requestData.getStorageRootKey() == null) {
        throw new ProvisioningDeviceClientException(new IllegalArgumentException("Storage root key cannot be null"));
    }
    if (requestData.getSslContext() == null) {
        throw new ProvisioningDeviceClientException(new IllegalArgumentException("sslContext cannot be null"));
    }
    if (responseCallback == null) {
        throw new ProvisioningDeviceClientException(new IllegalArgumentException("responseCallback cannot be null"));
    }
    try {
        // SRS_ContractAPIHttp_25_004: [This method shall retrieve the Url by calling 'generateRegisterUrl' on an object for UrlPathBuilder.]
        String url = new UrlPathBuilder(this.hostName, this.idScope, ProvisioningDeviceClientTransportProtocol.HTTPS).generateRegisterUrl(requestData.getRegistrationId());
        String base64EncodedEk = new String(encodeBase64(requestData.getEndorsementKey()), StandardCharsets.UTF_8);
        String base64EncodedSrk = new String(encodeBase64(requestData.getStorageRootKey()), StandardCharsets.UTF_8);
        // SRS_ContractAPIHttp_25_025: [ This method shall build the required Json input using parser. ]
        byte[] payload = new DeviceRegistrationParser(requestData.getRegistrationId(), requestData.getPayload(), base64EncodedEk, base64EncodedSrk).toJson().getBytes(StandardCharsets.UTF_8);
        // SRS_ContractAPIHttp_25_005: [This method shall prepare the PUT request by setting following headers on a HttpRequest 1. User-Agent : User Agent String for the SDK 2. Accept : "application/json" 3. Content-Type: "application/json; charset=utf-8".]
        HttpRequest httpRequest = this.prepareRequest(new URL(url), HttpMethod.PUT, payload, null);
        // SRS_ContractAPIHttp_25_006: [This method shall set the SSLContext for the Http Request.]
        httpRequest.setSSLContext(requestData.getSslContext());
        HttpResponse httpResponse = null;
        try {
            // SRS_ContractAPIHttp_25_007: [This method shall send http request and verify the status by calling 'ProvisioningDeviceClientExceptionManager.verifyHttpResponse'.]
            httpResponse = httpRequest.send();
            ProvisioningDeviceClientExceptionManager.verifyHttpResponse(httpResponse);
        } catch (ProvisioningDeviceHubException e) {
            // SRS_ContractAPIHttp_25_008: [If service return a status as 404 then this method shall trigger the callback to the user with the response message.]
            if (httpResponse.getStatus() == ACCEPTABLE_NONCE_HTTP_STATUS) {
                String tpmRegistrationResultJson = new String(httpResponse.getErrorReason(), StandardCharsets.UTF_8);
                TpmRegistrationResultParser registerResponseTPMParser = TpmRegistrationResultParser.createFromJson(tpmRegistrationResultJson);
                byte[] base64DecodedAuthKey = decodeBase64(registerResponseTPMParser.getAuthenticationKey().getBytes(StandardCharsets.UTF_8));
                responseCallback.run(new ResponseData(base64DecodedAuthKey, ContractState.DPS_REGISTRATION_RECEIVED, 0), dpsAuthorizationCallbackContext);
                return;
            } else {
                // SRS_ContractAPIHttp_25_009: [If service return any other status other than 404 then this method shall throw ProvisioningDeviceTransportException in case of 202 or ProvisioningDeviceHubException on any other status.]
                throw e;
            }
        }
    } catch (IOException e) {
        throw new ProvisioningDeviceTransportException(e);
    }
    throw new ProvisioningDeviceTransportException("Service did not return any authorization request");
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) ResponseData(com.microsoft.azure.sdk.iot.provisioning.device.internal.task.ResponseData) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) TpmRegistrationResultParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser) IOException(java.io.IOException) URL(java.net.URL) UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) DeviceRegistrationParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationParser)

Example 4 with TpmRegistrationResultParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser in project azure-iot-sdk-java by Azure.

the class RegistrationOperationStatusParserTest method constructorForTPMRegistrationResultCreatesFromJson.

// SRS_TpmRegistrationResultParser_25_001: [ This method returns the authentication key. ]
// SRS_TpmRegistrationResultParser_25_003: [ The constructor shall build this object from the provided Json. ]
@Test
public void constructorForTPMRegistrationResultCreatesFromJson() throws IllegalArgumentException, JsonParseException {
    final String json = "{" + "\"authenticationKey\":\"" + TEST_AUTH_KEY + "\"}";
    TpmRegistrationResultParser tpmRegistrationResultParser = TpmRegistrationResultParser.createFromJson(json);
    assertNotNull(tpmRegistrationResultParser.getAuthenticationKey());
    assertEquals(TEST_AUTH_KEY, tpmRegistrationResultParser.getAuthenticationKey());
}
Also used : TpmRegistrationResultParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser) Test(org.junit.Test)

Example 5 with TpmRegistrationResultParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser in project azure-iot-sdk-java by Azure.

the class RegistrationOperationStatusParserTest method constructorForTPMRegistrationResultCreatesFromJsonThrowsOnNull.

// SRS_TpmRegistrationResultParser_25_002: [ The constructor shall throw IllegalArgumentException if the provided Json is null or empty. ]
@Test(expected = IllegalArgumentException.class)
public void constructorForTPMRegistrationResultCreatesFromJsonThrowsOnNull() throws IllegalArgumentException, JsonParseException {
    final String json = null;
    TpmRegistrationResultParser tpmRegistrationResultParser = TpmRegistrationResultParser.createFromJson(json);
}
Also used : TpmRegistrationResultParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser) Test(org.junit.Test)

Aggregations

TpmRegistrationResultParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser)5 Test (org.junit.Test)4 HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)1 HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)1 UrlPathBuilder (com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder)1 DeviceRegistrationParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationParser)1 ResponseData (com.microsoft.azure.sdk.iot.provisioning.device.internal.task.ResponseData)1 IOException (java.io.IOException)1 URL (java.net.URL)1