Merge branch 'notification-service'
[platform/upstream/iotivity.git] / resource / src / OCAccountManager.cpp
1 /* ****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "OCAccountManager.h"
22 #include "OCUtilities.h"
23
24 #include "ocstack.h"
25
26 namespace OC {
27
28 using OC::result_guard;
29 using OC::checked_guard;
30
31 OCAccountManager::OCAccountManager(std::weak_ptr<IClientWrapper> clientWrapper,
32                                    const std::string& host,
33                                    OCConnectivityType connectivityType)
34  : m_clientWrapper(clientWrapper), m_host(host), m_connType(connectivityType),
35    m_invitationObserveHandle(nullptr)
36 {
37     if (m_host.empty() || m_clientWrapper.expired())
38     {
39         throw OCException(OC::Exception::INVALID_PARAM);
40     }
41
42     const char* di = OCGetServerInstanceIDString();
43     if (!di)
44     {
45         oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
46         throw OCException(OC::Exception::INVALID_PARAM);
47     }
48
49     m_deviceID.append(di);
50     m_groupObserveHandles = {};
51     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, m_defaultQos);
52 }
53
54 OCAccountManager::~OCAccountManager()
55 {
56 }
57
58 std::string OCAccountManager::host() const
59 {
60     return m_host;
61 }
62
63 OCConnectivityType OCAccountManager::connectivityType() const
64 {
65     return m_connType;
66 }
67
68 OCStackResult OCAccountManager::signUp(const std::string& authProvider,
69                                        const std::string& authCode,
70                                        PostCallback cloudConnectHandler)
71 {
72     return result_guard(signUp(authProvider, authCode, QueryParamsMap(), cloudConnectHandler));
73 }
74
75 OCStackResult OCAccountManager::signUp(const std::string& authProvider,
76                                        const std::string& authCode,
77                                        const QueryParamsMap& options,
78                                        PostCallback cloudConnectHandler)
79 {
80     std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
81
82     OCRepresentation rep;
83     rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
84     rep.setValue(OC_RSRVD_AUTHPROVIDER, authProvider);
85     rep.setValue(OC_RSRVD_AUTHCODE, authCode);
86
87     if (!options.empty())
88     {
89         OCRepresentation optionsRep;
90         for (auto iter : options)
91         {
92             optionsRep[iter.first] = iter.second;
93         }
94         rep.setValue(OC_RSRVD_OPTIONS, optionsRep);
95     }
96
97     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
98                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
99                          m_connType, cloudConnectHandler, m_defaultQos);
100 }
101
102 OCStackResult OCAccountManager::signIn(const std::string& userUuid,
103                                        const std::string& accessToken,
104                                        PostCallback cloudConnectHandler)
105 {
106     return result_guard(signInOut(userUuid, accessToken, true, cloudConnectHandler));
107 }
108
109 OCStackResult OCAccountManager::signOut(PostCallback cloudConnectHandler)
110 {
111     return result_guard(signInOut("", "", false, cloudConnectHandler));
112 }
113
114 OCStackResult OCAccountManager::signInOut(const std::string& userUuid,
115                                           const std::string& accessToken,
116                                           bool isSignIn,
117                                           PostCallback cloudConnectHandler)
118 {
119     std::string uri = m_host + OC_RSRVD_ACCOUNT_SESSION_URI;
120
121     OCRepresentation rep;
122     if (isSignIn)
123     {
124         rep.setValue(OC_RSRVD_USER_UUID, userUuid);
125         rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
126         rep.setValue(OC_RSRVD_ACCESS_TOKEN, accessToken);
127     }
128     rep.setValue(OC_RSRVD_LOGIN, isSignIn);
129
130     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
131                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
132                          m_connType, cloudConnectHandler, m_defaultQos);
133 }
134
135 OCStackResult OCAccountManager::refreshAccessToken(const std::string& userUuid,
136                                                    const std::string& refreshToken,
137                                                    PostCallback cloudConnectHandler)
138 {
139     std::string uri = m_host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
140
141     OCRepresentation rep;
142     rep.setValue(OC_RSRVD_USER_UUID, userUuid);
143     rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
144     rep.setValue(OC_RSRVD_GRANT_TYPE, std::string(OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN));
145     rep.setValue(OC_RSRVD_REFRESH_TOKEN, refreshToken);
146
147     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
148                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
149                          m_connType, cloudConnectHandler, m_defaultQos);
150 }
151
152 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
153                                            GetCallback cloudConnectHandler)
154 {
155     return result_guard(searchUser(userUuid, QueryParamsMap(), cloudConnectHandler));
156 }
157
158 OCStackResult OCAccountManager::searchUser(const QueryParamsMap& queryParams,
159                                            GetCallback cloudConnectHandler)
160 {
161     return result_guard(searchUser("", queryParams, cloudConnectHandler));
162 }
163
164 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
165                                            const QueryParamsMap& queryParams,
166                                            GetCallback cloudConnectHandler)
167 {
168     std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
169
170     QueryParamsMap fullQuery = {};
171
172     if (!userUuid.empty())
173     {
174         fullQuery.insert(std::make_pair(OC_RSRVD_USER_UUID, userUuid));
175     }
176
177     if (!queryParams.empty())
178     {
179         std::string searchQuery;
180         for (auto iter : queryParams)
181         {
182             searchQuery.append(iter.first + ":" + iter.second + ",");
183         }
184         searchQuery.resize(searchQuery.size() - 1);
185         fullQuery.insert(std::make_pair(OC_RSRVD_SEARCH, searchQuery));
186     }
187
188     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
189                          OCDevAddr(), uri, fullQuery, HeaderOptions(),
190                          m_connType, cloudConnectHandler, m_defaultQos);
191 }
192
193 OCStackResult OCAccountManager::deleteDevice(const std::string& deviceId,
194                                              DeleteCallback cloudConnectHandler)
195 {
196     std::string uri = m_host + OC_RSRVD_ACCOUNT_URI
197                       + "?" + OC_RSRVD_DEVICE_ID + "=" + deviceId;
198
199     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
200                          OCDevAddr(), uri, HeaderOptions(),
201                          m_connType, cloudConnectHandler, m_defaultQos);
202 }
203
204 OCStackResult OCAccountManager::createGroup(AclGroupType groupType,
205                                             PostCallback cloudConnectHandler)
206 {
207     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI;
208
209     std::string gtype;
210     switch (groupType)
211     {
212         case AclGroupType::PUBLIC:
213             gtype = OC_RSRVD_PUBLIC;
214             break;
215         case AclGroupType::PRIVATE:
216             gtype = OC_RSRVD_PRIVATE;
217             break;
218         default:
219             return result_guard(OC_STACK_INVALID_PARAM);
220     }
221     OCRepresentation rep;
222     rep.setValue(OC_RSRVD_GROUP_TYPE, gtype);
223
224     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
225                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
226                          m_connType, cloudConnectHandler, m_defaultQos);
227 }
228
229 OCStackResult OCAccountManager::getGroupList(GetCallback cloudConnectHandler)
230 {
231     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI;
232
233     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
234                          OCDevAddr(), uri, QueryParamsMap(), HeaderOptions(),
235                          m_connType, cloudConnectHandler, m_defaultQos);
236 }
237
238 OCStackResult OCAccountManager::deleteGroup(const std::string& groupId,
239                                             DeleteCallback cloudConnectHandler)
240 {
241     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI
242                       + "?" + OC_RSRVD_GROUP_ID + "=" + groupId;
243
244     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
245                          OCDevAddr(), uri, HeaderOptions(),
246                          m_connType, cloudConnectHandler, m_defaultQos);
247 }
248
249 OCStackResult OCAccountManager::joinGroup(const std::string& groupId,
250                                           PostCallback cloudConnectHandler)
251 {
252     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
253
254     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
255                          OCDevAddr(), uri, OCRepresentation(), QueryParamsMap(), HeaderOptions(),
256                          m_connType, cloudConnectHandler, m_defaultQos);
257 }
258
259 OCStackResult OCAccountManager::addDeviceToGroup(const std::string& groupId,
260                                                  const std::vector<std::string>& deviceId,
261                                                  PostCallback cloudConnectHandler)
262 {
263     if (deviceId.empty())
264     {
265         return result_guard(OC_STACK_INVALID_PARAM);
266     }
267
268     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
269
270     OCRepresentation rep;
271     rep.setValue<std::vector<std::string>>(std::string(OC_RSRVD_DEVICE_ID_LIST), deviceId);
272
273     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
274                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
275                          m_connType, cloudConnectHandler, m_defaultQos);
276 }
277
278 OCStackResult OCAccountManager::getGroupInfo(const std::string& groupId,
279                                              GetCallback cloudConnectHandler)
280 {
281     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
282
283     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
284                          OCDevAddr(), uri, QueryParamsMap(), HeaderOptions(),
285                          m_connType, cloudConnectHandler, m_defaultQos);
286 }
287
288 OCStackResult OCAccountManager::leaveGroup(const std::string& groupId,
289                                            DeleteCallback cloudConnectHandler)
290 {
291     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
292
293     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
294                          OCDevAddr(), uri, HeaderOptions(),
295                          m_connType, cloudConnectHandler, m_defaultQos);
296 }
297
298 OCStackResult OCAccountManager::deleteDeviceFromGroup(const std::string& groupId,
299                                                       const std::vector<std::string>& deviceId,
300                                                       DeleteCallback cloudConnectHandler)
301 {
302     if (deviceId.empty())
303     {
304         return result_guard(OC_STACK_INVALID_PARAM);
305     }
306
307     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
308
309
310     uri.append("?");
311     for (auto iter : deviceId)
312     {
313         uri.append((std::string)OC_RSRVD_DEVICE_ID_LIST + "=" + iter + ";");
314     }
315     uri.resize(uri.size() - 1);
316
317     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
318                          OCDevAddr(), uri, HeaderOptions(),
319                          m_connType, cloudConnectHandler, m_defaultQos);
320 }
321
322 OCStackResult OCAccountManager::observeGroup(const std::string& groupId,
323                                              ObserveCallback cloudConnectHandler)
324 {
325     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
326
327     OCDoHandle handle = nullptr;
328
329     OCStackResult result = checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
330                                          ObserveType::Observe, &handle, OCDevAddr(), uri,
331                                          QueryParamsMap(), HeaderOptions(), cloudConnectHandler,
332                                          m_defaultQos);
333
334     if (OC_STACK_OK == result)
335     {
336         m_groupObserveHandles.insert(std::pair<std::string, OCDoHandle>(groupId, handle));
337     }
338
339     return result;
340
341 }
342
343 OCStackResult OCAccountManager::cancelObserveGroup(const std::string& groupId)
344 {
345     auto found = m_groupObserveHandles.find(groupId);
346     if (m_groupObserveHandles.end() == found)
347     {
348         return result_guard(OC_STACK_INVALID_PARAM);
349     }
350
351     OCDoHandle handle = found->second;
352
353     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
354
355     OCStackResult result = checked_guard(m_clientWrapper.lock(),
356                                          &IClientWrapper::CancelObserveResource, handle,
357                                          (const char*)"", uri, HeaderOptions(), m_defaultQos);
358
359     if (OC_STACK_OK == result)
360     {
361         m_groupObserveHandles.erase(groupId);
362         handle = nullptr;
363     }
364
365     return result;
366 }
367
368 OCStackResult OCAccountManager::observeInvitation(ObserveCallback cloudConnectHandler)
369 {
370     std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
371
372     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
373                          ObserveType::Observe, &m_invitationObserveHandle, OCDevAddr(), uri,
374                          QueryParamsMap(), HeaderOptions(), cloudConnectHandler, m_defaultQos);
375 }
376
377 OCStackResult OCAccountManager::cancelObserveInvitation()
378 {
379     if (nullptr == m_invitationObserveHandle)
380     {
381         return result_guard(OC_STACK_INVALID_PARAM);
382     }
383
384     std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
385
386     OCStackResult result = checked_guard(m_clientWrapper.lock(),
387                                          &IClientWrapper::CancelObserveResource,
388                                          m_invitationObserveHandle,
389                                          (const char*)"", uri, HeaderOptions(), m_defaultQos);
390
391     if (OC_STACK_OK == result)
392     {
393         m_invitationObserveHandle = nullptr;
394     }
395
396     return result;
397 }
398
399 OCStackResult OCAccountManager::sendInvitation(const std::string& groupId,
400                                                const std::string& userUuid,
401                                                PostCallback cloudConnectHandler)
402 {
403     std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
404
405     OCRepresentation invitation;
406     invitation.setValue(OC_RSRVD_GROUP_ID, groupId);
407     invitation.setValue(OC_RSRVD_MEMBER_ID, userUuid);
408
409     std::vector<OCRepresentation> invite{invitation};
410
411     OCRepresentation rep;
412     rep.setValue(OC_RSRVD_INVITE, invite);
413
414     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
415                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
416                          m_connType, cloudConnectHandler, m_defaultQos);
417 }
418
419 OCStackResult OCAccountManager::cancelInvitation(const std::string& groupId,
420                                                  const std::string& userUuid,
421                                                  DeleteCallback cloudConnectHandler)
422 {
423     std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI + "?" + OC_RSRVD_GROUP_ID + "=" + groupId
424                       + ";" + OC_RSRVD_MEMBER_ID + "=" + userUuid;
425
426     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
427                          OCDevAddr(), uri, HeaderOptions(),
428                          m_connType, cloudConnectHandler, m_defaultQos);
429 }
430
431 OCStackResult OCAccountManager::deleteInvitation(const std::string& groupId,
432                                                  DeleteCallback cloudConnectHandler)
433 {
434     std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI + "?" + OC_RSRVD_GROUP_ID + "=" + groupId;
435
436     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
437                          OCDevAddr(), uri, HeaderOptions(),
438                          m_connType, cloudConnectHandler, m_defaultQos);
439 }
440
441 } // namespace OC