Merge branch 'master' into extended-easysetup
[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& userId,
101                                        const std::string& accessToken,
102                                        PostCallback cloudConnectHandler)
103 {
104     return result_guard(signInOut(userId, accessToken, true, cloudConnectHandler));
105 }
106
107 OCStackResult OCAccountManager::signOut(const std::string& userId,
108                                         const std::string& accessToken,
109                                         PostCallback cloudConnectHandler)
110 {
111     return result_guard(signInOut(userId, accessToken, false, cloudConnectHandler));
112 }
113
114 OCStackResult OCAccountManager::signInOut(const std::string& userId,
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     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);
126
127     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
128                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
129                          m_connType, cloudConnectHandler, m_defaultQos);
130 }
131
132 OCStackResult OCAccountManager::refreshAccessToken(const std::string& userId,
133                                                    const std::string& refreshToken,
134                                                    PostCallback cloudConnectHandler)
135 {
136     std::string uri = m_host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
137
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);
143
144     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
145                          OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
146                          m_connType, cloudConnectHandler, m_defaultQos);
147 }
148 } // namespace OC