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 * *****************************************************************/
23 #include "payload_logging.h"
25 #include "ocpayload.h"
26 //#include "psinterface.h"
27 #include "securevirtualresourcetypes.h"
28 #include "doxmresource.h"
29 #include "oic_malloc.h"
30 #include "oic_string.h"
31 #include "srmutility.h"
32 #include "pmutility.h"
33 #include "credresource.h"
34 #include "payload_logging.h"
37 #include "cloudAuth.h"
39 #define TAG "cloudAuth"
43 #define MAX_URI_QUERY MAX_URI_LENGTH + MAX_QUERY_LENGTH
57 static sessionObject_t sessionObject = {0,0,0,0,0,0,0,0};
59 extern void handleCB(void* ctx, OCStackResult result, void* data);
62 * Session free function
64 * @return OCStackResult application result
68 if (sessionObject.accesstoken) OICFree(sessionObject.accesstoken);
69 if (sessionObject.refreshtoken) OICFree(sessionObject.refreshtoken);
70 if (sessionObject.tokentype) OICFree(sessionObject.tokentype);
71 if (sessionObject.uid) OICFree(sessionObject.uid);
72 if (sessionObject.redirecturi) OICFree(sessionObject.redirecturi);
73 if (sessionObject.certificate) OICFree(sessionObject.certificate);
74 if (sessionObject.sid) OICFree(sessionObject.sid);
76 memset(&sessionObject, 0, sizeof(sessionObject_t));
80 * Session init function
82 * @param[in] sessionObject session data
83 * @return OCStackResult application result
85 OCStackResult SessionInit()
87 OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
91 OIC_LOG_V(DEBUG, TAG, "OUT: %s", __func__);
97 * Session parse payload
100 * @param[in] sessionObject session data
101 * @return OCStackResult application result
103 OCStackResult SessionParsePayload(OCRepPayload *payload)
105 OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
107 VERIFY_NON_NULL(TAG, payload, ERROR);
111 if (!OCRepPayloadGetPropString(payload, OC_RSRVD_ACCESS_TOKEN,
112 &sessionObject.accesstoken))
114 OIC_LOG(ERROR, TAG, "Can't get: accesstoken");
116 if (!OCRepPayloadGetPropString(payload, OC_RSRVD_REFRESH_TOKEN,
117 &sessionObject.refreshtoken))
119 OIC_LOG(ERROR, TAG, "Can't get: refreshtoken");
121 if (!OCRepPayloadGetPropString(payload, OC_RSRVD_TOKEN_TYPE,
122 &sessionObject.tokentype))
124 OIC_LOG(ERROR, TAG, "Can't get: tokentype");
126 if (!OCRepPayloadGetPropInt(payload, OC_RSRVD_EXPIRES_IN,
127 &(sessionObject.expiresin)))
129 OIC_LOG(ERROR, TAG, "Can't get: expiresin");
131 if (!OCRepPayloadGetPropString(payload, OC_RSRVD_USER_UUID,
134 OIC_LOG(ERROR, TAG, "Can't get: uid");
136 if (!OCRepPayloadGetPropString(payload, OC_RSRVD_REDIRECT_URI,
137 &sessionObject.redirecturi))
139 OIC_LOG(ERROR, TAG, "Can't get: redirecturi");
141 if (!OCRepPayloadGetPropString(payload, OC_RSRVD_CERTIFICATE,
142 &sessionObject.certificate))
144 OIC_LOG(ERROR, TAG, "Can't get: certificate");
146 if (!OCRepPayloadGetPropString(payload, OC_RSRVD_SUBJECT_ID,
149 OIC_LOG(ERROR, TAG, "Can't get: sid");
152 OIC_LOG_V(DEBUG, TAG, "OUT: %s", __func__);
158 * Cloud login handler
160 * @param[in] response login response
162 OCStackApplicationResult handleCloudSignUpResponse(void *ctx,
164 OCClientResponse *response)
171 OIC_LOG(ERROR, TAG, "Received NULL response!");
175 if (response->payload)
177 OIC_LOG(ERROR, TAG, "Payload received");
178 OIC_LOG_PAYLOAD(DEBUG, response->payload);
181 if (response->result != LOGIN_OK)
183 OIC_LOG_V(ERROR, TAG, "Login error: %d",response->result);
187 OIC_LOG(DEBUG, TAG, "Login successful");
188 SessionParsePayload((OCRepPayload*)response->payload);
191 handleCB(NULL, OC_STACK_OK, NULL);
192 return OC_STACK_DELETE_TRANSACTION;
196 * Cloud authentication
198 * @param[in] endPoint cloud host and port
199 * @param[in] authProvider auth provider
200 * @param[in] authToken token
201 * @return OCStackResult application result
204 CloudSignUp(OCDevAddr *endPoint,
205 const char *authProvider,
206 const char *authToken,
207 OCClientResponseHandler response)
209 char uri[MAX_URI_LENGTH] = { 0 };
211 OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
213 if (!endPoint || !authProvider || !authToken)
215 OIC_LOG(ERROR, TAG, "Some of the input params are NULL");
216 return OC_STACK_INVALID_PARAM;
219 char *deviceId = getDeviceId();
222 OIC_LOG(ERROR, TAG, "Can't get the device id");
223 return OC_STACK_ERROR;
226 snprintf(uri, MAX_URI_LENGTH, DEFAULT_QUERY,
227 endPoint->addr, endPoint->port, OC_RSRVD_ACCOUNT_URI);
229 OIC_LOG_V(DEBUG, TAG, "Query created: %s", uri);
231 OCCallbackData cbData;
232 memset(&cbData, 0, sizeof(OCCallbackData));
233 cbData.cb = response;
234 cbData.context = (void*)handleCloudSignUpResponse;
236 OCRepPayload *payload = OCRepPayloadCreate();
239 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
240 return OC_STACK_NO_MEMORY;
243 OCRepPayloadSetPropString(payload, OC_RSRVD_DEVICE_ID, deviceId);
244 OCRepPayloadSetPropString(payload, OC_RSRVD_AUTHPROVIDER, OICStrdup(authProvider));
245 OCRepPayloadSetPropString(payload, OC_RSRVD_AUTHCODE, OICStrdup(authToken));
247 return OCDoResource(NULL, OC_REST_POST, uri, NULL, (OCPayload *)payload,
248 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
252 * Cloud Sign In handler
254 * @param[in] OCClientResponse cloud response
255 * @return OCStackApplicationResult application result
257 OCStackApplicationResult handleCloudSignInResponse(void *ctx,
259 OCClientResponse *response)
261 OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
268 OIC_LOG(ERROR, TAG, "Received NULL response!");
272 if (response->payload)
274 OIC_LOG(ERROR, TAG, "Payload received");
275 OIC_LOG_PAYLOAD(DEBUG, response->payload);
278 if (response->result < 4 && response->payload)
280 OIC_LOG_V(ERROR, TAG, "Sign In error: result: %d, payload exist: %s",
281 response->result, response->payload ? "yes" : "no");
285 if (!OCRepPayloadGetPropString((OCRepPayload*)response->payload, OC_RSRVD_USER_UUID,
286 &(sessionObject.uid)))
288 OIC_LOG(ERROR, TAG, "Can't get: uid");
291 if (!OCRepPayloadGetPropInt((OCRepPayload*)response->payload, OC_RSRVD_EXPIRES_IN,
292 &(sessionObject.expiresin)))
294 OIC_LOG(ERROR, TAG, "Cann't get: expiresin");
297 OIC_LOG_V(DEBUG, TAG, "OUT: %s", __func__);
300 handleCB(NULL, OC_STACK_OK, NULL);
301 return OC_STACK_DELETE_TRANSACTION;
305 * Sign In request function
307 * @param[in] endPoint cloud host and port
308 * @return OCStackResult application result
310 OCStackResult CloudSignIn(OCDevAddr *endPoint, OCClientResponseHandler response)
312 OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
316 OIC_LOG_V(ERROR, TAG, "%s: endPoint is NULL",__func__);
317 return OC_STACK_INVALID_PARAM;
320 if (!sessionObject.uid)
322 OIC_LOG_V(ERROR, TAG, "%s: UID is missing. Please run Sign Up first",__func__);
323 return OC_STACK_ERROR;
326 if (!sessionObject.accesstoken)
328 OIC_LOG_V(ERROR, TAG, "%s: accesstoken is missing. Please run Sign Up first",__func__);
329 return OC_STACK_ERROR;
332 char *deviceId = getDeviceId();
335 OIC_LOG(ERROR, TAG, "Can't get the device id");
336 return OC_STACK_ERROR;
339 OCRepPayload* payload = OCRepPayloadCreate();
342 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
343 return OC_STACK_NO_MEMORY;
346 OCRepPayloadSetPropString(payload, OC_RSRVD_USER_UUID, sessionObject.uid);
347 OCRepPayloadSetPropString(payload, OC_RSRVD_DEVICE_ID, deviceId);
348 OCRepPayloadSetPropString(payload, OC_RSRVD_ACCESS_TOKEN,
349 sessionObject.accesstoken);
350 OCRepPayloadSetPropBool(payload, OC_RSRVD_LOGIN, true);
352 char uri[MAX_URI_QUERY] = { 0, };
353 snprintf(uri, MAX_URI_QUERY, DEFAULT_QUERY,
354 endPoint->addr, endPoint->port,
355 OC_RSRVD_ACCOUNT_SESSION_URI);
357 OCCallbackData cbData;
358 memset(&cbData, 0, sizeof(OCCallbackData));
359 cbData.cb = response;
360 cbData.context = (void*)handleCloudSignInResponse;
362 return OCDoResource(NULL, OC_REST_POST, uri, NULL,
363 (OCPayload *)payload,
365 OC_LOW_QOS, &cbData, NULL, 0);
368 * Cloud Sign Out handler
370 * @param[in] OCClientResponse cloud response
371 * @return OCStackApplicationResult application result
373 OCStackApplicationResult handleCloudSignOutResponse(void *ctx,
375 OCClientResponse *response)
377 OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
384 OIC_LOG(ERROR, TAG, "Received NULL response!");
388 if (response->payload)
390 OIC_LOG(ERROR, TAG, "Payload received");
391 OIC_LOG_PAYLOAD(DEBUG, response->payload);
394 if (response->result < 4 && response->payload)
396 OIC_LOG_V(ERROR, TAG, "Sign Out error");
397 return OC_STACK_DELETE_TRANSACTION;
401 OIC_LOG_V(ERROR, TAG, "Sign Out OK");
406 OIC_LOG_V(DEBUG, TAG, "OUT: %s", __func__);
409 handleCB(NULL, OC_STACK_OK, NULL);
410 return OC_STACK_DELETE_TRANSACTION;
413 * Sign In request function
415 * @param[in] endPoint cloud host and port
416 * @return OCStackResult application result
418 OCStackResult CloudSignOut(OCDevAddr *endPoint, OCClientResponseHandler response)
420 OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
424 OIC_LOG_V(ERROR, TAG, "%s: endPoint is NULL",__func__);
425 return OC_STACK_INVALID_PARAM;
428 char *deviceId = getDeviceId();
431 OIC_LOG(ERROR, TAG, "Cann't get the device id");
432 return OC_STACK_ERROR;
435 OCRepPayload* payload = OCRepPayloadCreate();
438 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
439 return OC_STACK_NO_MEMORY;
442 OCRepPayloadSetPropString(payload, OC_RSRVD_DEVICE_ID, deviceId);
443 OCRepPayloadSetPropString(payload, OC_RSRVD_ACCESS_TOKEN,
444 sessionObject.accesstoken);
445 OCRepPayloadSetPropBool(payload, OC_RSRVD_LOGIN, false);
447 char uri[MAX_URI_QUERY] = { 0, };
448 snprintf(uri, MAX_URI_QUERY, DEFAULT_QUERY,
449 endPoint->addr, endPoint->port,
450 OC_RSRVD_ACCOUNT_SESSION_URI);
452 OCCallbackData cbData;
453 memset(&cbData, 0, sizeof(OCCallbackData));
454 cbData.cb = response;
455 cbData.context = (void*)handleCloudSignOutResponse;
457 return OCDoResource(NULL, OC_REST_POST, uri, NULL,
458 (OCPayload *)payload,
460 OC_LOW_QOS, &cbData, NULL, 0);