1 /* ****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "OCAccountManager.h"
22 #include "OCUtilities.h"
28 using OC::result_guard;
29 using OC::checked_guard;
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)
36 if (m_host.empty() || m_clientWrapper.expired())
38 throw OCException(OC::Exception::INVALID_PARAM);
41 const char* di = OCGetServerInstanceIDString();
44 oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
45 throw OCException(OC::Exception::INVALID_PARAM);
48 m_deviceID.append(di);
49 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, m_defaultQos);
52 OCAccountManager::~OCAccountManager()
56 std::string OCAccountManager::host() const
61 OCConnectivityType OCAccountManager::connectivityType() const
66 OCStackResult OCAccountManager::signUp(const std::string& authProvider,
67 const std::string& authCode,
68 PostCallback cloudConnectHandler)
70 return result_guard(signUp(authProvider, authCode, QueryParamsMap(), cloudConnectHandler));
73 OCStackResult OCAccountManager::signUp(const std::string& authProvider,
74 const std::string& authCode,
75 const QueryParamsMap& options,
76 PostCallback cloudConnectHandler)
78 std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
81 rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
82 rep.setValue(OC_RSRVD_AUTHPROVIDER, authProvider);
83 rep.setValue(OC_RSRVD_AUTHCODE, authCode);
87 OCRepresentation optionsRep;
88 for (auto iter : options)
90 optionsRep[iter.first] = iter.second;
92 rep.setValue(OC_RSRVD_OPTIONS, optionsRep);
95 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
96 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
97 m_connType, cloudConnectHandler, m_defaultQos);
100 OCStackResult OCAccountManager::signIn(const std::string& userUuid,
101 const std::string& accessToken,
102 PostCallback cloudConnectHandler)
104 return result_guard(signInOut(userUuid, accessToken, true, cloudConnectHandler));
107 OCStackResult OCAccountManager::signOut(PostCallback cloudConnectHandler)
109 return result_guard(signInOut("", "", false, cloudConnectHandler));
112 OCStackResult OCAccountManager::signInOut(const std::string& userUuid,
113 const std::string& accessToken,
115 PostCallback cloudConnectHandler)
117 std::string uri = m_host + OC_RSRVD_ACCOUNT_SESSION_URI;
119 OCRepresentation rep;
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);
126 rep.setValue(OC_RSRVD_LOGIN, isSignIn);
128 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
129 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
130 m_connType, cloudConnectHandler, m_defaultQos);
133 OCStackResult OCAccountManager::refreshAccessToken(const std::string& userUuid,
134 const std::string& refreshToken,
135 PostCallback cloudConnectHandler)
137 std::string uri = m_host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
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);
145 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
146 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
147 m_connType, cloudConnectHandler, m_defaultQos);
150 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
151 GetCallback cloudConnectHandler)
153 return result_guard(searchUser(userUuid, QueryParamsMap(), cloudConnectHandler));
156 OCStackResult OCAccountManager::searchUser(const QueryParamsMap& queryParams,
157 GetCallback cloudConnectHandler)
159 return result_guard(searchUser("", queryParams, cloudConnectHandler));
162 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
163 const QueryParamsMap& queryParams,
164 GetCallback cloudConnectHandler)
166 std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
168 QueryParamsMap fullQuery = {};
170 if (!userUuid.empty())
172 fullQuery.insert(std::make_pair(OC_RSRVD_USER_UUID, userUuid));
175 if (!queryParams.empty())
177 std::string searchQuery;
178 for (auto iter : queryParams)
180 searchQuery.append(iter.first + ":" + iter.second + ",");
182 searchQuery.resize(searchQuery.size() - 1);
183 fullQuery.insert(std::make_pair(OC_RSRVD_SEARCH, searchQuery));
186 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
187 OCDevAddr(), uri, fullQuery, HeaderOptions(),
188 m_connType, cloudConnectHandler, m_defaultQos);
191 OCStackResult OCAccountManager::deleteDevice(const std::string& deviceId,
192 DeleteCallback cloudConnectHandler)
194 std::string uri = m_host + OC_RSRVD_ACCOUNT_URI
195 + "?" + OC_RSRVD_DEVICE_ID + "=" + deviceId;
197 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
198 OCDevAddr(), uri, HeaderOptions(),
199 m_connType, cloudConnectHandler, m_defaultQos);