bump product version to 7.2.5.1
[LibreOffice.git] / ucb / source / ucp / cmis / auth_provider.cxx
blob05a8846a11a7e72c5cc37544b76bb438dd27c1ae
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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;
24 using namespace std;
26 namespace cmis
28 bool AuthProvider::authenticationQuery( string& username, string& password )
30 if ( m_xEnv.is() )
32 uno::Reference< task::XInteractionHandler > xIH
33 = m_xEnv->getInteractionHandler();
35 if ( xIH.is() )
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 ),
42 false, false );
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 );
53 if ( !xAbort.is() )
55 const rtl::Reference<
56 ucbhelper::InteractionSupplyAuthentication > & xSupp
57 = xRequest->getAuthenticationSupplier();
59 username = OUSTR_TO_STDSTR( xSupp->getUserName() );
60 password = OUSTR_TO_STDSTR( xSupp->getPassword() );
62 return true;
67 return false;
70 string AuthProvider::getRefreshToken(string& rUsername)
72 string refreshToken;
73 const css::uno::Reference<css::ucb::XCommandEnvironment> xEnv = getXEnv();
74 if (xEnv.is())
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();
92 if (xSelection.is())
94 // Handler handled the request.
95 uno::Reference<task::XInteractionAbort> xAbort(xSelection.get(),
96 uno::UNO_QUERY);
97 if (!xAbort.is())
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())
117 task::UrlRecord aRec
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]);
123 return refreshToken;
126 bool AuthProvider::storeRefreshToken(const string& username, const string& password,
127 const string& refreshToken)
129 if (refreshToken.empty())
130 return false;
131 if (password == refreshToken)
132 return true;
133 const css::uno::Reference<css::ucb::XCommandEnvironment> xEnv = getXEnv();
134 if (xEnv.is())
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);
149 return true;
152 return false;
155 css::uno::WeakReference< css::ucb::XCommandEnvironment> AuthProvider::sm_xEnv;
157 void AuthProvider::setXEnv(const css::uno::Reference< css::ucb::XCommandEnvironment>& xEnv )
159 sm_xEnv = xEnv;
162 css::uno::Reference< css::ucb::XCommandEnvironment> AuthProvider::getXEnv()
164 return sm_xEnv;
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( );
175 if ( xEnv.is() )
177 uno::Reference< task::XInteractionHandler > xIH
178 = xEnv->getInteractionHandler();
180 if ( xIH.is() )
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",
188 url_oustr );
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( ) );
209 return strdup( "" );
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */