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<
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 instructions = "Open the following link in your browser and "
84  "paste the code from the URL you have been redirected to in the "
85  "box below. For example:\n"
86  "https://login.live.com/oauth20_desktop.srf?code=YOUR_CODE&lc=1033";
87  OUString url_oustr( url, strlen( url ), RTL_TEXTENCODING_UTF8 );
88  const css::uno::Reference<
89  css::ucb::XCommandEnvironment> xEnv = getXEnv( );
90 
91  if ( xEnv.is() )
92  {
93  uno::Reference< task::XInteractionHandler > xIH
94  = xEnv->getInteractionHandler();
95 
96  if ( xIH.is() )
97  {
100  instructions, 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  OUString instructions = "PIN:";
129  const css::uno::Reference<
130  css::ucb::XCommandEnvironment> xEnv = getXEnv( );
131 
132  if ( xEnv.is() )
133  {
134  uno::Reference< task::XInteractionHandler > xIH
135  = xEnv->getInteractionHandler();
136 
137  if ( xIH.is() )
138  {
141  instructions, "" );
142 
143  xIH->handle( xRequest.get() );
144 
146  = xRequest->getSelection();
147 
148  if ( xSelection.is() )
149  {
150  // Handler handled the request.
152  xAuthFallback = xRequest->getAuthFallbackInter( );
153  if ( xAuthFallback.is() )
154  {
155  OUString code = xAuthFallback->getCode( );
156  return strdup( OUSTR_TO_STDSTR( code ).c_str( ) );
157  }
158  }
159  }
160  }
161 
162  return strdup( "" );
163  }
164 }
165 
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define STD_TO_OUSTR(str)
#define OUSTR_TO_STDSTR(s)