Merge branch 'master' into 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 {
36     if (m_host.empty() || m_clientWrapper.expired())
37     {
38         throw OCException(OC::Exception::INVALID_PARAM);
39     }
40
41     const char* di = OCGetServerInstanceIDString();
42     if (!di)
43     {
44         oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
45         throw OCException(OC::Exception::INVALID_PARAM);
46     }
47
48     m_deviceID.append(di);
49     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, m_defaultQos);
50 }
51
52 OCAccountManager::~OCAccountManager()
53 {
54 }
55
56 std::string OCAccountManager::host() const
57 {
58     return m_host;
59 }
60
61 OCConnectivityType OCAccountManager::connectivityType() const
62 {
63     return m_connType;
64 }
65
66 OCStackResult OCAccountManager::signUp(const std::string& authProvider,
67                                        const std::string& authCode,
68                                        PostCallback cloudConnectHandler)
69 {
70     return result_guard(signUp(authProvider, authCode, QueryParamsMap(), cloudConnectHandler));
71 }
72
73 OCStackResult OCAccountManager::signUp(const std::string& authProvider,
74                                        const std::string& authCode,
75                                        const QueryParamsMap& options,
76                                        PostCallback cloudConnectHandler)
77 {
78     std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
79
80     OCRepresentation rep;
81     rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
82     rep.setValue(OC_RSRVD_AUTHPROVIDER, authProvider);
83     rep.setValue(OC_RSRVD_AUTHCODE, authCode);
84
85     if (!options.empty())
86     {
87         OCRepresentation optionsRep;
88         for (auto iter : options)
89         {
90             optionsRep[iter.first] = iter.second;
91         }
92         rep.setValue(OC_RSRVD_OPTIONS, optionsRep);
93     }
94
95     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
96                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
97                          m_connType, cloudConnectHandler, m_defaultQos);
98 }
99
100 OCStackResult OCAccountManager::signIn(const std::string& userUuid,
101                                        const std::string& accessToken,
102                                        PostCallback cloudConnectHandler)
103 {
104     return result_guard(signInOut(userUuid, accessToken, true, cloudConnectHandler));
105 }
106
107 OCStackResult OCAccountManager::signOut(PostCallback cloudConnectHandler)
108 {
109     return result_guard(signInOut("", "", false, cloudConnectHandler));
110 }
111
112 OCStackResult OCAccountManager::signInOut(const std::string& userUuid,
113                                           const std::string& accessToken,
114                                           bool isSignIn,
115                                           PostCallback cloudConnectHandler)
116 {
117     std::string uri = m_host + OC_RSRVD_ACCOUNT_SESSION_URI;
118
119     OCRepresentation rep;
120     if (isSignIn)
121     {
122         rep.setValue(OC_RSRVD_USER_UUID, userUuid);
123         rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
124         rep.setValue(OC_RSRVD_ACCESS_TOKEN, accessToken);
125     }
126     rep.setValue(OC_RSRVD_LOGIN, isSignIn);
127
128     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
129                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
130                          m_connType, cloudConnectHandler, m_defaultQos);
131 }
132
133 OCStackResult OCAccountManager::refreshAccessToken(const std::string& userUuid,
134                                                    const std::string& refreshToken,
135                                                    PostCallback cloudConnectHandler)
136 {
137     std::string uri = m_host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
138
139     OCRepresentation rep;
140     rep.setValue(OC_RSRVD_USER_UUID, userUuid);
141     rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
142     rep.setValue(OC_RSRVD_GRANT_TYPE, OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN);
143     rep.setValue(OC_RSRVD_REFRESH_TOKEN, refreshToken);
144
145     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
146                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
147                          m_connType, cloudConnectHandler, m_defaultQos);
148 }
149
150 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
151                                            GetCallback cloudConnectHandler)
152 {
153     return result_guard(searchUser(userUuid, QueryParamsMap(), cloudConnectHandler));
154 }
155
156 OCStackResult OCAccountManager::searchUser(const QueryParamsMap& queryParams,
157                                            GetCallback cloudConnectHandler)
158 {
159     return result_guard(searchUser("", queryParams, cloudConnectHandler));
160 }
161
162 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
163                                            const QueryParamsMap& queryParams,
164                                            GetCallback cloudConnectHandler)
165 {
166     std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
167
168     QueryParamsMap fullQuery = {};
169
170     if (!userUuid.empty())
171     {
172         fullQuery.insert(std::make_pair(OC_RSRVD_USER_UUID, userUuid));
173     }
174
175     if (!queryParams.empty())
176     {
177         std::string searchQuery;
178         for (auto iter : queryParams)
179         {
180             searchQuery.append(iter.first + ":" + iter.second + ",");
181         }
182         searchQuery.resize(searchQuery.size() - 1);
183         fullQuery.insert(std::make_pair(OC_RSRVD_SEARCH, searchQuery));
184     }
185
186     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
187                          OCDevAddr(), uri, fullQuery, HeaderOptions(),
188                          m_connType, cloudConnectHandler, m_defaultQos);
189 }
190
191 OCStackResult OCAccountManager::deleteDevice(const std::string& deviceId,
192                                              DeleteCallback cloudConnectHandler)
193 {
194     std::string uri = m_host + OC_RSRVD_ACCOUNT_URI
195                       + "?" + OC_RSRVD_DEVICE_ID + "=" + deviceId;
196
197     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
198                          OCDevAddr(), uri, HeaderOptions(),
199                          m_connType, cloudConnectHandler, m_defaultQos);
200 }
201
202 } // namespace OC