Search in sources :

Example 1 with ProvisioningDeviceClientAuthenticationException

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

the class ProvisioningTask method invokeRegister.

private RegistrationOperationStatusParser invokeRegister() throws InterruptedException, ExecutionException, TimeoutException, ProvisioningDeviceClientException {
    RegisterTask registerTask = new RegisterTask(this.provisioningDeviceClientConfig, securityProvider, provisioningDeviceClientContract, authorization);
    FutureTask<RegistrationOperationStatusParser> futureRegisterTask = new FutureTask<>(registerTask);
    executor.submit(futureRegisterTask);
    RegistrationOperationStatusParser registrationOperationStatusParser = futureRegisterTask.get(MAX_TIME_TO_WAIT_FOR_REGISTRATION, TimeUnit.MILLISECONDS);
    if (registrationOperationStatusParser == null) {
        this.dpsStatus = PROVISIONING_DEVICE_STATUS_ERROR;
        throw new ProvisioningDeviceClientAuthenticationException("Registration response could not be retrieved, " + "authentication failure");
    }
    ProvisioningStatus status = ProvisioningStatus.fromString(registrationOperationStatusParser.getStatus());
    if (status == null) {
        this.dpsStatus = PROVISIONING_DEVICE_STATUS_ERROR;
        throw new ProvisioningDeviceClientAuthenticationException("Received null status for registration, " + "authentication failure");
    }
    if (registrationOperationStatusParser.getOperationId() == null) {
        throw new ProvisioningDeviceClientAuthenticationException("operation id could not be retrieved, " + "authentication failure");
    }
    return registrationOperationStatusParser;
}
Also used : RegistrationOperationStatusParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser) ProvisioningDeviceClientAuthenticationException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientAuthenticationException)

Example 2 with ProvisioningDeviceClientAuthenticationException

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

the class RegisterTask method authenticateWithSasToken.

private RegistrationOperationStatusParser authenticateWithSasToken(RequestData requestData) throws IOException, InterruptedException, ProvisioningDeviceClientException, SecurityProviderException {
    /*SRS_RegisterTask_25_014: [ If the provided security client is for Key then, this method shall construct SasToken by doing the following

            1. Build a tokenScope of format <scope>/registrations/<registrationId>
            2. Sign the HSM with the string of format <tokenScope>/n<expiryTime> and receive a token
            3. Encode the token to Base64 format and UrlEncode it to generate the signature. ]*/
    String sasToken = this.constructSasToken();
    requestData.setSasToken(sasToken);
    // SRS_RegisterTask_25_016: [ If the provided security client is for Key then, this method shall trigger authenticateWithProvisioningService on the contract API using the sasToken generated and wait for response and return it. ]
    ResponseData responseDataForSasTokenAuth = new ResponseData();
    this.provisioningDeviceClientContract.authenticateWithProvisioningService(requestData, responseCallback, responseDataForSasTokenAuth);
    waitForResponse(responseDataForSasTokenAuth);
    if (responseDataForSasTokenAuth.getResponseData() != null && responseDataForSasTokenAuth.getContractState() == DPS_REGISTRATION_RECEIVED) {
        this.authorization.setSasToken(sasToken);
        String jsonBody = new String(responseDataForSasTokenAuth.getResponseData(), StandardCharsets.UTF_8);
        try {
            return RegistrationOperationStatusParser.createFromJson(jsonBody);
        } catch (IllegalArgumentException e) {
            ProvisioningErrorParser provisioningErrorParser = ProvisioningErrorParser.createFromJson(jsonBody);
            throw new ProvisioningDeviceClientException(provisioningErrorParser.getExceptionMessage());
        }
    } else {
        // SRS_RegisterTask_25_017: [ If the provided security client is for Key then, this method shall throw ProvisioningDeviceClientException if null response to authenticateWithProvisioningService is received. ]
        throw new ProvisioningDeviceClientAuthenticationException("Service did not authorize SasToken");
    }
}
Also used : ProvisioningErrorParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser) ProvisioningDeviceClientAuthenticationException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientAuthenticationException) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)

Example 3 with ProvisioningDeviceClientAuthenticationException

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

the class ProvisioningTask method invokeStatus.

