summaryrefslogtreecommitdiffstats
path: root/inc/libcmis/session-factory.hxx
blob: 227ac4d572566e87c1a57d717974cea98fde5410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* libcmis
 * Version: MPL 1.1 / GPLv2+ / LGPLv2+
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License or as specified alternatively below. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Major Contributor(s):
 * Copyright (C) 2011 SUSE <cbosdonnat@suse.com>
 *
 *
 * All Rights Reserved.
 *
 * For minor contributions see the git repository.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPLv2+"), or
 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
 * in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
 * instead of those above.
 */
#ifndef _SESSION_FACTORY_HXX_
#define _SESSION_FACTORY_HXX_

#include <vector>
#include <map>
#include <string>

#include "libcmis/exception.hxx"
#include "libcmis/libcmis-api.h"
#include "libcmis/oauth2-data.hxx"
#include "libcmis/repository.hxx"
#include "libcmis/session.hxx"

// needed for a callback type
typedef void CURL;

namespace libcmis
{
    /** This callback provides the OAuth2 code or NULL.

        The returned string must be free()d by the caller.
      */
    typedef char* ( *OAuth2AuthCodeProvider )( const char* authUrl,
        const char* username, const char* password );

    class LIBCMIS_API AuthProvider
    {
        public:
            virtual ~AuthProvider() { };

            /** The function implementing it needs to fill the username and password parameters
                and return true. Returning false means that the user cancelled the authentication
                and will fail the query.
              */
            virtual bool authenticationQuery( std::string& username, std::string& password ) = 0;
    };
    typedef boost::shared_ptr< AuthProvider > AuthProviderPtr;
  
    /** Handler class used to request user input when an invalid SSL certificate is encountered.
     */ 
    class LIBCMIS_API CertValidationHandler
    {
        public:
             virtual ~CertValidationHandler( ){ };

             /** This function is provided a vector of X509 certificates encoded in base64, with
                 the first certificate being the one to validate, and the others are the issuers
                 chain.

                 The result will be stored in the session object to avoid asking several times
                 to validate the same certificate.

                 \result true if the certificate should be ignored, false to fail the request.
               */
             virtual bool validateCertificate( std::vector< std::string > certificatesChain ) = 0;
    };
    typedef boost::shared_ptr< CertValidationHandler > CertValidationHandlerPtr;

    typedef void(*CurlInitProtocolsFunction)(CURL *);

    class LIBCMIS_API SessionFactory
    {
        private:

            static AuthProviderPtr s_authProvider;

            static std::string s_proxy;
            static std::string s_noProxy;
            static std::string s_proxyUser;
            static std::string s_proxyPass;

            static OAuth2AuthCodeProvider s_oauth2AuthCodeProvider;

            static CertValidationHandlerPtr s_certValidationHandler;

        public:

            static void setAuthenticationProvider( AuthProviderPtr provider ) { s_authProvider = provider; }
            static AuthProviderPtr getAuthenticationProvider( ) { return s_authProvider; }
            
            static void setOAuth2AuthCodeProvider( OAuth2AuthCodeProvider provider ) { s_oauth2AuthCodeProvider = provider; }
            static OAuth2AuthCodeProvider getOAuth2AuthCodeProvider( ) { return s_oauth2AuthCodeProvider; }

            /** Set the handler to ask the user what to do with invalid SSL certificates. If not set,
                every invalid certificate will raise an exception.
              */
            static void setCertificateValidationHandler( CertValidationHandlerPtr handler ) { s_certValidationHandler = handler; }
            static CertValidationHandlerPtr getCertificateValidationHandler( ) { return s_certValidationHandler; }

            static void setCurlInitProtocolsFunction(CurlInitProtocolsFunction);

            static void setProxySettings( std::string proxy,
                    std::string noProxy,
                    std::string proxyUser,
                    std::string proxyPass );

            static const std::string& getProxy() { return s_proxy; }
            static const std::string& getNoProxy() { return s_noProxy; }
            static const std::string& getProxyUser() { return s_proxyUser; }
            static const std::string& getProxyPass() { return s_proxyPass; }

            /** Create a session from the given parameters. The binding type is automatically
                detected based on the provided URL.

                The resulting pointer should be deleted by the caller.
              */
            static Session* createSession( std::string bindingUrl,
                    std::string username = std::string( ),
                    std::string password = std::string( ),
                    std::string repositoryId = std::string( ),
                    bool noSslCheck = false,
                    OAuth2DataPtr oauth2 = OAuth2DataPtr(), bool verbose = false );

            /**
                Gets the informations of the repositories on the server.

                \deprecated
                    Since libcmis 0.4.0, this helper function simply creates a session
                    using the createSession function with no repository and then calls
                    getRepositories on the resulting session.
                    Kept only for backward API compatibility.
              */
            static std::vector< RepositoryPtr > getRepositories( std::string bindingUrl,
                    std::string username = std::string( ),
                    std::string password = std::string( ),
                    bool verbose = false );
    };
}

#endif