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) string( OUStringToOString( s, RTL_TEXTENCODING_UTF8 ).getStr() )
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 
17 
18 #include "auth_provider.hxx"
19 
20 using namespace com::sun::star;
21 using namespace std;
22 
23 namespace cmis
24 {
25  bool AuthProvider::authenticationQuery( string& username, string& password )
26  {
27  if ( m_xEnv.is() )
28  {
29  uno::Reference< task::XInteractionHandler > xIH
30  = m_xEnv->getInteractionHandler();
31 
32  if ( xIH.is() )
33  {
36  m_sUrl, m_sBindingUrl, OUString(),
37  STD_TO_OUSTR( username ),
38  STD_TO_OUSTR( password ),
39  false, false );
40  xIH->handle( xRequest.get() );
41 
43  = xRequest->getSelection();
44 
45  if ( xSelection.is() )
46  {
47  // Handler handled the request.
48  uno::Reference< task::XInteractionAbort > xAbort(
49  xSelection.get(), uno::UNO_QUERY );
50  if ( !xAbort.is() )
51  {
52  const rtl::Reference<
53  ucbhelper::InteractionSupplyAuthentication > & xSupp
54  = xRequest->getAuthenticationSupplier();
55 
56  username = OUSTR_TO_STDSTR( xSupp->getUserName() );
57  password = OUSTR_TO_STDSTR( xSupp->getPassword() );
58 
59  return true;
60  }
61  }
62  }
63  }
64  return false;
65  }
66 
67  css::uno::WeakReference< css::ucb::XCommandEnvironment> AuthProvider::sm_xEnv;
68 
69  void AuthProvider::setXEnv(const css::uno::Reference< css::ucb::XCommandEnvironment>& xEnv )
70  {
71  sm_xEnv = xEnv;
72  }
73 
74  css::uno::Reference< css::ucb::XCommandEnvironment> AuthProvider::getXEnv()
75  {
76  return sm_xEnv;
77  }
78 
79  char* AuthProvider::onedriveAuthCodeFallback( const char* url,
80  const char* /*username*/,
81  const char* /*password*/ )
82  {
83  OUString url_oustr( url, strlen( url ), RTL_TEXTENCODING_UTF8 );
84  const css::uno::Reference<
85  css::ucb::XCommandEnvironment> xEnv = getXEnv( );
86 
87  if ( xEnv.is() )
88  {
89  uno::Reference< task::XInteractionHandler > xIH
90  = xEnv->getInteractionHandler();
91 
92  if ( xIH.is() )
93  {
96  "Open the following link in your browser and "
97  "paste the code from the URL you have been redirected to in the "
98  "box below. For example:\n"
99  "https://login.live.com/oauth20_desktop.srf?code=YOUR_CODE&lc=1033",
100  url_oustr );
101 
102  xIH->handle( xRequest.get() );
103 
105  = xRequest->getSelection();
106 
107  if ( xSelection.is() )
108  {
109  // Handler handled the request.
111  xAuthFallback = xRequest->getAuthFallbackInter( );
112  if ( xAuthFallback.is() )
113  {
114  OUString code = xAuthFallback->getCode( );
115  return strdup( OUSTR_TO_STDSTR( code ).c_str( ) );
116  }
117  }
118  }
119  }
120 
121  return strdup( "" );
122  }
123 
124  char* AuthProvider::gdriveAuthCodeFallback( const char* /*url*/,
125  const char* /*username*/,
126  const char* /*password*/ )
127  {
128  const css::uno::Reference<
129  css::ucb::XCommandEnvironment> xEnv = getXEnv( );
130 
131  if ( xEnv.is() )
132  {
133  uno::Reference< task::XInteractionHandler > xIH
134  = xEnv->getInteractionHandler();
135 
136  if ( xIH.is() )
137  {
140  "PIN:", "" );
141 
142  xIH->handle( xRequest.get() );
143 
145  = xRequest->getSelection();
146 
147  if ( xSelection.is() )
148  {
149  // Handler handled the request.
151  xAuthFallback = xRequest->getAuthFallbackInter( );
152  if ( xAuthFallback.is() )
153  {
154  OUString code = xAuthFallback->getCode( );
155  return strdup( OUSTR_TO_STDSTR( code ).c_str( ) );
156  }
157  }
158  }
159  }
160 
161  return strdup( "" );
162  }
163 }
164 
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Unicode code
#define STD_TO_OUSTR(str)
#define OUSTR_TO_STDSTR(s)