1 /**********************************************************************
2  * Project:  CPL - Common Portability Library
3  * Purpose:  OpenStack Swift Object Storage routines
4  * Author:   Even Rouault <even.rouault at spatialys.com>
5  *
6  **********************************************************************
7  * Copyright (c) 2018, Even Rouault <even.rouault at spatialys.com>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  ****************************************************************************/
27 
28 #ifndef CPL_SWIFT_INCLUDED_H
29 #define CPL_SWIFT_INCLUDED_H
30 
31 #ifndef DOXYGEN_SKIP
32 
33 #ifdef HAVE_CURL
34 
35 #include <curl/curl.h>
36 #include "cpl_http.h"
37 #include "cpl_aws.h"
38 #include "cpl_json.h"
39 #include <map>
40 
41 class VSISwiftHandleHelper final: public IVSIS3LikeHandleHelper
42 {
43         CPLString m_osURL;
44         CPLString m_osStorageURL;
45         CPLString m_osAuthToken;
46         CPLString m_osBucket;
47         CPLString m_osObjectKey;
48 
49         static bool GetConfiguration(CPLString& osStorageURL,
50                                      CPLString& osAuthToken);
51 
52         static bool GetCached(const char* pszURLKey,
53                               const char* pszUserKey,
54                               const char* pszPasswordKey,
55                               CPLString& osStorageURL,
GetSwiftHeaders(const CPLString & osAuthToken)56                               CPLString& osAuthToken);
57 
58         static CPLString BuildURL(const CPLString& osStorageURL,
59                                   const CPLString& osBucket,
60                                   const CPLString& osObjectKey);
61 
62         void RebuildURL() override;
63 
64         // V1 Authentication
65         static bool CheckCredentialsV1();
66         static bool AuthV1(CPLString& osStorageURL,
67                            CPLString& osAuthToken);
68 
VSISwiftHandleHelper(const CPLString & osStorageURL,const CPLString & osAuthToken,const CPLString & osBucket,const CPLString & osObjectKey)69         // V3 Authentication
70         static bool CheckCredentialsV3(const CPLString& osAuthType);
71         static bool AuthV3(const CPLString& osAuthType,
72                            CPLString& osStorageURL,
73                            CPLString& osAuthToken);
74         static CPLJSONObject CreateAuthV3RequestObject(const CPLString& osAuthType);
75         static bool GetAuthV3StorageURL(const CPLHTTPResult *psResult,
76                                         CPLString& storageURL);
77 
78     public:
79         VSISwiftHandleHelper(const CPLString& osStorageURL,
80                              const CPLString& osAuthToken,
81                              const CPLString& osBucket,
82                              const CPLString& osObjectKey);
83        ~VSISwiftHandleHelper();
84 
~VSISwiftHandleHelper()85         bool Authenticate();
86 
87         static VSISwiftHandleHelper* BuildFromURI(const char* pszURI,
88                                                const char* pszFSPrefix);
89 
90         struct curl_slist* GetCurlHeaders(const CPLString& osVerbosVerb,
91                                           const struct curl_slist* psExistingHeaders,
92                                           const void *pabyDataContent = nullptr,
GetConfiguration(CPLString & osStorageURL,CPLString & osAuthToken)93                                           size_t nBytesContent = 0) const override;
94 
95         const CPLString& GetURL() const override { return m_osURL; }
96 
97         static void CleanMutex();
98         static void ClearCache();
99 };
100 
101 
102 #endif /* HAVE_CURL */
103 
104 #endif /* #ifndef DOXYGEN_SKIP */
105 
106 #endif /* CPL_SWIFT_INCLUDED_H */
107