669917f4f92b97d991d41745bbb0c1d3bca6477c
[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     if (authProvider.empty() || authCode.empty())
81     {
82         return result_guard(OC_STACK_INVALID_PARAM);
83     }
84
85     std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
86
87     OCRepresentation rep;
88     rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
89     rep.setValue(OC_RSRVD_AUTHPROVIDER, authProvider);
90     rep.setValue(OC_RSRVD_AUTHCODE, authCode);
91
92     if (!options.empty())
93     {
94         for (auto iter : options)
95         {
96             rep.setValue(iter.first, iter.second);
97         }
98     }
99
100     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
101                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
102                          m_connType, cloudConnectHandler, m_defaultQos);
103 }
104
105 OCStackResult OCAccountManager::signIn(const std::string& userUuid,
106                                        const std::string& accessToken,
107                                        PostCallback cloudConnectHandler)
108 {
109     if (userUuid.empty() || accessToken.empty())
110     {
111         return result_guard(OC_STACK_INVALID_PARAM);
112     }
113
114     return result_guard(signInOut(userUuid, accessToken, true, cloudConnectHandler));
115 }
116
117 OCStackResult OCAccountManager::signOut(PostCallback cloudConnectHandler)
118 {
119     return result_guard(signInOut("", "", false, cloudConnectHandler));
120 }
121
122 OCStackResult OCAccountManager::signInOut(const std::string& userUuid,
123                                           const std::string& accessToken,
124                                           bool isSignIn,
125                                           PostCallback cloudConnectHandler)
126 {
127     std::string uri = m_host + OC_RSRVD_ACCOUNT_SESSION_URI;
128
129     OCRepresentation rep;
130     if (isSignIn)
131     {
132         rep.setValue(OC_RSRVD_USER_UUID, userUuid);
133         rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
134         rep.setValue(OC_RSRVD_ACCESS_TOKEN, accessToken);
135     }
136     rep.setValue(OC_RSRVD_LOGIN, isSignIn);
137
138     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
139                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
140                          m_connType, cloudConnectHandler, m_defaultQos);
141 }
142
143 OCStackResult OCAccountManager::refreshAccessToken(const std::string& userUuid,
144                                                    const std::string& refreshToken,
145                                                    PostCallback cloudConnectHandler)
146 {
147     if (userUuid.empty() || refreshToken.empty())
148     {
149         return result_guard(OC_STACK_INVALID_PARAM);
150     }
151
152     std::string uri = m_host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
153
154     OCRepresentation rep;
155     rep.setValue(OC_RSRVD_USER_UUID, userUuid);
156     rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
157     rep.setValue(OC_RSRVD_GRANT_TYPE, std::string(OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN));
158     rep.setValue(OC_RSRVD_REFRESH_TOKEN, refreshToken);
159
160     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
161                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
162                          m_connType, cloudConnectHandler, m_defaultQos);
163 }
164
165 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
166                                            GetCallback cloudConnectHandler)
167 {
168     if (userUuid.empty())
169     {
170         return result_guard(OC_STACK_INVALID_PARAM);
171     }
172
173     return result_guard(searchUser(userUuid, QueryParamsMap(), cloudConnectHandler));
174 }
175
176 OCStackResult OCAccountManager::searchUser(const QueryParamsMap& queryParams,
177                                            GetCallback cloudConnectHandler)
178 {
179     if (queryParams.empty())
180     {
181         return result_guard(OC_STACK_INVALID_PARAM);
182     }
183
184     return result_guard(searchUser("", queryParams, cloudConnectHandler));
185 }
186
187 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
188                                            const QueryParamsMap& queryParams,
189                                            GetCallback cloudConnectHandler)
190 {
191     std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
192
193     QueryParamsMap fullQuery = {};
194
195     if (!userUuid.empty())
196     {
197         fullQuery.insert(std::make_pair(OC_RSRVD_USER_UUID, userUuid));
198     }
199
200     if (!queryParams.empty())
201     {
202         std::string searchQuery;
203         for (auto iter : queryParams)
204         {
205             searchQuery.append(iter.first + ":" + iter.second + ",");
206         }
207         searchQuery.resize(searchQuery.size() - 1);
208         fullQuery.insert(std::make_pair(OC_RSRVD_SEARCH, searchQuery));
209     }
210
211     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
212                          OCDevAddr(), uri, fullQuery, HeaderOptions(),
213                          m_connType, cloudConnectHandler, m_defaultQos);
214 }
215
216 OCStackResult OCAccountManager::deleteDevice(const std::string& deviceId,
217                                              DeleteCallback cloudConnectHandler)
218 {
219     if (deviceId.empty())
220     {
221         return result_guard(OC_STACK_INVALID_PARAM);
222     }
223
224     std::string uri = m_host + OC_RSRVD_ACCOUNT_URI
225                       + "?" + OC_RSRVD_DEVICE_ID + "=" + deviceId;
226
227     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
228                          OCDevAddr(), uri, HeaderOptions(),
229                          m_connType, cloudConnectHandler, m_defaultQos);
230 }
231
232 OCStackResult OCAccountManager::createGroup(AclGroupType groupType,
233                                             PostCallback cloudConnectHandler)
234 {
235     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI;
236
237     std::string gtype;
238     switch (groupType)
239     {
240         case AclGroupType::PUBLIC:
241             gtype = OC_RSRVD_PUBLIC;
242             break;
243         case AclGroupType::PRIVATE:
244             gtype = OC_RSRVD_PRIVATE;
245             break;
246         default:
247             return result_guard(OC_STACK_INVALID_PARAM);
248     }
249     OCRepresentation rep;
250     rep.setValue(OC_RSRVD_GROUP_TYPE, gtype);
251
252     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
253                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
254                          m_connType, cloudConnectHandler, m_defaultQos);
255 }
256
257 OCStackResult OCAccountManager::getGroupList(GetCallback cloudConnectHandler)
258 {
259     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI;
260
261     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
262                          OCDevAddr(), uri, QueryParamsMap(), HeaderOptions(),
263                          m_connType, cloudConnectHandler, m_defaultQos);
264 }
265
266 OCStackResult OCAccountManager::deleteGroup(const std::string& groupId,
267                                             DeleteCallback cloudConnectHandler)
268 {
269     if (groupId.empty())
270     {
271         return result_guard(OC_STACK_INVALID_PARAM);
272     }
273
274     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI
275                       + "?" + OC_RSRVD_GROUP_ID + "=" + groupId;
276
277     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
278                          OCDevAddr(), uri, HeaderOptions(),
279                          m_connType, cloudConnectHandler, m_defaultQos);
280 }
281
282 OCStackResult OCAccountManager::joinGroup(const std::string& groupId,
283                                           PostCallback cloudConnectHandler)
284 {
285     if (groupId.empty())
286     {
287         return result_guard(OC_STACK_INVALID_PARAM);
288     }
289
290     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
291
292     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
293                          OCDevAddr(), uri, OCRepresentation(), QueryParamsMap(), HeaderOptions(),
294                          m_connType, cloudConnectHandler, m_defaultQos);
295 }
296
297 OCStackResult OCAccountManager::addDeviceToGroup(const std::string& groupId,
298                                                  const std::vector<std::string>& deviceId,
299                                                  PostCallback cloudConnectHandler)
300 {
301     if (groupId.empty() || deviceId.empty())
302     {
303         return result_guard(OC_STACK_INVALID_PARAM);
304     }
305
306     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
307
308     OCRepresentation rep;
309     rep.setValue<std::vector<std::string>>(std::string(OC_RSRVD_DEVICE_ID_LIST), deviceId);
310
311     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
312                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
313                          m_connType, cloudConnectHandler, m_defaultQos);
314 }
315
316 OCStackResult OCAccountManager::getGroupInfo(const std::string& groupId,
317                                              GetCallback cloudConnectHandler)
318 {
319     if (groupId.empty())
320     {
321         return result_guard(OC_STACK_INVALID_PARAM);
322     }
323
324     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
325
326     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
327                          OCDevAddr(), uri, QueryParamsMap(), HeaderOptions(),
328                          m_connType, cloudConnectHandler, m_defaultQos);
329 }
330
331 OCStackResult OCAccountManager::leaveGroup(const std::string& groupId,
332                                            DeleteCallback cloudConnectHandler)
333 {
334     if (groupId.empty())
335     {
336         return result_guard(OC_STACK_INVALID_PARAM);
337     }
338
339     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
340
341     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
342                          OCDevAddr(), uri, HeaderOptions(),
343                          m_connType, cloudConnectHandler, m_defaultQos);
344 }
345
346 OCStackResult OCAccountManager::deleteDeviceFromGroup(const std::string& groupId,
347                                                       const std::vector<std::string>& deviceId,
348                                                       DeleteCallback cloudConnectHandler)
349 {
350     if (groupId.empty() || deviceId.empty())
351     {
352         return result_guard(OC_STACK_INVALID_PARAM);
353     }
354
355     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
356
357
358     uri.append("?");
359     for (auto iter : deviceId)
360     {
361         uri.append((std::string)OC_RSRVD_DEVICE_ID_LIST + "=" + iter + ";");
362     }
363     uri.resize(uri.size() - 1);
364
365     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
366                          OCDevAddr(), uri, HeaderOptions(),
367                          m_connType, cloudConnectHandler, m_defaultQos);
368 }
369
370 OCStackResult OCAccountManager::observeGroup(const std::string& groupId,
371                                              ObserveCallback cloudConnectHandler)
372 {
373     if (groupId.empty())
374     {
375         return result_guard(OC_STACK_INVALID_PARAM);
376     }
377
378     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
379
380     OCDoHandle handle = nullptr;
381
382     OCStackResult result = checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
383                                          ObserveType::Observe, &handle, OCDevAddr(), uri,
384                                          QueryParamsMap(), HeaderOptions(), cloudConnectHandler,
385                                          m_defaultQos);
386
387     if (OC_STACK_OK == result)
388     {
389         m_groupObserveHandles.insert(std::pair<std::string, OCDoHandle>(groupId, handle));
390     }
391
392     return result;
393
394 }
395
396 OCStackResult OCAccountManager::cancelObserveGroup(const std::string& groupId)
397 {
398     if (groupId.empty())
399     {
400         return result_guard(OC_STACK_INVALID_PARAM);
401     }
402
403     auto found = m_groupObserveHandles.find(groupId);
404     if (m_groupObserveHandles.end() == found)
405     {
406         return result_guard(OC_STACK_INVALID_PARAM);
407     }
408
409     OCDoHandle handle = found->second;
410
411     std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
412
413     OCStackResult result = checked_guard(m_clientWrapper.lock(),
414                                          &IClientWrapper::CancelObserveResource, handle,
415                                          (const char*)"", uri, HeaderOptions(), m_defaultQos);
416
417     if (OC_STACK_OK == result)
418     {
419         m_groupObserveHandles.erase(groupId);
420         handle = nullptr;
421     }
422
423     return result;
424 }
425
426 OCStackResult OCAccountManager::observeInvitation(ObserveCallback cloudConnectHandler)
427 {
428     std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
429
430     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
431                          ObserveType::Observe, &m_invitationObserveHandle, OCDevAddr(), uri,
432                          QueryParamsMap(), HeaderOptions(), cloudConnectHandler, m_defaultQos);
433 }
434
435 OCStackResult OCAccountManager::cancelObserveInvitation()
436 {
437     if (nullptr == m_invitationObserveHandle)
438     {
439         return result_guard(OC_STACK_INVALID_PARAM);
440     }
441
442     std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
443
444     OCStackResult result = checked_guard(m_clientWrapper.lock(),
445                                          &IClientWrapper::CancelObserveResource,
446                                          m_invitationObserveHandle,
447                                          (const char*)"", uri, HeaderOptions(), m_defaultQos);
448
449     if (OC_STACK_OK == result)
450     {
451         m_invitationObserveHandle = nullptr;
452     }
453
454     return result;
455 }
456
457 OCStackResult OCAccountManager::sendInvitation(const std::string& groupId,
458                                                const std::string& userUuid,
459                                                PostCallback cloudConnectHandler)
460 {
461     if (groupId.empty() || userUuid.empty())
462     {
463         return result_guard(OC_STACK_INVALID_PARAM);
464     }
465
466     std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
467
468     OCRepresentation invitation;
469     invitation.setValue(OC_RSRVD_GROUP_ID, groupId);
470     invitation.setValue(OC_RSRVD_MEMBER_ID, userUuid);
471
472     std::vector<OCRepresentation> invite{invitation};
473
474     OCRepresentation rep;
475     rep.setValue(OC_RSRVD_INVITE, invite);
476
477     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
478                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
479                          m_connType, cloudConnectHandler, m_defaultQos);
480 }
481
482 OCStackResult OCAccountManager::cancelInvitation(const std::string& groupId,
483                                                  const std::string& userUuid,
484                                                  DeleteCallback cloudConnectHandler)
485 {
486     if (groupId.empty() || userUuid.empty())
487     {
488         return result_guard(OC_STACK_INVALID_PARAM);
489     }
490
491     std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI + "?" + OC_RSRVD_GROUP_ID + "=" + groupId
492                       + ";" + OC_RSRVD_MEMBER_ID + "=" + userUuid;
493
494     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
495                          OCDevAddr(), uri, HeaderOptions(),
496                          m_connType, cloudConnectHandler, m_defaultQos);
497 }
498
499 OCStackResult OCAccountManager::deleteInvitation(const std::string& groupId,
500                                                  DeleteCallback cloudConnectHandler)
501 {
502     if (groupId.empty())
503     {
504         return result_guard(OC_STACK_INVALID_PARAM);
505     }
506
507     std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI + "?" + OC_RSRVD_GROUP_ID + "=" + groupId;
508
509     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
510                          OCDevAddr(), uri, HeaderOptions(),
511                          m_connType, cloudConnectHandler, m_defaultQos);
512 }
513
514 } // namespace OC