build script changes and package name change, svace issue fix
[platform/core/system/edge-home-orchestration-service.git] / src / account.c
1 /*******************************************************************************
2  * Copyright 2020 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  *******************************************************************************/
17
18 #include "account.h"
19 #include "homeedge.h"
20 #include "sa_cert.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <curl/curl.h>
25 #include <pthread.h>
26 #include <json-glib/json-glib.h>
27 #include <glib-object.h>
28
29 #define MAX_KEY_SIZE    2048
30
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;
36
37 typedef struct {
38         char *data;
39         size_t size;
40 } respData;
41
42 static void parse_response(void *user_data)
43 {
44         respData *response = (respData*) user_data;
45         JsonParser *jsonParser = NULL;
46         JsonNode *root;
47         JsonNode * node;
48         JsonObject *object;
49         GError *error = NULL;
50         char data[response->size+1];
51
52         strncpy(data, response->data, response->size);
53         data[response->size] = '\0';
54
55         jsonParser = json_parser_new();
56         if (!json_parser_load_from_data(jsonParser, data, -1, &error)) {
57                 g_error_free(error);
58                 g_object_unref(jsonParser);
59                 return ;
60         }
61
62         root = json_parser_get_root(jsonParser);
63         object = json_node_get_object(root);
64         node = json_object_get_member(object, "userId");
65         if (node) {
66                 char *val = NULL;
67                 val = (char*) json_node_get_string(node);
68                 if (val) {
69                         user_id = (char*) malloc(strlen(val) + 1);
70                         if (user_id) {
71                                 strncpy(user_id, val, strlen(val));
72                                 user_id[strlen(val)] = '\0';
73                         }
74                 }
75         }
76
77         g_object_unref(jsonParser);
78 }
79
80 static size_t _response_handler(void *contents, size_t size, size_t nmemb, void *userp)
81 {
82         printf("Http response recvd\n");
83         respData *response = (respData*) userp;
84         size_t totSize = nmemb * size;
85
86         if (NULL == response->data) {
87                         response->data = malloc(totSize);
88         } else {
89                         response->data = realloc(response->data, response->size + totSize);
90         }
91         memcpy(response->data + response->size, contents, totSize);
92         response->size += totSize;
93
94         return (totSize);
95 }
96
97 static void* _http_handler(void *user_data)
98 {
99         char url[MAX_SIZE] = {0, };
100         snprintf(url, sizeof(url), "https://%s/auth/oauth2/token", auth_server_url);
101
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);
104
105         CURL *curl;
106         CURLcode err;
107         curl = curl_easy_init();
108
109         if (curl) {
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);
117
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));
122                 } else {
123                         long code;
124                         curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
125                         dlog_print(DLOG_INFO, LOG_TAG, "http response code :: %ld\n", code);
126
127                         // Parse response
128                         respData *resp_data = (void*) user_data;
129                         if (resp_data) {
130                                 resp_data->data[resp_data->size] = '\0';
131                                 parse_response(user_data);
132
133                                 // generate new certificate
134                                 char cert[MAX_KEY_SIZE] = {0, };
135                                 snprintf(cert, sizeof(cert), "%s%s", (char*) getCertificate(), user_id);
136
137                                 resp_cb(ACCOUNT_STATUS_ERROR_NONE, user_id, cert);
138                         }
139                 }
140                 curl_easy_cleanup(curl);
141                 curl_slist_free_all(chunk);
142
143                 // free the response data
144                 respData *response = (respData*) user_data;
145                 if (response) {
146                         if (response->data) {
147                                 free(response->data);
148                         }
149                         free (response);
150                 }
151         }
152         return 0;
153 }
154
155 void account_getUserId(account_resp_cb cb, char *url, char *code)
156 {
157         resp_cb = cb;
158         if (url) {
159                 strncpy(auth_server_url, url, strlen(url));
160                 auth_server_url[strlen(url)] = '\0';
161         }
162         if (code) {
163                 strncpy(auth_code, code, strlen(code));
164                 auth_code[strlen(code)] = '\0';
165         }
166
167         respData *response = (respData*) malloc(sizeof(respData));
168         response->data = NULL;
169         response->size = 0;
170         pthread_create(&httpThread, NULL, &_http_handler, (void*) response);
171 }