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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
/* 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.
*/
#include <libcmis-c/session-factory.h>
#include <map>
#include <string>
#include <stdlib.h>
#include <libcmis/session-factory.hxx>
#include <libcmis-c/session.h>
#include <libcmis-c/vectors.h>
#include "internals.hxx"
using namespace std;
namespace
{
size_t const CRED_MAX_LEN = 1024;
class WrapperAuthProvider : public libcmis::AuthProvider
{
private:
libcmis_authenticationCallback m_callback;
public:
WrapperAuthProvider( libcmis_authenticationCallback callback ) :
m_callback( callback )
{
}
virtual ~WrapperAuthProvider( ) { };
virtual bool authenticationQuery( string& username, string& password );
};
bool WrapperAuthProvider::authenticationQuery( string& username, string& password )
{
/* NOTE: As I understand this, the callback is responsible for
* filling the correct username and password (possibly using
* the passed values as defaults in some dialog or so). But then
* there is no guarantee that the new username/password will
* not be longer than the present one, in which case it will
* not fit into the available space! For now, use a buffer size
* big enough for practical purposes.
*
* It might be a better idea to change the callback's signature
* to bool ( * )( char** username, char** password )
* and make it the callee's responsibility to reallocate the
* strings if it needs to.
*/
char user[CRED_MAX_LEN];
strncpy(user, username.c_str( ), sizeof( user ) );
user[CRED_MAX_LEN - 1] = '\0';
char pass[CRED_MAX_LEN];
strncpy(pass, password.c_str( ), sizeof( pass ) );
pass[CRED_MAX_LEN - 1] = '\0';
bool result = m_callback( user, pass );
// Update the username and password with the input
username = user;
password = pass;
return result;
}
class WrapperCertHandler : public libcmis::CertValidationHandler
{
private:
libcmis_certValidationCallback m_callback;
public:
WrapperCertHandler( libcmis_certValidationCallback callback ) :
m_callback( callback )
{
}
virtual ~WrapperCertHandler( ) { };
virtual bool validateCertificate( vector< string > certificatesChain );
};
bool WrapperCertHandler::validateCertificate( vector< string > certificatesChain )
{
libcmis_vector_string_Ptr chain = new ( nothrow ) libcmis_vector_string( );
if ( chain )
chain->handle = certificatesChain;
bool result = m_callback( chain );
libcmis_vector_string_free( chain );
return result;
}
}
std::string createString( char* str )
{
if ( str )
return string( str );
else
return string( );
}
void libcmis_setAuthenticationCallback( libcmis_authenticationCallback callback )
{
libcmis::AuthProviderPtr provider( new ( nothrow ) WrapperAuthProvider( callback ) );
if ( provider )
libcmis::SessionFactory::setAuthenticationProvider( provider );
}
void libcmis_setCertValidationCallback( libcmis_certValidationCallback callback )
{
libcmis::CertValidationHandlerPtr handler( new ( nothrow )WrapperCertHandler( callback ) );
if ( handler )
libcmis::SessionFactory::setCertificateValidationHandler( handler );
}
void libcmis_setOAuth2AuthCodeProvider( libcmis_oauth2AuthCodeProvider callback )
{
libcmis::SessionFactory::setOAuth2AuthCodeProvider( callback );
}
libcmis_oauth2AuthCodeProvider libcmis_getOAuth2AuthCodeProvider( )
{
return libcmis::SessionFactory::getOAuth2AuthCodeProvider( );
}
void libcmis_setProxySettings( char* proxy, char* noProxy,
char* proxyUser, char* proxyPass )
{
libcmis::SessionFactory::setProxySettings( string( proxy ), string( noProxy ),
string( proxyUser ), string( proxyPass ) );
}
const char* libcmis_getProxy( )
{
return libcmis::SessionFactory::getProxy( ).c_str();
}
const char* libcmis_getNoProxy( )
{
return libcmis::SessionFactory::getNoProxy( ).c_str();
}
const char* libcmis_getProxyUser( )
{
return libcmis::SessionFactory::getProxyUser( ).c_str();
}
const char* libcmis_getProxyPass( )
{
return libcmis::SessionFactory::getProxyPass( ).c_str();
}
libcmis_SessionPtr libcmis_createSession(
char* bindingUrl,
char* repositoryId,
char* username,
char* password,
bool noSslCheck,
libcmis_OAuth2DataPtr oauth2,
bool verbose,
libcmis_ErrorPtr error )
{
libcmis_SessionPtr session = NULL;
try
{
libcmis::OAuth2DataPtr oauth2Handle;
if ( oauth2 != NULL )
oauth2Handle = oauth2->handle;
libcmis::Session* handle = libcmis::SessionFactory::createSession(
createString( bindingUrl ),
createString( username ),
createString( password ),
createString( repositoryId ), noSslCheck, oauth2Handle, verbose );
session = new libcmis_session( );
session->handle = handle;
}
catch ( const libcmis::Exception& e )
{
if ( error != NULL )
{
error->message = strdup( e.what() );
error->type = strdup( e.getType().c_str() );
}
}
catch ( const bad_alloc& e )
{
if ( error != NULL )
{
error->message = strdup( e.what() );
error->badAlloc = true;
}
}
return session;
}
libcmis_vector_Repository_Ptr libcmis_getRepositories(
char* bindingUrl,
char* username,
char* password,
bool verbose,
libcmis_ErrorPtr error )
{
libcmis_SessionPtr session = libcmis_createSession(
bindingUrl, NULL, username, password, false, NULL, verbose, error );
libcmis_vector_Repository_Ptr repositories = libcmis_session_getRepositories( session );
libcmis_session_free( session );
return repositories;
}
|