1 /*******************************************************************************
2 * Copyright 2020 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 *******************************************************************************/
24 #include <curl/curl.h>
26 #include <json-glib/json-glib.h>
27 #include <glib-object.h>
29 #define MAX_KEY_SIZE 2048
31 static pthread_t httpThread;
32 static char auth_server_url[MAX_SIZE] = {0, };
33 static char auth_code[MAX_SIZE] = {0, };
34 static char *user_id = NULL;
35 static account_resp_cb resp_cb;
42 static void parse_response(void *user_data)
44 respData *response = (respData*) user_data;
45 JsonParser *jsonParser = NULL;
50 char data[response->size+1];
52 strncpy(data, response->data, response->size);
53 data[response->size] = '\0';
55 jsonParser = json_parser_new();
56 if (!json_parser_load_from_data(jsonParser, data, -1, &error)) {
58 g_object_unref(jsonParser);
62 root = json_parser_get_root(jsonParser);
63 object = json_node_get_object(root);
64 node = json_object_get_member(object, "userId");
67 val = (char*) json_node_get_string(node);
69 user_id = (char*) malloc(strlen(val) + 1);
71 strncpy(user_id, val, strlen(val));
72 user_id[strlen(val)] = '\0';
77 g_object_unref(jsonParser);
80 static size_t _response_handler(void *contents, size_t size, size_t nmemb, void *userp)
82 printf("Http response recvd\n");
83 respData *response = (respData*) userp;
84 size_t totSize = nmemb * size;
86 if (NULL == response->data) {
87 response->data = malloc(totSize);
89 response->data = realloc(response->data, response->size + totSize);
91 memcpy(response->data + response->size, contents, totSize);
92 response->size += totSize;
97 static void* _http_handler(void *user_data)
99 char url[MAX_SIZE] = {0, };
100 snprintf(url, sizeof(url), "https://%s/auth/oauth2/token", auth_server_url);
102 char body[MAX_SIZE] = {0, };
103 snprintf(body, sizeof(body), "grant_type=authorization_code&client_id=%s&client_secret=%s&code=%s", getClientId(), getClientSecret(), auth_code);
107 curl = curl_easy_init();
110 struct curl_slist *chunk = NULL;
111 chunk = curl_slist_append(chunk, "ContentType: application/x-www-form/urlencoded");
112 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
113 curl_easy_setopt(curl, CURLOPT_URL, url);
114 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
115 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_handler);
116 curl_easy_setopt(curl, CURLOPT_WRITEDATA, user_data);
118 err = curl_easy_perform(curl);
119 if (err != CURLE_OK) {
120 printf("http request failed : [%s]\n", curl_easy_strerror(err));
121 dlog_print(DLOG_INFO, LOG_TAG, "http request failed : [%s]\n", curl_easy_strerror(err));
124 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
125 dlog_print(DLOG_INFO, LOG_TAG, "http response code :: %ld\n", code);
128 respData *resp_data = (void*) user_data;
130 resp_data->data[resp_data->size] = '\0';
131 parse_response(user_data);
133 // generate new certificate
134 char cert[MAX_KEY_SIZE] = {0, };
135 snprintf(cert, sizeof(cert), "%s%s", (char*) getCertificate(), user_id);
137 resp_cb(ACCOUNT_STATUS_ERROR_NONE, user_id, cert);
140 curl_easy_cleanup(curl);
141 curl_slist_free_all(chunk);
143 // free the response data
144 respData *response = (respData*) user_data;
146 if (response->data) {
147 free(response->data);
155 void account_getUserId(account_resp_cb cb, char *url, char *code)
159 strncpy(auth_server_url, url, strlen(url));
160 auth_server_url[strlen(url)] = '\0';
163 strncpy(auth_code, code, strlen(code));
164 auth_code[strlen(code)] = '\0';
167 respData *response = (respData*) malloc(sizeof(respData));
168 response->data = NULL;
170 pthread_create(&httpThread, NULL, &_http_handler, (void*) response);