LibreOffice Module ucb (master) 1
auth_provider.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#define OUSTR_TO_STDSTR(s) std::string( OUStringToOString( s, RTL_TEXTENCODING_UTF8 ) )
11#define STD_TO_OUSTR( str ) OUString( str.c_str(), str.length( ), RTL_TEXTENCODING_UTF8 )
12
13#include <com/sun/star/task/XInteractionHandler.hpp>
14#include <com/sun/star/task/PasswordContainer.hpp>
15#include <com/sun/star/task/XPasswordContainer2.hpp>
16
20
21#include "auth_provider.hxx"
22
23using namespace com::sun::star;
24
25namespace cmis
26{
27 bool AuthProvider::authenticationQuery( std::string& username, std::string& password )
28 {
29 if ( m_xEnv.is() )
30 {
31 uno::Reference< task::XInteractionHandler > xIH
32 = m_xEnv->getInteractionHandler();
33
34 if ( xIH.is() )
35 {
38 m_sUrl, m_sBindingUrl, OUString(),
39 STD_TO_OUSTR( username ),
40 STD_TO_OUSTR( password ),
41 false, false );
42 xIH->handle( xRequest );
43
45 = xRequest->getSelection();
46
47 if ( xSelection.is() )
48 {
49 // Handler handled the request.
50 uno::Reference< task::XInteractionAbort > xAbort(
51 xSelection.get(), uno::UNO_QUERY );
52 if ( !xAbort.is() )
53 {
54 const rtl::Reference<
56 = xRequest->getAuthenticationSupplier();
57
58 username = OUSTR_TO_STDSTR( xSupp->getUserName() );
59 password = OUSTR_TO_STDSTR( xSupp->getPassword() );
60
61 return true;
62 }
63 }
64 }
65 }
66 return false;
67 }
68
69 std::string AuthProvider::getRefreshToken(std::string& rUsername)
70 {
71 std::string refreshToken;
72 const css::uno::Reference<css::ucb::XCommandEnvironment> xEnv = getXEnv();
73 if (xEnv.is())
74 {
75 uno::Reference<task::XInteractionHandler> xIH = xEnv->getInteractionHandler();
76
77 if (rUsername.empty())
78 {
82 ucbhelper::SimpleAuthenticationRequest::EntityType::ENTITY_NA, OUString(),
83 ucbhelper::SimpleAuthenticationRequest::EntityType::ENTITY_MODIFY,
84 STD_TO_OUSTR(rUsername),
85 ucbhelper::SimpleAuthenticationRequest::EntityType::ENTITY_NA, OUString());
86 xIH->handle(xRequest);
87
89 = xRequest->getSelection();
90
91 if (xSelection.is())
92 {
93 // Handler handled the request.
94 uno::Reference<task::XInteractionAbort> xAbort(xSelection.get(),
95 uno::UNO_QUERY);
96 if (!xAbort.is())
97 {
99 = xRequest->getAuthenticationSupplier();
100
101 rUsername = OUSTR_TO_STDSTR(xSupp->getUserName());
102 }
103 }
104 }
105
106 uno::Reference<uno::XComponentContext> xContext
107 = ::comphelper::getProcessComponentContext();
108 uno::Reference<task::XPasswordContainer2> xMasterPasswd
109 = task::PasswordContainer::create(xContext);
110 if (xMasterPasswd->hasMasterPassword())
111 {
112 xMasterPasswd->authorizateWithMasterPassword(xIH);
113 }
114 if (xMasterPasswd->isPersistentStoringAllowed())
115 {
116 task::UrlRecord aRec
117 = xMasterPasswd->findForName(m_sBindingUrl, STD_TO_OUSTR(rUsername), xIH);
118 if (aRec.UserList.hasElements() && aRec.UserList[0].Passwords.hasElements())
119 refreshToken = OUSTR_TO_STDSTR(aRec.UserList[0].Passwords[0]);
120 }
121 }
122 return refreshToken;
123 }
124
125 bool AuthProvider::storeRefreshToken(const std::string& username, const std::string& password,
126 const std::string& refreshToken)
127 {
128 if (refreshToken.empty())
129 return false;
130 if (password == refreshToken)
131 return true;
132 const css::uno::Reference<css::ucb::XCommandEnvironment> xEnv = getXEnv();
133 if (xEnv.is())
134 {
135 uno::Reference<task::XInteractionHandler> xIH = xEnv->getInteractionHandler();
136 uno::Reference<uno::XComponentContext> xContext
137 = ::comphelper::getProcessComponentContext();
138 uno::Reference<task::XPasswordContainer2> xMasterPasswd
139 = task::PasswordContainer::create(xContext);
140 uno::Sequence<OUString> aPasswd{ STD_TO_OUSTR(refreshToken) };
141 if (xMasterPasswd->isPersistentStoringAllowed())
142 {
143 if (xMasterPasswd->hasMasterPassword())
144 {
145 xMasterPasswd->authorizateWithMasterPassword(xIH);
146 }
147 xMasterPasswd->addPersistent(m_sBindingUrl, STD_TO_OUSTR(username), aPasswd, xIH);
148 return true;
149 }
150 }
151 return false;
152 }
153
154 css::uno::WeakReference< css::ucb::XCommandEnvironment> AuthProvider::sm_xEnv;
155
156 void AuthProvider::setXEnv(const css::uno::Reference< css::ucb::XCommandEnvironment>& xEnv )
157 {
158 sm_xEnv = xEnv;
159 }
160
161 css::uno::Reference< css::ucb::XCommandEnvironment> AuthProvider::getXEnv()
162 {
163 return sm_xEnv;
164 }
165
167 const char* /*username*/,
168 const char* /*password*/ )
169 {
170 OUString url_oustr( url, strlen( url ), RTL_TEXTENCODING_UTF8 );
171 const css::uno::Reference<
172 css::ucb::XCommandEnvironment> xEnv = getXEnv( );
173
174 if ( xEnv.is() )
175 {
176 uno::Reference< task::XInteractionHandler > xIH
177 = xEnv->getInteractionHandler();
178
179 if ( xIH.is() )
180 {
183 "Open the following link in your browser and "
184 "paste the code from the URL you have been redirected to in the "
185 "box below. For example:\n"
186 "http://localhost/LibreOffice?code=YOUR_CODE",
187 url_oustr );
188
189 xIH->handle( xRequest );
190
192 = xRequest->getSelection();
193
194 if ( xSelection.is() )
195 {
196 // Handler handled the request.
198 xAuthFallback = xRequest->getAuthFallbackInter( );
199 if ( xAuthFallback.is() )
200 {
201 OUString code = xAuthFallback->getCode( );
202 return strdup( OUSTR_TO_STDSTR( code ).c_str( ) );
203 }
204 }
205 }
206 }
207
208 return strdup( "" );
209 }
210}
211
212/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define OUSTR_TO_STDSTR(s)
#define STD_TO_OUSTR(str)
bool authenticationQuery(std::string &username, std::string &password) override
bool storeRefreshToken(const std::string &username, const std::string &password, const std::string &refreshToken)
static css::uno::WeakReference< css::ucb::XCommandEnvironment > sm_xEnv
const css::uno::Reference< css::ucb::XCommandEnvironment > & m_xEnv
static void setXEnv(const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv)
static char * copyWebAuthCodeFallback(const char *url, const char *, const char *)
std::string getRefreshToken(std::string &username)
static css::uno::Reference< css::ucb::XCommandEnvironment > getXEnv()
sal_Unicode code