1 /* *****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
24 #include "occloudprovisioning.h"
27 #include "oic_malloc.h"
30 #include "ocpayload.h"
31 #include "pmutility.h"
32 #include "cacommonutil.h"
34 #define TAG "OIC_CLOUD_ACL_INVITE"
37 * This helper function parses "name" : { "gid":[], "mid":[] } payload
39 * @param[in] payload received payload
40 * @param[in] name property name
41 * @param[out] out string array pair to fill
42 * @return OCStackResult application result
44 static OCStackResult parseInvitePayload(const OCRepPayload *payload, const char *name, stringArrayPair_t *out)
46 OCStackResult result = OC_STACK_NO_MEMORY;
47 size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
48 OCRepPayload **heplerPayload = NULL;
51 if (!OCRepPayloadGetPropObjectArray(payload, name, &heplerPayload, dimensions))
53 OIC_LOG_V(ERROR, TAG, "Can't get: %s", name);
54 return OC_STACK_MALFORMED_RESPONSE;
57 size_t count = calcDimTotal(dimensions);
59 stringArray_t *gidlist = &out->gidlist;
60 stringArray_t *midlist = &out->midlist;
62 gidlist->length = count;
63 midlist->length = count;
65 gidlist->array = OICCalloc(gidlist->length, sizeof(char *));
66 if (NULL == gidlist->array)
68 OIC_LOG(ERROR, TAG, "Can't allocate gidlist->array");
72 midlist->array = OICCalloc(midlist->length, sizeof(char *));
73 if (NULL == midlist->array)
75 OIC_LOG(ERROR, TAG, "Can't allocate midlist->array");
79 for (i = 0; i < gidlist->length; i++)
81 const OCRepPayload *payload = heplerPayload[i];
83 if (!OCRepPayloadGetPropString(payload, OC_RSRVD_GROUP_ID, &gidlist->array[i]))
85 OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_GROUP_ID);
86 result = OC_STACK_MALFORMED_RESPONSE;
90 if (!OCRepPayloadGetPropString(payload, OC_RSRVD_MEMBER_ID, &midlist->array[i]))
92 OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_MEMBER_ID);
93 result = OC_STACK_MALFORMED_RESPONSE;
96 OCRepPayloadDestroy(heplerPayload[i]);
102 if (result != OC_STACK_OK)
104 clearStringArray(gidlist);
105 clearStringArray(midlist);
107 for (size_t k = i; k < gidlist->length; k++)
109 OCRepPayloadDestroy(heplerPayload[i]);
112 OICFree(heplerPayload);
117 * ACL get invitation request received data handler
119 * @param[in] ctx context
120 * @param[out] data data required to external application
121 * @param[in] response peer response
122 * @return OCStackResult application result
124 static OCStackResult handleAclGetInvitationResponse(void *ctx, void **data, OCClientResponse *response)
127 OCStackResult result = OC_STACK_OK;
129 if (NULL == response->payload)
131 OIC_LOG(ERROR, TAG, "Receive NULL payload");
132 return OC_STACK_INVALID_PARAM;
135 inviteResponse_t *answer = OICCalloc(1, sizeof(inviteResponse_t));
138 OIC_LOG(ERROR, TAG, "Can't allocate answer");
139 return OC_STACK_NO_MEMORY;
142 const OCRepPayload *payload = (const OCRepPayload *)response->payload;
144 result = parseInvitePayload(payload, OC_RSRVD_INVITE, &answer->invite);
145 if (result != OC_STACK_OK)
150 result = parseInvitePayload(payload, OC_RSRVD_INVITED, &answer->invited);
151 if (result != OC_STACK_OK)
162 * ACL policy check request received data handler
164 * @param[in] ctx context
165 * @param[out] data data required to external application
166 * @param[in] response peer response
167 * @return OCStackResult application result
169 static OCStackResult handleAclPolicyCheckResponse(void *ctx, void **data, OCClientResponse *response)
173 if (NULL == response->payload)
175 OIC_LOG(ERROR, TAG, "Receive NULL payload");
176 return OC_STACK_INVALID_PARAM;
181 if (!OCRepPayloadGetPropString((const OCRepPayload *)response->payload, OC_RSRVD_GROUP_PERMISSION, &gp))
183 OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_GROUP_PERMISSION);
184 return OC_STACK_MALFORMED_RESPONSE;
191 OCStackResult OCCloudAclInviteUser(void* ctx,
193 const stringArray_t *groupIds,
194 const stringArray_t *memberIds,
195 const OCDevAddr *endPoint,
196 OCCloudResponseCB callback)
198 OCStackResult result = OC_STACK_ERROR;
199 char uri[MAX_URI_LENGTH] = { 0 };
202 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
203 VERIFY_NON_NULL_RET(groupIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
204 VERIFY_NON_NULL_RET(memberIds, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
206 if (groupIds->length != memberIds->length)
208 OIC_LOG(ERROR, TAG, "members and groups lists should have the same length!!!");
209 return OC_STACK_INVALID_PARAM;
212 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
213 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
215 OCCallbackData cbData;
216 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
218 OCRepPayload *payload = OCRepPayloadCreate();
221 return OC_STACK_NO_MEMORY;
224 OCRepPayload **heplerPayload = OICCalloc(groupIds->length, sizeof(OCRepPayload *));
226 for (i = 0; i < groupIds->length; i++)
228 heplerPayload[i] = OCRepPayloadCreate();
229 if (!heplerPayload[i])
233 OCRepPayloadSetPropString(heplerPayload[i], OC_RSRVD_GROUP_ID, groupIds->array[i]);
234 OCRepPayloadSetPropString(heplerPayload[i], OC_RSRVD_MEMBER_ID, memberIds->array[i]);
237 //add next fields if they were filled
238 if (userId) OCRepPayloadSetPropString(payload, OC_RSRVD_USER_UUID, userId);
240 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {groupIds->length, 0, 0};
241 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_INVITE,
242 (const struct OCRepPayload **)heplerPayload, dimensions);
244 return OCDoResource(NULL, OC_REST_POST, uri, NULL, (OCPayload *)payload,
245 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
247 OCRepPayloadDestroy(payload);
248 for (size_t k = 0; k < i; k++)
250 OCRepPayloadDestroy(heplerPayload[k]);
252 OCRepPayloadDestroy(*heplerPayload);
256 OCStackResult OCCloudAclGetInvitation(void* ctx,
258 const OCDevAddr *endPoint,
259 OCCloudResponseCB callback)
261 char uri[MAX_URI_LENGTH] = { 0 };
263 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
265 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
266 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
270 size_t len = strlen(uri);
271 snprintf(uri + len, MAX_URI_LENGTH -len, "?%s=%s", OC_RSRVD_USER_UUID, userId);
274 OCCallbackData cbData;
275 fillCallbackData(&cbData, ctx, callback, handleAclGetInvitationResponse, NULL);
277 return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
278 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
281 OCStackResult OCCloudAclDeleteInvitation(void* ctx,
284 const OCDevAddr *endPoint,
285 OCCloudResponseCB callback)
287 char uri[MAX_URI_LENGTH] = { 0 };
289 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
290 VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
292 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
293 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
297 size_t len = strlen(uri);
298 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_USER_UUID, userId);
301 size_t len = strlen(uri);
302 snprintf(uri + len, MAX_URI_LENGTH - len, "%c%s=%s", userId?'&':'?', OC_RSRVD_GROUP_ID, groupId);
304 OCCallbackData cbData;
305 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
307 return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
308 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
311 OCStackResult OCCloudAclCancelInvitation(void* ctx,
314 const char *memberId,
315 const OCDevAddr *endPoint,
316 OCCloudResponseCB callback)
318 char uri[MAX_URI_LENGTH] = { 0 };
321 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
322 VERIFY_NON_NULL_RET(groupId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
323 VERIFY_NON_NULL_RET(memberId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
325 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
326 endPoint->addr, endPoint->port, OC_RSRVD_ACL_INVITE_URL);
330 size_t len = strlen(uri);
331 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_USER_UUID, userId);
335 snprintf(uri + len, MAX_URI_LENGTH - len, "%c%s=%s", userId?'&':'?', OC_RSRVD_GROUP_ID, groupId);
337 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_MEMBER_ID, memberId);
339 OCCallbackData cbData;
340 fillCallbackData(&cbData, ctx, callback, NULL, NULL);
342 return OCDoResource(NULL, OC_REST_DELETE, uri, NULL, NULL,
343 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
346 OCStackResult OCCloudAclPolicyCheck(void* ctx,
347 const char *subjectId,
348 const char *deviceId,
350 const char *user_uri,
351 const OCDevAddr *endPoint,
352 OCCloudResponseCB callback)
354 char uri[MAX_URI_LENGTH] = { 0 };
357 VERIFY_NON_NULL_RET(endPoint, TAG, "NULL endpoint", OC_STACK_INVALID_PARAM);
358 VERIFY_NON_NULL_RET(subjectId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
359 VERIFY_NON_NULL_RET(deviceId, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
360 VERIFY_NON_NULL_RET(method, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
361 VERIFY_NON_NULL_RET(user_uri, TAG, "NULL input param", OC_STACK_INVALID_PARAM);
363 snprintf(uri, MAX_URI_LENGTH, "%s%s:%d%s", DEFAULT_PREFIX,
364 endPoint->addr, endPoint->port, OC_RSRVD_ACL_VERIFY_URL);
367 snprintf(uri + len, MAX_URI_LENGTH - len, "?%s=%s", OC_RSRVD_SUBJECT_ID, subjectId);
369 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_DEVICE_ID, deviceId);
371 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_REQUEST_METHOD, method);
373 snprintf(uri + len, MAX_URI_LENGTH - len, "&%s=%s", OC_RSRVD_REQUEST_URI, user_uri);
375 OCCallbackData cbData;
376 fillCallbackData(&cbData, ctx, callback, handleAclPolicyCheckResponse, NULL);
378 return OCDoResource(NULL, OC_REST_GET, uri, NULL, NULL,
379 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);