private RegistrationOperationStatusParser invokeStatus(String operationId) throws TimeoutException, InterruptedException, ExecutionException, ProvisioningDeviceClientException {
    Thread.sleep(provisioningDeviceClientContract.getRetryValue());
    StatusTask statusTask = new StatusTask(securityProvider, provisioningDeviceClientContract, provisioningDeviceClientConfig, operationId, this.authorization);
    FutureTask<RegistrationOperationStatusParser> futureStatusTask = new FutureTask<>(statusTask);
    executor.submit(futureStatusTask);
    RegistrationOperationStatusParser statusRegistrationOperationStatusParser = futureStatusTask.get(MAX_TIME_TO_WAIT_FOR_STATUS_UPDATE, TimeUnit.MILLISECONDS);
    if (statusRegistrationOperationStatusParser == null) {
        this.dpsStatus = PROVISIONING_DEVICE_STATUS_ERROR;
        throw new ProvisioningDeviceClientAuthenticationException("Status response could not be retrieved, " + "authentication failure");
    }
    if (statusRegistrationOperationStatusParser.getStatus() == null) {
        this.dpsStatus = PROVISIONING_DEVICE_STATUS_ERROR;
        throw new ProvisioningDeviceClientAuthenticationException("Status could not be retrieved, " + "authentication failure");
    }
    if (ProvisioningStatus.fromString(statusRegistrationOperationStatusParser.getStatus()) == null) {
        this.dpsStatus = PROVISIONING_DEVICE_STATUS_ERROR;
        throw new ProvisioningDeviceClientAuthenticationException("Status could not be retrieved, " + "authentication failure");
    }
    return statusRegistrationOperationStatusParser;
}
Also used : RegistrationOperationStatusParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser) ProvisioningDeviceClientAuthenticationException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientAuthenticationException)

Example 4 with ProvisioningDeviceClientAuthenticationException

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

the class RegisterTask method authenticateWithTPM.

private RegistrationOperationStatusParser authenticateWithTPM(RequestData requestData) throws ProvisioningDeviceClientException, SecurityProviderException {
    try {
        if (securityProvider instanceof SecurityProviderTpm) {
            SecurityProviderTpm securityClientTpm = (SecurityProviderTpm) securityProvider;
            // SRS_RegisterTask_25_011: [ If the provided security client is for Key then, this method shall trigger authenticateWithTPM on the contract API and wait for Authentication Key and decode it from Base64. Also this method shall pass the exception back to the user if it fails. ]
            ResponseData nonceResponseData = new ResponseData();
            log.debug("Requesting service nonce for tpm authentication");
            this.provisioningDeviceClientContract.requestNonceForTPM(requestData, responseCallback, nonceResponseData);
            waitForResponse(nonceResponseData);
            if (nonceResponseData.getContractState() == DPS_REGISTRATION_RECEIVED) {
                if (nonceResponseData.getResponseData() != null) {
                    // SRS_RegisterTask_25_018: [ If the provided security client is for Key then, this method shall import the Base 64 encoded Authentication Key into the HSM using the security client and pass the exception to the user on failure. ]
                    log.debug("Received service nonce, activating tpm identity key with it");
                    securityClientTpm.activateIdentityKey(nonceResponseData.getResponseData());
                } else {
                    // SRS_RegisterTask_25_013: [ If the provided security client is for Key then, this method shall throw ProvisioningDeviceClientException if Authentication Key received is null. ]
                    throw new ProvisioningDeviceClientAuthenticationException("Service did not send authentication key");
                }
                log.debug("Authenticating with device provisioning service using the activated tpm identity key");
                return authenticateWithSasToken(requestData);
            } else {
                // SRS_RegisterTask_25_012: [ If the provided security client is for Key then, this method shall throw ProvisioningDeviceClientException if null response is received. ]
                throw new ProvisioningDeviceClientException("Did not receive DPS registration nonce successfully");
            }
        } else {
            throw new ProvisioningDeviceClientException("could not identify security provider");
        }
    } catch (IOException | InterruptedException e) {
        throw new ProvisioningDeviceClientException(e);
    }
}
Also used : SecurityProviderTpm(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm) ProvisioningDeviceClientAuthenticationException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientAuthenticationException) IOException(java.io.IOException) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)

Aggregations

ProvisioningDeviceClientAuthenticationException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientAuthenticationException)4 ProvisioningDeviceClientException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)2 RegistrationOperationStatusParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser)2 ProvisioningErrorParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser)1 SecurityProviderTpm (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm)1 IOException (java.io.IOException)1