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) std::string( OUStringToOString( s, RTL_TEXTENCODING_UTF8 ) )
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
;
27 bool AuthProvider::authenticationQuery( std::string
& username
, std::string
& password
)
31 uno::Reference
< task::XInteractionHandler
> xIH
32 = m_xEnv
->getInteractionHandler();
36 rtl::Reference
< ucbhelper::SimpleAuthenticationRequest
> xRequest
37 = new ucbhelper::SimpleAuthenticationRequest(
38 m_sUrl
, m_sBindingUrl
, OUString(),
39 STD_TO_OUSTR( username
),
40 STD_TO_OUSTR( password
),
42 xIH
->handle( xRequest
);
44 rtl::Reference
< ucbhelper::InteractionContinuation
> xSelection
45 = xRequest
->getSelection();
47 if ( xSelection
.is() )
49 // Handler handled the request.
50 uno::Reference
< task::XInteractionAbort
> xAbort(
51 xSelection
.get(), uno::UNO_QUERY
);
55 ucbhelper::InteractionSupplyAuthentication
> & xSupp
56 = xRequest
->getAuthenticationSupplier();
58 username
= OUSTR_TO_STDSTR( xSupp
->getUserName() );
59 password
= OUSTR_TO_STDSTR( xSupp
->getPassword() );
69 std::string
AuthProvider::getRefreshToken(std::string
& rUsername
)
71 std::string refreshToken
;
72 const css::uno::Reference
<css::ucb::XCommandEnvironment
> xEnv
= getXEnv();
75 uno::Reference
<task::XInteractionHandler
> xIH
= xEnv
->getInteractionHandler();
77 if (rUsername
.empty())
79 rtl::Reference
<ucbhelper::SimpleAuthenticationRequest
> xRequest
80 = new ucbhelper::SimpleAuthenticationRequest(
81 m_sUrl
, m_sBindingUrl
,
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
);
88 rtl::Reference
<ucbhelper::InteractionContinuation
> xSelection
89 = xRequest
->getSelection();
93 // Handler handled the request.
94 uno::Reference
<task::XInteractionAbort
> xAbort(xSelection
.get(),
98 const rtl::Reference
<ucbhelper::InteractionSupplyAuthentication
>& xSupp
99 = xRequest
->getAuthenticationSupplier();
101 rUsername
= OUSTR_TO_STDSTR(xSupp
->getUserName());
106 uno::Reference
<uno::XComponentContext
> xContext
107 = ::comphelper::getProcessComponentContext();
108 uno::Reference
<task::XPasswordContainer2
> xMasterPasswd
109 = task::PasswordContainer::create(xContext
);
110 if (xMasterPasswd
->hasMasterPassword())
112 xMasterPasswd
->authorizateWithMasterPassword(xIH
);
114 if (xMasterPasswd
->isPersistentStoringAllowed())
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]);
125 bool AuthProvider::storeRefreshToken(const std::string
& username
, const std::string
& password
,
126 const std::string
& refreshToken
)
128 if (refreshToken
.empty())
130 if (password
== refreshToken
)
132 const css::uno::Reference
<css::ucb::XCommandEnvironment
> xEnv
= getXEnv();
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())
143 if (xMasterPasswd
->hasMasterPassword())
145 xMasterPasswd
->authorizateWithMasterPassword(xIH
);
147 xMasterPasswd
->addPersistent(m_sBindingUrl
, STD_TO_OUSTR(username
), aPasswd
, xIH
);
154 css::uno::WeakReference
< css::ucb::XCommandEnvironment
> AuthProvider::sm_xEnv
;
156 void AuthProvider::setXEnv(const css::uno::Reference
< css::ucb::XCommandEnvironment
>& xEnv
)
161 css::uno::Reference
< css::ucb::XCommandEnvironment
> AuthProvider::getXEnv()
166 char* AuthProvider::copyWebAuthCodeFallback( const char* url
,
167 const char* /*username*/,
168 const char* /*password*/ )
170 OUString
url_oustr( url
, strlen( url
), RTL_TEXTENCODING_UTF8
);
171 const css::uno::Reference
<
172 css::ucb::XCommandEnvironment
> xEnv
= getXEnv( );
176 uno::Reference
< task::XInteractionHandler
> xIH
177 = xEnv
->getInteractionHandler();
181 rtl::Reference
< ucbhelper::AuthenticationFallbackRequest
> xRequest
182 = new ucbhelper::AuthenticationFallbackRequest (
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",
189 xIH
->handle( xRequest
);
191 rtl::Reference
< ucbhelper::InteractionContinuation
> xSelection
192 = xRequest
->getSelection();
194 if ( xSelection
.is() )
196 // Handler handled the request.
197 const rtl::Reference
< ucbhelper::InteractionAuthFallback
>&
198 xAuthFallback
= xRequest
->getAuthFallbackInter( );
199 if ( xAuthFallback
.is() )
201 OUString code
= xAuthFallback
->getCode( );
202 return strdup( OUSTR_TO_STDSTR( code
).c_str( ) );
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */