1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #define OUSTR_TO_STDSTR(s) string( OUStringToOString( s, RTL_TEXTENCODING_UTF8 ).getStr() )
11 #define STD_TO_OUSTR( str ) OUString( str.c_str(), str.length( ), RTL_TEXTENCODING_UTF8 )
13 #include <com/sun/star/task/XInteractionHandler.hpp>
14 #include <com/sun/star/task/PasswordContainer.hpp>
15 #include <com/sun/star/task/XPasswordContainer2.hpp>
17 #include <comphelper/processfactory.hxx>
18 #include <ucbhelper/simpleauthenticationrequest.hxx>
19 #include <ucbhelper/authenticationfallback.hxx>
21 #include "auth_provider.hxx"
23 using namespace com::sun::star
;
28 bool AuthProvider::authenticationQuery( string
& username
, string
& password
)
32 uno::Reference
< task::XInteractionHandler
> xIH
33 = m_xEnv
->getInteractionHandler();
37 rtl::Reference
< ucbhelper::SimpleAuthenticationRequest
> xRequest
38 = new ucbhelper::SimpleAuthenticationRequest(
39 m_sUrl
, m_sBindingUrl
, OUString(),
40 STD_TO_OUSTR( username
),
41 STD_TO_OUSTR( password
),
43 xIH
->handle( xRequest
);
45 rtl::Reference
< ucbhelper::InteractionContinuation
> xSelection
46 = xRequest
->getSelection();
48 if ( xSelection
.is() )
50 // Handler handled the request.
51 uno::Reference
< task::XInteractionAbort
> xAbort(
52 xSelection
.get(), uno::UNO_QUERY
);
56 ucbhelper::InteractionSupplyAuthentication
> & xSupp
57 = xRequest
->getAuthenticationSupplier();
59 username
= OUSTR_TO_STDSTR( xSupp
->getUserName() );
60 password
= OUSTR_TO_STDSTR( xSupp
->getPassword() );
70 string
AuthProvider::getRefreshToken(string
& rUsername
)
73 const css::uno::Reference
<css::ucb::XCommandEnvironment
> xEnv
= getXEnv();
76 uno::Reference
<task::XInteractionHandler
> xIH
= xEnv
->getInteractionHandler();
78 if (rUsername
.empty())
80 rtl::Reference
<ucbhelper::SimpleAuthenticationRequest
> xRequest
81 = new ucbhelper::SimpleAuthenticationRequest(
82 m_sUrl
, m_sBindingUrl
,
83 ucbhelper::SimpleAuthenticationRequest::EntityType::ENTITY_NA
, OUString(),
84 ucbhelper::SimpleAuthenticationRequest::EntityType::ENTITY_MODIFY
,
85 STD_TO_OUSTR(rUsername
),
86 ucbhelper::SimpleAuthenticationRequest::EntityType::ENTITY_NA
, OUString());
87 xIH
->handle(xRequest
);
89 rtl::Reference
<ucbhelper::InteractionContinuation
> xSelection
90 = xRequest
->getSelection();
94 // Handler handled the request.
95 uno::Reference
<task::XInteractionAbort
> xAbort(xSelection
.get(),
99 const rtl::Reference
<ucbhelper::InteractionSupplyAuthentication
>& xSupp
100 = xRequest
->getAuthenticationSupplier();
102 rUsername
= OUSTR_TO_STDSTR(xSupp
->getUserName());
107 uno::Reference
<uno::XComponentContext
> xContext
108 = ::comphelper::getProcessComponentContext();
109 uno::Reference
<task::XPasswordContainer2
> xMasterPasswd
110 = task::PasswordContainer::create(xContext
);
111 if (xMasterPasswd
->hasMasterPassword())
113 xMasterPasswd
->authorizateWithMasterPassword(xIH
);
115 if (xMasterPasswd
->isPersistentStoringAllowed())
118 = xMasterPasswd
->findForName(m_sBindingUrl
, STD_TO_OUSTR(rUsername
), xIH
);
119 if (aRec
.UserList
.hasElements() && aRec
.UserList
[0].Passwords
.hasElements())
120 refreshToken
= OUSTR_TO_STDSTR(aRec
.UserList
[0].Passwords
[0]);
126 bool AuthProvider::storeRefreshToken(const string
& username
, const string
& password
,
127 const string
& refreshToken
)
129 if (refreshToken
.empty())
131 if (password
== refreshToken
)
133 const css::uno::Reference
<css::ucb::XCommandEnvironment
> xEnv
= getXEnv();
136 uno::Reference
<task::XInteractionHandler
> xIH
= xEnv
->getInteractionHandler();
137 uno::Reference
<uno::XComponentContext
> xContext
138 = ::comphelper::getProcessComponentContext();
139 uno::Reference
<task::XPasswordContainer2
> xMasterPasswd
140 = task::PasswordContainer::create(xContext
);
141 uno::Sequence
<OUString
> aPasswd
{ STD_TO_OUSTR(refreshToken
) };
142 if (xMasterPasswd
->isPersistentStoringAllowed())
144 if (xMasterPasswd
->hasMasterPassword())
146 xMasterPasswd
->authorizateWithMasterPassword(xIH
);
148 xMasterPasswd
->addPersistent(m_sBindingUrl
, STD_TO_OUSTR(username
), aPasswd
, xIH
);
155 css::uno::WeakReference
< css::ucb::XCommandEnvironment
> AuthProvider::sm_xEnv
;
157 void AuthProvider::setXEnv(const css::uno::Reference
< css::ucb::XCommandEnvironment
>& xEnv
)
162 css::uno::Reference
< css::ucb::XCommandEnvironment
> AuthProvider::getXEnv()
167 char* AuthProvider::copyWebAuthCodeFallback( const char* url
,
168 const char* /*username*/,
169 const char* /*password*/ )
171 OUString
url_oustr( url
, strlen( url
), RTL_TEXTENCODING_UTF8
);
172 const css::uno::Reference
<
173 css::ucb::XCommandEnvironment
> xEnv
= getXEnv( );
177 uno::Reference
< task::XInteractionHandler
> xIH
178 = xEnv
->getInteractionHandler();
182 rtl::Reference
< ucbhelper::AuthenticationFallbackRequest
> xRequest
183 = new ucbhelper::AuthenticationFallbackRequest (
184 "Open the following link in your browser and "
185 "paste the code from the URL you have been redirected to in the "
186 "box below. For example:\n"
187 "http://localhost/LibreOffice?code=YOUR_CODE",
190 xIH
->handle( xRequest
);
192 rtl::Reference
< ucbhelper::InteractionContinuation
> xSelection
193 = xRequest
->getSelection();
195 if ( xSelection
.is() )
197 // Handler handled the request.
198 const rtl::Reference
< ucbhelper::InteractionAuthFallback
>&
199 xAuthFallback
= xRequest
->getAuthFallbackInter( );
200 if ( xAuthFallback
.is() )
202 OUString code
= xAuthFallback
->getCode( );
203 return strdup( OUSTR_TO_STDSTR( code
).c_str( ) );
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */