1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_UUI_PASSWORDCONTAINER_HXX
29 #define INCLUDED_UUI_PASSWORDCONTAINER_HXX
30 
31 #include "cppuhelper/implbase2.hxx"
32 
33 #include "com/sun/star/lang/XServiceInfo.hpp"
34 #include "com/sun/star/lang/XSingleServiceFactory.hpp"
35 #include "com/sun/star/task/XInteractionHandler.hpp"
36 #include "com/sun/star/task/XPasswordContainer.hpp"
37 
38 namespace com {
39     namespace sun {
40         namespace star {
41             namespace lang {
42                 class XMultiServiceFactory;
43             }
44             namespace ucb {
45                 class AuthenticationRequest;
46                 class XInteractionSupplyAuthentication;
47 } } } }
48 
49 namespace uui {
50 
51 // ============================================================================
52 
53 /** Passwordcontainer UNO service (com.sun.star.task.PasswordContainer) helper.
54  */
55 class PasswordContainerHelper
56 {
57 public:
58     PasswordContainerHelper(
59         com::sun::star::uno::Reference<
60             com::sun::star::lang::XMultiServiceFactory > const &
61                 xServiceFactory );
62 
63     // ------------------------------------------------------------------------
64 
65     /** This member function tries to handle an authentication interaction
66         request by looking up credentials for the given URL in the password
67         container service.
68 
69         In case of success the given interaction continuation
70         (XInteractionSupplyAuthentication) is filled with the credentials found
71         in the password container.
72 
73         Please note the the continuation gets not "selected" by this
74         implementation. "Selecting" the continuation is up to the caller (e.g.
75         an implementation of XInteractionHandler::handle) of this function.
76 
77         @param rRequest
78             The authentication request.
79 
80         @param xSupplyAuthentication
81             The "supply authentication" interaction continuation.
82 
83         @param rURL
84             The URL to lookup credentials for.
85 
86         @param xIH
87             The interaction handler to use, for example if a master password is
88             needed to access the password container.
89 
90         @return
91             True, if the authentication request was handled successfully.
92             False, otherwise.
93      */
94     bool handleAuthenticationRequest(
95         com::sun::star::ucb::AuthenticationRequest const & rRequest,
96         com::sun::star::uno::Reference<
97             com::sun::star::ucb::XInteractionSupplyAuthentication > const &
98                 xSupplyAuthentication,
99         rtl::OUString const & rURL,
100         com::sun::star::uno::Reference<
101             com::sun::star::task::XInteractionHandler > const & xIH )
102     SAL_THROW( (com::sun::star::uno::RuntimeException) );
103 
104     /** This member function adds credentials for the given URL to the password
105         container.
106 
107         @param rURL
108             The URL the credentials are valid for. rURL must not be empty.
109 
110         @param rUsername
111             The user name.
112 
113         @param rPasswords
114             This list of passwords.
115 
116         @param xIH
117             The interaction handler to use, for example if a master password is
118             needed to access the password container.
119 
120         @param bPersist
121             True, the record will get stored persistently; restored upon
122             password container initialization
123             False, the record will be stored until password container instance
124             gets destroyed.
125 
126         @return
127             True, if the record was added successfully.
128             False, otherwise.
129 
130     */
131     bool addRecord( rtl::OUString const & rURL,
132                     rtl::OUString const & rUsername,
133                     com::sun::star::uno::Sequence< rtl::OUString > const &
134                     rPasswords,
135                     com::sun::star::uno::Reference<
136                         com::sun::star::task::XInteractionHandler > const & xIH,
137                     bool bPersist )
138         SAL_THROW( (com::sun::star::uno::RuntimeException) );
139 
140     // ------------------------------------------------------------------------
141 
142 private:
143     com::sun::star::uno::Reference<
144         com::sun::star::task::XPasswordContainer > m_xPasswordContainer;
145 };
146 
147 // ============================================================================
148 
149 class PasswordContainerInteractionHandler :
150         public cppu::WeakImplHelper2< com::sun::star::lang::XServiceInfo,
151                                       com::sun::star::task::XInteractionHandler >
152 {
153 public:
154     PasswordContainerInteractionHandler(
155         const com::sun::star::uno::Reference<
156             com::sun::star::lang::XMultiServiceFactory >& rXSMgr );
157     virtual ~PasswordContainerInteractionHandler();
158 
159     // XServiceInfo
160     virtual ::rtl::OUString SAL_CALL getImplementationName()
161         throw ( com::sun::star::uno::RuntimeException );
162 
163     virtual sal_Bool SAL_CALL
164     supportsService( const ::rtl::OUString& ServiceName )
165         throw ( com::sun::star::uno::RuntimeException );
166 
167     virtual com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
168     getSupportedServiceNames()
169         throw ( com::sun::star::uno::RuntimeException );
170 
171     // XInteractionHandler
172     virtual void SAL_CALL
173     handle( const ::com::sun::star::uno::Reference<
174                 ::com::sun::star::task::XInteractionRequest >& Request )
175         throw (::com::sun::star::uno::RuntimeException);
176 
177     // Non-UNO interfaces
178     static rtl::OUString
179     getImplementationName_Static();
180 
181     static com::sun::star::uno::Sequence< rtl::OUString >
182     getSupportedServiceNames_Static();
183 
184     static com::sun::star::uno::Reference<
185             com::sun::star::lang::XSingleServiceFactory >
186     createServiceFactory( const com::sun::star::uno::Reference<
187             com::sun::star::lang::XMultiServiceFactory > & rxServiceMgr );
188 
189 private:
190     //com::sun::star::uno::Reference<
191     //    com::sun::star::lang::XMultiServiceFactory > m_xSMgr;
192     PasswordContainerHelper m_aPwContainerHelper;
193 };
194 
195 } // namespace uui
196 
197 #endif
198