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& userId,
101 const std::string& accessToken,
102 PostCallback cloudConnectHandler)
104 return result_guard(signInOut(userId, accessToken, true, cloudConnectHandler));
107 OCStackResult OCAccountManager::signOut(const std::string& userId,
108 const std::string& accessToken,
109 PostCallback cloudConnectHandler)
111 return result_guard(signInOut(userId, accessToken, false, cloudConnectHandler));
114 OCStackResult OCAccountManager::signInOut(const std::string& userId,
115 const std::string& accessToken,
117 PostCallback cloudConnectHandler)
119 std::string uri = m_host + OC_RSRVD_ACCOUNT_SESSION_URI;
121 OCRepresentation rep;
122 rep.setValue(OC_RSRVD_USER_ID, userId);
123 rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
124 rep.setValue(OC_RSRVD_ACCESS_TOKEN, accessToken);
125 rep.setValue(OC_RSRVD_LOGIN, isSignIn);
127 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
128 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
129 m_connType, cloudConnectHandler, m_defaultQos);
132 OCStackResult OCAccountManager::refreshAccessToken(const std::string& userId,
133 const std::string& refreshToken,
134 PostCallback cloudConnectHandler)
136 std::string uri = m_host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
138 OCRepresentation rep;
139 rep.setValue(OC_RSRVD_USER_ID, userId);
140 rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
141 rep.setValue(OC_RSRVD_GRANT_TYPE, OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN);
142 rep.setValue(OC_RSRVD_REFRESH_TOKEN, refreshToken);
144 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
145 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
146 m_connType, cloudConnectHandler, m_defaultQos);