Microsoft Azure IoTHub client libraries
Dependents: sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more
This library implements the Microsoft Azure IoTHub client library. The code is replicated from https://github.com/Azure/azure-iot-sdks
iothub_client_authorization.c@63:1bf1c2d60aab, 2017-04-06 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Thu Apr 06 14:11:13 2017 -0700
- Revision:
- 63:1bf1c2d60aab
- Parent:
- 62:5a4cdacf5090
- Child:
- 75:86205ca63a59
1.1.11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 62:5a4cdacf5090 | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 62:5a4cdacf5090 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 62:5a4cdacf5090 | 3 | |
AzureIoTClient | 62:5a4cdacf5090 | 4 | #include <stdlib.h> |
AzureIoTClient | 62:5a4cdacf5090 | 5 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 62:5a4cdacf5090 | 6 | #include "azure_c_shared_utility/macro_utils.h" |
AzureIoTClient | 62:5a4cdacf5090 | 7 | #include "azure_c_shared_utility/umock_c_prod.h" |
AzureIoTClient | 62:5a4cdacf5090 | 8 | #include "azure_c_shared_utility/crt_abstractions.h" |
AzureIoTClient | 62:5a4cdacf5090 | 9 | #include "azure_c_shared_utility/agenttime.h" |
AzureIoTClient | 62:5a4cdacf5090 | 10 | #include "azure_c_shared_utility/xlogging.h" |
AzureIoTClient | 62:5a4cdacf5090 | 11 | #include "azure_c_shared_utility/strings.h" |
AzureIoTClient | 62:5a4cdacf5090 | 12 | #include "azure_c_shared_utility/sastoken.h" |
AzureIoTClient | 62:5a4cdacf5090 | 13 | |
AzureIoTClient | 62:5a4cdacf5090 | 14 | #include "iothub_client_authorization.h" |
AzureIoTClient | 62:5a4cdacf5090 | 15 | |
AzureIoTClient | 62:5a4cdacf5090 | 16 | #define DEFAULT_SAS_TOKEN_EXPIRY_TIME_SECS 3600 |
AzureIoTClient | 62:5a4cdacf5090 | 17 | #define INDEFINITE_TIME ((time_t)(-1)) |
AzureIoTClient | 62:5a4cdacf5090 | 18 | |
AzureIoTClient | 62:5a4cdacf5090 | 19 | typedef struct IOTHUB_AUTHORIZATION_DATA_TAG |
AzureIoTClient | 62:5a4cdacf5090 | 20 | { |
AzureIoTClient | 62:5a4cdacf5090 | 21 | char* device_sas_token; |
AzureIoTClient | 62:5a4cdacf5090 | 22 | char* device_key; |
AzureIoTClient | 62:5a4cdacf5090 | 23 | char* device_id; |
AzureIoTClient | 62:5a4cdacf5090 | 24 | size_t token_expiry_time_sec; |
AzureIoTClient | 62:5a4cdacf5090 | 25 | IOTHUB_CREDENTIAL_TYPE cred_type; |
AzureIoTClient | 62:5a4cdacf5090 | 26 | } IOTHUB_AUTHORIZATION_DATA; |
AzureIoTClient | 62:5a4cdacf5090 | 27 | |
AzureIoTClient | 62:5a4cdacf5090 | 28 | static int get_seconds_since_epoch(size_t* seconds) |
AzureIoTClient | 62:5a4cdacf5090 | 29 | { |
AzureIoTClient | 62:5a4cdacf5090 | 30 | int result; |
AzureIoTClient | 62:5a4cdacf5090 | 31 | time_t current_time; |
AzureIoTClient | 62:5a4cdacf5090 | 32 | if ((current_time = get_time(NULL)) == INDEFINITE_TIME) |
AzureIoTClient | 62:5a4cdacf5090 | 33 | { |
AzureIoTClient | 62:5a4cdacf5090 | 34 | LogError("Failed getting the current local time (get_time() failed)"); |
AzureIoTClient | 62:5a4cdacf5090 | 35 | result = __LINE__; |
AzureIoTClient | 62:5a4cdacf5090 | 36 | } |
AzureIoTClient | 62:5a4cdacf5090 | 37 | else |
AzureIoTClient | 62:5a4cdacf5090 | 38 | { |
AzureIoTClient | 62:5a4cdacf5090 | 39 | *seconds = (size_t)get_difftime(current_time, (time_t)0); |
AzureIoTClient | 62:5a4cdacf5090 | 40 | result = 0; |
AzureIoTClient | 62:5a4cdacf5090 | 41 | } |
AzureIoTClient | 62:5a4cdacf5090 | 42 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 43 | } |
AzureIoTClient | 62:5a4cdacf5090 | 44 | |
AzureIoTClient | 62:5a4cdacf5090 | 45 | IOTHUB_AUTHORIZATION_HANDLE IoTHubClient_Auth_Create(const char* device_key, const char* device_id, const char* device_sas_token) |
AzureIoTClient | 62:5a4cdacf5090 | 46 | { |
AzureIoTClient | 62:5a4cdacf5090 | 47 | IOTHUB_AUTHORIZATION_DATA* result; |
AzureIoTClient | 62:5a4cdacf5090 | 48 | /* Codes_SRS_IoTHub_Authorization_07_001: [if device_id is NULL IoTHubClient_Auth_Create, shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 49 | if (device_id == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 50 | { |
AzureIoTClient | 62:5a4cdacf5090 | 51 | LogError("Invalid Parameter device_id: %p", device_key, device_id); |
AzureIoTClient | 62:5a4cdacf5090 | 52 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 53 | } |
AzureIoTClient | 62:5a4cdacf5090 | 54 | else |
AzureIoTClient | 62:5a4cdacf5090 | 55 | { |
AzureIoTClient | 62:5a4cdacf5090 | 56 | /* Codes_SRS_IoTHub_Authorization_07_002: [IoTHubClient_Auth_Create shall allocate a IOTHUB_AUTHORIZATION_HANDLE that is needed for subsequent calls. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 57 | result = (IOTHUB_AUTHORIZATION_DATA*)malloc(sizeof(IOTHUB_AUTHORIZATION_DATA) ); |
AzureIoTClient | 62:5a4cdacf5090 | 58 | if (result == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 59 | { |
AzureIoTClient | 62:5a4cdacf5090 | 60 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 61 | LogError("Failed allocating IOTHUB_AUTHORIZATION_DATA"); |
AzureIoTClient | 62:5a4cdacf5090 | 62 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 63 | } |
AzureIoTClient | 62:5a4cdacf5090 | 64 | else |
AzureIoTClient | 62:5a4cdacf5090 | 65 | { |
AzureIoTClient | 62:5a4cdacf5090 | 66 | memset(result, 0, sizeof(IOTHUB_AUTHORIZATION_DATA) ); |
AzureIoTClient | 62:5a4cdacf5090 | 67 | result->token_expiry_time_sec = DEFAULT_SAS_TOKEN_EXPIRY_TIME_SECS; |
AzureIoTClient | 62:5a4cdacf5090 | 68 | |
AzureIoTClient | 62:5a4cdacf5090 | 69 | if (device_key != NULL && mallocAndStrcpy_s(&result->device_key, device_key) != 0) |
AzureIoTClient | 62:5a4cdacf5090 | 70 | { |
AzureIoTClient | 62:5a4cdacf5090 | 71 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 72 | LogError("Failed allocating device_key"); |
AzureIoTClient | 62:5a4cdacf5090 | 73 | free(result); |
AzureIoTClient | 62:5a4cdacf5090 | 74 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 75 | } |
AzureIoTClient | 62:5a4cdacf5090 | 76 | else if (mallocAndStrcpy_s(&result->device_id, device_id) != 0) |
AzureIoTClient | 62:5a4cdacf5090 | 77 | { |
AzureIoTClient | 62:5a4cdacf5090 | 78 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 79 | LogError("Failed allocating device_key"); |
AzureIoTClient | 62:5a4cdacf5090 | 80 | free(result->device_key); |
AzureIoTClient | 62:5a4cdacf5090 | 81 | free(result); |
AzureIoTClient | 62:5a4cdacf5090 | 82 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 83 | } |
AzureIoTClient | 62:5a4cdacf5090 | 84 | else |
AzureIoTClient | 62:5a4cdacf5090 | 85 | { |
AzureIoTClient | 63:1bf1c2d60aab | 86 | if (device_key != NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 87 | { |
AzureIoTClient | 62:5a4cdacf5090 | 88 | /* Codes_SRS_IoTHub_Authorization_07_003: [ IoTHubClient_Auth_Create shall set the credential type to IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY if the device_sas_token is NULL. ]*/ |
AzureIoTClient | 62:5a4cdacf5090 | 89 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY; |
AzureIoTClient | 62:5a4cdacf5090 | 90 | } |
AzureIoTClient | 63:1bf1c2d60aab | 91 | else if (device_sas_token != NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 92 | { |
AzureIoTClient | 62:5a4cdacf5090 | 93 | /* Codes_SRS_IoTHub_Authorization_07_020: [ else IoTHubClient_Auth_Create shall set the credential type to IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 94 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN; |
AzureIoTClient | 62:5a4cdacf5090 | 95 | if (mallocAndStrcpy_s(&result->device_sas_token, device_sas_token) != 0) |
AzureIoTClient | 62:5a4cdacf5090 | 96 | { |
AzureIoTClient | 62:5a4cdacf5090 | 97 | /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 98 | LogError("Failed allocating device_key"); |
AzureIoTClient | 62:5a4cdacf5090 | 99 | free(result->device_key); |
AzureIoTClient | 62:5a4cdacf5090 | 100 | free(result->device_id); |
AzureIoTClient | 62:5a4cdacf5090 | 101 | free(result); |
AzureIoTClient | 62:5a4cdacf5090 | 102 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 103 | } |
AzureIoTClient | 62:5a4cdacf5090 | 104 | } |
AzureIoTClient | 63:1bf1c2d60aab | 105 | else |
AzureIoTClient | 63:1bf1c2d60aab | 106 | { |
AzureIoTClient | 63:1bf1c2d60aab | 107 | /* Codes_SRS_IoTHub_Authorization_07_024: [ if device_sas_token and device_key are NULL IoTHubClient_Auth_Create shall set the credential type to IOTHUB_CREDENTIAL_TYPE_UNKNOWN. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 108 | result->cred_type = IOTHUB_CREDENTIAL_TYPE_UNKNOWN; |
AzureIoTClient | 63:1bf1c2d60aab | 109 | } |
AzureIoTClient | 62:5a4cdacf5090 | 110 | } |
AzureIoTClient | 62:5a4cdacf5090 | 111 | } |
AzureIoTClient | 62:5a4cdacf5090 | 112 | } |
AzureIoTClient | 62:5a4cdacf5090 | 113 | /* Codes_SRS_IoTHub_Authorization_07_004: [ If successful IoTHubClient_Auth_Create shall return a IOTHUB_AUTHORIZATION_HANDLE value. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 114 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 115 | } |
AzureIoTClient | 62:5a4cdacf5090 | 116 | |
AzureIoTClient | 62:5a4cdacf5090 | 117 | |
AzureIoTClient | 62:5a4cdacf5090 | 118 | void IoTHubClient_Auth_Destroy(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 62:5a4cdacf5090 | 119 | { |
AzureIoTClient | 62:5a4cdacf5090 | 120 | /* Codes_SRS_IoTHub_Authorization_07_005: [ if handle is NULL IoTHubClient_Auth_Destroy shall do nothing. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 121 | if (handle != NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 122 | { |
AzureIoTClient | 62:5a4cdacf5090 | 123 | /* Codes_SRS_IoTHub_Authorization_07_006: [ IoTHubClient_Auth_Destroy shall free all resources associated with the IOTHUB_AUTHORIZATION_HANDLE handle. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 124 | free(handle->device_key); |
AzureIoTClient | 62:5a4cdacf5090 | 125 | free(handle->device_id); |
AzureIoTClient | 62:5a4cdacf5090 | 126 | free(handle->device_sas_token); |
AzureIoTClient | 62:5a4cdacf5090 | 127 | free(handle); |
AzureIoTClient | 62:5a4cdacf5090 | 128 | } |
AzureIoTClient | 62:5a4cdacf5090 | 129 | } |
AzureIoTClient | 62:5a4cdacf5090 | 130 | |
AzureIoTClient | 63:1bf1c2d60aab | 131 | IOTHUB_CREDENTIAL_TYPE IoTHubClient_Auth_Set_x509_Type(IOTHUB_AUTHORIZATION_HANDLE handle, bool enable_x509) |
AzureIoTClient | 63:1bf1c2d60aab | 132 | { |
AzureIoTClient | 63:1bf1c2d60aab | 133 | IOTHUB_CREDENTIAL_TYPE result; |
AzureIoTClient | 63:1bf1c2d60aab | 134 | if (handle != NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 135 | { |
AzureIoTClient | 63:1bf1c2d60aab | 136 | if (enable_x509) |
AzureIoTClient | 63:1bf1c2d60aab | 137 | { |
AzureIoTClient | 63:1bf1c2d60aab | 138 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_X509; |
AzureIoTClient | 63:1bf1c2d60aab | 139 | } |
AzureIoTClient | 63:1bf1c2d60aab | 140 | else |
AzureIoTClient | 63:1bf1c2d60aab | 141 | { |
AzureIoTClient | 63:1bf1c2d60aab | 142 | if (handle->device_sas_token == NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 143 | { |
AzureIoTClient | 63:1bf1c2d60aab | 144 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY; |
AzureIoTClient | 63:1bf1c2d60aab | 145 | } |
AzureIoTClient | 63:1bf1c2d60aab | 146 | else if (handle->device_key == NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 147 | { |
AzureIoTClient | 63:1bf1c2d60aab | 148 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN; |
AzureIoTClient | 63:1bf1c2d60aab | 149 | } |
AzureIoTClient | 63:1bf1c2d60aab | 150 | else |
AzureIoTClient | 63:1bf1c2d60aab | 151 | { |
AzureIoTClient | 63:1bf1c2d60aab | 152 | result = handle->cred_type = IOTHUB_CREDENTIAL_TYPE_UNKNOWN; |
AzureIoTClient | 63:1bf1c2d60aab | 153 | } |
AzureIoTClient | 63:1bf1c2d60aab | 154 | } |
AzureIoTClient | 63:1bf1c2d60aab | 155 | } |
AzureIoTClient | 63:1bf1c2d60aab | 156 | else |
AzureIoTClient | 63:1bf1c2d60aab | 157 | { |
AzureIoTClient | 63:1bf1c2d60aab | 158 | result = IOTHUB_CREDENTIAL_TYPE_UNKNOWN; |
AzureIoTClient | 63:1bf1c2d60aab | 159 | } |
AzureIoTClient | 63:1bf1c2d60aab | 160 | return result; |
AzureIoTClient | 63:1bf1c2d60aab | 161 | } |
AzureIoTClient | 63:1bf1c2d60aab | 162 | |
AzureIoTClient | 62:5a4cdacf5090 | 163 | IOTHUB_CREDENTIAL_TYPE IoTHubClient_Auth_Get_Credential_Type(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 62:5a4cdacf5090 | 164 | { |
AzureIoTClient | 62:5a4cdacf5090 | 165 | IOTHUB_CREDENTIAL_TYPE result; |
AzureIoTClient | 62:5a4cdacf5090 | 166 | /* Codes_SRS_IoTHub_Authorization_07_007: [ if handle is NULL IoTHub_Auth_Get_Credential_Type shall return IOTHUB_CREDENTIAL_TYPE_UNKNOWN. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 167 | if (handle == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 168 | { |
AzureIoTClient | 62:5a4cdacf5090 | 169 | LogError("Invalid Parameter handle: %p", handle); |
AzureIoTClient | 62:5a4cdacf5090 | 170 | result = IOTHUB_CREDENTIAL_TYPE_UNKNOWN; |
AzureIoTClient | 62:5a4cdacf5090 | 171 | } |
AzureIoTClient | 62:5a4cdacf5090 | 172 | else |
AzureIoTClient | 62:5a4cdacf5090 | 173 | { |
AzureIoTClient | 62:5a4cdacf5090 | 174 | /* Codes_SRS_IoTHub_Authorization_07_008: [ IoTHub_Auth_Get_Credential_Type shall return the credential type that is set upon creation. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 175 | result = handle->cred_type; |
AzureIoTClient | 62:5a4cdacf5090 | 176 | } |
AzureIoTClient | 62:5a4cdacf5090 | 177 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 178 | } |
AzureIoTClient | 62:5a4cdacf5090 | 179 | |
AzureIoTClient | 62:5a4cdacf5090 | 180 | char* IoTHubClient_Auth_Get_SasToken(IOTHUB_AUTHORIZATION_HANDLE handle, const char* scope, size_t expire_time) |
AzureIoTClient | 62:5a4cdacf5090 | 181 | { |
AzureIoTClient | 62:5a4cdacf5090 | 182 | char* result; |
AzureIoTClient | 63:1bf1c2d60aab | 183 | /* Codes_SRS_IoTHub_Authorization_07_009: [ if handle or scope are NULL, IoTHubClient_Auth_Get_SasToken shall return NULL. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 184 | if (handle == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 185 | { |
AzureIoTClient | 63:1bf1c2d60aab | 186 | LogError("Invalid Parameter handle: %p", handle); |
AzureIoTClient | 62:5a4cdacf5090 | 187 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 188 | } |
AzureIoTClient | 62:5a4cdacf5090 | 189 | else |
AzureIoTClient | 62:5a4cdacf5090 | 190 | { |
AzureIoTClient | 63:1bf1c2d60aab | 191 | /* Codes_SRS_IoTHub_Authorization_07_021: [If the device_sas_token is NOT NULL IoTHubClient_Auth_Get_SasToken shall return a copy of the device_sas_token. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 192 | if (handle->device_sas_token != NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 193 | { |
AzureIoTClient | 63:1bf1c2d60aab | 194 | if (mallocAndStrcpy_s(&result, handle->device_sas_token) != 0) |
AzureIoTClient | 63:1bf1c2d60aab | 195 | { |
AzureIoTClient | 63:1bf1c2d60aab | 196 | LogError("failure allocating sas token", scope); |
AzureIoTClient | 63:1bf1c2d60aab | 197 | result = NULL; |
AzureIoTClient | 63:1bf1c2d60aab | 198 | } |
AzureIoTClient | 63:1bf1c2d60aab | 199 | } |
AzureIoTClient | 63:1bf1c2d60aab | 200 | /* Codes_SRS_IoTHub_Authorization_07_009: [ if handle or scope are NULL, IoTHubClient_Auth_Get_SasToken shall return NULL. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 201 | else if (scope == NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 202 | { |
AzureIoTClient | 63:1bf1c2d60aab | 203 | LogError("Invalid Parameter scope: %p", scope); |
AzureIoTClient | 62:5a4cdacf5090 | 204 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 205 | } |
AzureIoTClient | 63:1bf1c2d60aab | 206 | else |
AzureIoTClient | 62:5a4cdacf5090 | 207 | { |
AzureIoTClient | 63:1bf1c2d60aab | 208 | const char* key_name = ""; |
AzureIoTClient | 63:1bf1c2d60aab | 209 | STRING_HANDLE sas_token; |
AzureIoTClient | 63:1bf1c2d60aab | 210 | size_t sec_since_epoch; |
AzureIoTClient | 63:1bf1c2d60aab | 211 | |
AzureIoTClient | 63:1bf1c2d60aab | 212 | /* Codes_SRS_IoTHub_Authorization_07_010: [ IoTHubClient_Auth_Get_ConnString shall construct the expiration time using the expire_time. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 213 | if (get_seconds_since_epoch(&sec_since_epoch) != 0) |
AzureIoTClient | 62:5a4cdacf5090 | 214 | { |
AzureIoTClient | 62:5a4cdacf5090 | 215 | /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 216 | LogError("failure getting seconds from epoch"); |
AzureIoTClient | 62:5a4cdacf5090 | 217 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 218 | } |
AzureIoTClient | 63:1bf1c2d60aab | 219 | else |
AzureIoTClient | 62:5a4cdacf5090 | 220 | { |
AzureIoTClient | 63:1bf1c2d60aab | 221 | /* Codes_SRS_IoTHub_Authorization_07_011: [ IoTHubClient_Auth_Get_ConnString shall call SASToken_CreateString to construct the sas token. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 222 | size_t expiry_time = sec_since_epoch+expire_time; |
AzureIoTClient | 63:1bf1c2d60aab | 223 | if ( (sas_token = SASToken_CreateString(handle->device_key, scope, key_name, expiry_time)) == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 224 | { |
AzureIoTClient | 62:5a4cdacf5090 | 225 | /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 226 | LogError("Failed creating sas_token"); |
AzureIoTClient | 62:5a4cdacf5090 | 227 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 228 | } |
AzureIoTClient | 63:1bf1c2d60aab | 229 | else |
AzureIoTClient | 63:1bf1c2d60aab | 230 | { |
AzureIoTClient | 63:1bf1c2d60aab | 231 | /* Codes_SRS_IoTHub_Authorization_07_012: [ On success IoTHubClient_Auth_Get_ConnString shall allocate and return the sas token in a char*. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 232 | if (mallocAndStrcpy_s(&result, STRING_c_str(sas_token) ) != 0) |
AzureIoTClient | 63:1bf1c2d60aab | 233 | { |
AzureIoTClient | 63:1bf1c2d60aab | 234 | /* Codes_SRS_IoTHub_Authorization_07_020: [ If any error is encountered IoTHubClient_Auth_Get_ConnString shall return NULL. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 235 | LogError("Failed copying result"); |
AzureIoTClient | 63:1bf1c2d60aab | 236 | result = NULL; |
AzureIoTClient | 63:1bf1c2d60aab | 237 | } |
AzureIoTClient | 63:1bf1c2d60aab | 238 | STRING_delete(sas_token); |
AzureIoTClient | 63:1bf1c2d60aab | 239 | } |
AzureIoTClient | 62:5a4cdacf5090 | 240 | } |
AzureIoTClient | 62:5a4cdacf5090 | 241 | } |
AzureIoTClient | 62:5a4cdacf5090 | 242 | } |
AzureIoTClient | 62:5a4cdacf5090 | 243 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 244 | } |
AzureIoTClient | 62:5a4cdacf5090 | 245 | |
AzureIoTClient | 62:5a4cdacf5090 | 246 | const char* IoTHubClient_Auth_Get_DeviceId(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 62:5a4cdacf5090 | 247 | { |
AzureIoTClient | 62:5a4cdacf5090 | 248 | const char* result; |
AzureIoTClient | 62:5a4cdacf5090 | 249 | if (handle == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 250 | { |
AzureIoTClient | 62:5a4cdacf5090 | 251 | /* Codes_SRS_IoTHub_Authorization_07_013: [ if handle is NULL, IoTHubClient_Auth_Get_DeviceId shall return NULL. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 252 | LogError("Invalid Parameter handle: %p", handle); |
AzureIoTClient | 62:5a4cdacf5090 | 253 | result = NULL; |
AzureIoTClient | 62:5a4cdacf5090 | 254 | } |
AzureIoTClient | 62:5a4cdacf5090 | 255 | else |
AzureIoTClient | 62:5a4cdacf5090 | 256 | { |
AzureIoTClient | 62:5a4cdacf5090 | 257 | /* Codes_SRS_IoTHub_Authorization_07_014: [ IoTHubClient_Auth_Get_DeviceId shall return the device_id specified upon creation. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 258 | result = handle->device_id; |
AzureIoTClient | 62:5a4cdacf5090 | 259 | } |
AzureIoTClient | 62:5a4cdacf5090 | 260 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 261 | } |
AzureIoTClient | 62:5a4cdacf5090 | 262 | |
AzureIoTClient | 63:1bf1c2d60aab | 263 | const char* IoTHubClient_Auth_Get_DeviceKey(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 62:5a4cdacf5090 | 264 | { |
AzureIoTClient | 63:1bf1c2d60aab | 265 | const char* result; |
AzureIoTClient | 63:1bf1c2d60aab | 266 | if (handle == NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 267 | { |
AzureIoTClient | 63:1bf1c2d60aab | 268 | /* Codes_SRS_IoTHub_Authorization_07_022: [ if handle is NULL, IoTHubClient_Auth_Get_DeviceKey shall return NULL. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 269 | LogError("Invalid Parameter handle: %p", handle); |
AzureIoTClient | 63:1bf1c2d60aab | 270 | result = NULL; |
AzureIoTClient | 63:1bf1c2d60aab | 271 | } |
AzureIoTClient | 63:1bf1c2d60aab | 272 | else |
AzureIoTClient | 63:1bf1c2d60aab | 273 | { |
AzureIoTClient | 63:1bf1c2d60aab | 274 | /* Codes_SRS_IoTHub_Authorization_07_023: [ IoTHubClient_Auth_Get_DeviceKey shall return the device_key specified upon creation. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 275 | result = handle->device_key; |
AzureIoTClient | 63:1bf1c2d60aab | 276 | } |
AzureIoTClient | 63:1bf1c2d60aab | 277 | return result; |
AzureIoTClient | 63:1bf1c2d60aab | 278 | } |
AzureIoTClient | 63:1bf1c2d60aab | 279 | |
AzureIoTClient | 63:1bf1c2d60aab | 280 | SAS_TOKEN_STATUS IoTHubClient_Auth_Is_SasToken_Valid(IOTHUB_AUTHORIZATION_HANDLE handle) |
AzureIoTClient | 63:1bf1c2d60aab | 281 | { |
AzureIoTClient | 63:1bf1c2d60aab | 282 | SAS_TOKEN_STATUS result; |
AzureIoTClient | 62:5a4cdacf5090 | 283 | if (handle == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 284 | { |
AzureIoTClient | 62:5a4cdacf5090 | 285 | /* Codes_SRS_IoTHub_Authorization_07_015: [ if handle is NULL, IoTHubClient_Auth_Is_SasToken_Valid shall return false. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 286 | LogError("Invalid Parameter handle: %p", handle); |
AzureIoTClient | 63:1bf1c2d60aab | 287 | result = SAS_TOKEN_STATUS_FAILED; |
AzureIoTClient | 62:5a4cdacf5090 | 288 | } |
AzureIoTClient | 62:5a4cdacf5090 | 289 | else |
AzureIoTClient | 62:5a4cdacf5090 | 290 | { |
AzureIoTClient | 62:5a4cdacf5090 | 291 | if (handle->cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN) |
AzureIoTClient | 62:5a4cdacf5090 | 292 | { |
AzureIoTClient | 62:5a4cdacf5090 | 293 | if (handle->device_sas_token == NULL) |
AzureIoTClient | 62:5a4cdacf5090 | 294 | { |
AzureIoTClient | 62:5a4cdacf5090 | 295 | /* Codes_SRS_IoTHub_Authorization_07_017: [ If the sas_token is NULL IoTHubClient_Auth_Is_SasToken_Valid shall return false. ] */ |
AzureIoTClient | 63:1bf1c2d60aab | 296 | LogError("Failure: device_sas_toke is NULL"); |
AzureIoTClient | 63:1bf1c2d60aab | 297 | result = SAS_TOKEN_STATUS_FAILED; |
AzureIoTClient | 62:5a4cdacf5090 | 298 | } |
AzureIoTClient | 62:5a4cdacf5090 | 299 | else |
AzureIoTClient | 62:5a4cdacf5090 | 300 | { |
AzureIoTClient | 62:5a4cdacf5090 | 301 | /* Codes_SRS_IoTHub_Authorization_07_018: [ otherwise IoTHubClient_Auth_Is_SasToken_Valid shall return the value returned by SASToken_Validate. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 302 | STRING_HANDLE strSasToken = STRING_construct(handle->device_sas_token); |
AzureIoTClient | 63:1bf1c2d60aab | 303 | if (strSasToken != NULL) |
AzureIoTClient | 63:1bf1c2d60aab | 304 | { |
AzureIoTClient | 63:1bf1c2d60aab | 305 | if (!SASToken_Validate(strSasToken)) |
AzureIoTClient | 63:1bf1c2d60aab | 306 | { |
AzureIoTClient | 63:1bf1c2d60aab | 307 | result = SAS_TOKEN_STATUS_INVALID; |
AzureIoTClient | 63:1bf1c2d60aab | 308 | } |
AzureIoTClient | 63:1bf1c2d60aab | 309 | else |
AzureIoTClient | 63:1bf1c2d60aab | 310 | { |
AzureIoTClient | 63:1bf1c2d60aab | 311 | result = SAS_TOKEN_STATUS_VALID; |
AzureIoTClient | 63:1bf1c2d60aab | 312 | } |
AzureIoTClient | 63:1bf1c2d60aab | 313 | STRING_delete(strSasToken); |
AzureIoTClient | 63:1bf1c2d60aab | 314 | } |
AzureIoTClient | 63:1bf1c2d60aab | 315 | else |
AzureIoTClient | 63:1bf1c2d60aab | 316 | { |
AzureIoTClient | 63:1bf1c2d60aab | 317 | LogError("Failure constructing SAS Token"); |
AzureIoTClient | 63:1bf1c2d60aab | 318 | result = SAS_TOKEN_STATUS_FAILED; |
AzureIoTClient | 63:1bf1c2d60aab | 319 | } |
AzureIoTClient | 62:5a4cdacf5090 | 320 | } |
AzureIoTClient | 62:5a4cdacf5090 | 321 | } |
AzureIoTClient | 62:5a4cdacf5090 | 322 | else |
AzureIoTClient | 62:5a4cdacf5090 | 323 | { |
AzureIoTClient | 62:5a4cdacf5090 | 324 | /* Codes_SRS_IoTHub_Authorization_07_016: [ if credential type is not IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN IoTHubClient_Auth_Is_SasToken_Valid shall return true. ] */ |
AzureIoTClient | 62:5a4cdacf5090 | 325 | result = true; |
AzureIoTClient | 62:5a4cdacf5090 | 326 | } |
AzureIoTClient | 62:5a4cdacf5090 | 327 | } |
AzureIoTClient | 62:5a4cdacf5090 | 328 | return result; |
AzureIoTClient | 62:5a4cdacf5090 | 329 | } |