pulsar-client-cpp
Loading...
Searching...
No Matches
Authentication.h
1
19#ifndef PULSAR_AUTHENTICATION_H_
20#define PULSAR_AUTHENTICATION_H_
21
22#include <pulsar/Result.h>
23#include <pulsar/defines.h>
24
25#include <functional>
26#include <map>
27#include <memory>
28#include <string>
29#include <vector>
30
31namespace pulsar {
32
34class Authentication;
35
36class PULSAR_PUBLIC AuthenticationDataProvider {
37 public:
39
43 virtual bool hasDataForTls();
44
48 virtual std::string getTlsCertificates();
49
53 virtual std::string getTlsPrivateKey();
54
58 virtual bool hasDataForHttp();
59
63 virtual std::string getHttpAuthType();
64
68 virtual std::string getHttpHeaders();
69
73 virtual bool hasDataFromCommand();
74
78 virtual std::string getCommandData();
79
80 protected:
82};
83
84typedef std::shared_ptr<AuthenticationDataProvider> AuthenticationDataPtr;
85typedef std::shared_ptr<Authentication> AuthenticationPtr;
86typedef std::map<std::string, std::string> ParamMap;
87
88class PULSAR_PUBLIC Authentication {
89 public:
90 virtual ~Authentication();
91
95 virtual const std::string getAuthMethodName() const = 0;
96
104 virtual Result getAuthData(AuthenticationDataPtr& authDataContent) {
105 authDataContent = authData_;
106 return ResultOk;
107 }
108
120 static ParamMap parseDefaultFormatAuthParams(const std::string& authParamsString);
121
122 protected:
124 AuthenticationDataPtr authData_;
125 friend class ClientConfiguration;
126};
127
136class PULSAR_PUBLIC AuthFactory {
137 public:
138 static AuthenticationPtr Disabled();
139
145 static AuthenticationPtr create(const std::string& pluginNameOrDynamicLibPath);
146
153 static AuthenticationPtr create(const std::string& pluginNameOrDynamicLibPath,
154 const std::string& authParamsString);
155
172 static AuthenticationPtr create(const std::string& pluginNameOrDynamicLibPath, ParamMap& params);
173
174 protected:
175 static bool isShutdownHookRegistered_;
176 static std::vector<void*> loadedLibrariesHandles_;
177 static void release_handles();
178};
179
183class PULSAR_PUBLIC AuthTls : public Authentication {
184 public:
185 AuthTls(AuthenticationDataPtr&);
186 ~AuthTls();
187
194 static AuthenticationPtr create(ParamMap& params);
195
201 static AuthenticationPtr create(const std::string& authParamsString);
202
209 static AuthenticationPtr create(const std::string& certificatePath, const std::string& privateKeyPath);
210
214 const std::string getAuthMethodName() const;
215
223 Result getAuthData(AuthenticationDataPtr& authDataTls);
224
225 private:
226 AuthenticationDataPtr authDataTls_;
227};
228
229typedef std::function<std::string()> TokenSupplier;
230
234class PULSAR_PUBLIC AuthToken : public Authentication {
235 public:
236 AuthToken(AuthenticationDataPtr&);
237 ~AuthToken();
238
254 static AuthenticationPtr create(ParamMap& params);
255
261 static AuthenticationPtr create(const std::string& authParamsString);
262
269 static AuthenticationPtr createWithToken(const std::string& token);
270
277 static AuthenticationPtr create(const TokenSupplier& tokenSupplier);
278
282 const std::string getAuthMethodName() const;
283
291 Result getAuthData(AuthenticationDataPtr& authDataToken);
292
293 private:
294 AuthenticationDataPtr authDataToken_;
295};
296
300class PULSAR_PUBLIC AuthBasic : public Authentication {
301 public:
302 explicit AuthBasic(AuthenticationDataPtr&);
303 ~AuthBasic() override;
304
311 static AuthenticationPtr create(ParamMap& params);
312
318 static AuthenticationPtr create(const std::string& authParamsString);
319
323 static AuthenticationPtr create(const std::string& username, const std::string& password);
324
328 static AuthenticationPtr create(const std::string& username, const std::string& password,
329 const std::string& method);
330
334 const std::string getAuthMethodName() const override;
335
343 Result getAuthData(AuthenticationDataPtr& authDataBasic) override;
344
345 private:
346 AuthenticationDataPtr authDataBasic_;
347};
348
352class PULSAR_PUBLIC AuthAthenz : public Authentication {
353 public:
354 AuthAthenz(AuthenticationDataPtr&);
355 ~AuthAthenz();
356
366 static AuthenticationPtr create(ParamMap& params);
367
373 static AuthenticationPtr create(const std::string& authParamsString);
374
378 const std::string getAuthMethodName() const;
379
387 Result getAuthData(AuthenticationDataPtr& authDataAthenz);
388
389 private:
390 AuthenticationDataPtr authDataAthenz_;
391};
392
393// OAuth 2.0 token and associated information.
394// currently mainly works for access token
396 public:
397 enum
398 {
399 undefined_expiration = -1
400 };
401
404
410 Oauth2TokenResult& setAccessToken(const std::string& accessToken);
411
417 Oauth2TokenResult& setIdToken(const std::string& idToken);
418
425 Oauth2TokenResult& setRefreshToken(const std::string& refreshToken);
426
432 Oauth2TokenResult& setExpiresIn(const int64_t expiresIn);
433
437 const std::string& getAccessToken() const;
438
442 const std::string& getIdToken() const;
443
448 const std::string& getRefreshToken() const;
449
453 int64_t getExpiresIn() const;
454
455 private:
456 // map to json "access_token"
457 std::string accessToken_;
458 // map to json "id_token"
459 std::string idToken_;
460 // map to json "refresh_token"
461 std::string refreshToken_;
462 // map to json "expires_in"
463 int64_t expiresIn_;
464};
465
466typedef std::shared_ptr<Oauth2TokenResult> Oauth2TokenResultPtr;
467
469 public:
470 virtual ~Oauth2Flow();
471
475 virtual void initialize() = 0;
476
481 virtual Oauth2TokenResultPtr authenticate() = 0;
482
486 virtual void close() = 0;
487
488 protected:
489 Oauth2Flow();
490};
491
492typedef std::shared_ptr<Oauth2Flow> FlowPtr;
493
495 public:
496 virtual ~CachedToken();
497
501 virtual bool isExpired() = 0;
502
508 virtual AuthenticationDataPtr getAuthData() = 0;
509
510 protected:
511 CachedToken();
512};
513
514typedef std::shared_ptr<CachedToken> CachedTokenPtr;
515
528class PULSAR_PUBLIC AuthOauth2 : public Authentication {
529 public:
530 AuthOauth2(ParamMap& params);
531 ~AuthOauth2();
532
541 static AuthenticationPtr create(ParamMap& params);
542
548 static AuthenticationPtr create(const std::string& authParamsString);
549
553 const std::string getAuthMethodName() const;
554
562 Result getAuthData(AuthenticationDataPtr& authDataOauth2);
563
564 private:
565 FlowPtr flowPtr_;
566 CachedTokenPtr cachedTokenPtr_;
567};
568
569} // namespace pulsar
570
571#endif /* PULSAR_AUTHENTICATION_H_ */
Definition Authentication.h:352
Result getAuthData(AuthenticationDataPtr &authDataAthenz)
const std::string getAuthMethodName() const
static AuthenticationPtr create(ParamMap &params)
static AuthenticationPtr create(const std::string &authParamsString)
Definition Authentication.h:300
Result getAuthData(AuthenticationDataPtr &authDataBasic) override
static AuthenticationPtr create(const std::string &username, const std::string &password)
static AuthenticationPtr create(const std::string &username, const std::string &password, const std::string &method)
static AuthenticationPtr create(const std::string &authParamsString)
const std::string getAuthMethodName() const override
static AuthenticationPtr create(ParamMap &params)
Definition Authentication.h:136
static AuthenticationPtr create(const std::string &pluginNameOrDynamicLibPath)
static AuthenticationPtr create(const std::string &pluginNameOrDynamicLibPath, ParamMap &params)
static AuthenticationPtr create(const std::string &pluginNameOrDynamicLibPath, const std::string &authParamsString)
Definition Authentication.h:528
static AuthenticationPtr create(ParamMap &params)
const std::string getAuthMethodName() const
static AuthenticationPtr create(const std::string &authParamsString)
Result getAuthData(AuthenticationDataPtr &authDataOauth2)
Definition Authentication.h:183
static AuthenticationPtr create(const std::string &authParamsString)
const std::string getAuthMethodName() const
static AuthenticationPtr create(ParamMap &params)
static AuthenticationPtr create(const std::string &certificatePath, const std::string &privateKeyPath)
Result getAuthData(AuthenticationDataPtr &authDataTls)
Definition Authentication.h:234
const std::string getAuthMethodName() const
Result getAuthData(AuthenticationDataPtr &authDataToken)
static AuthenticationPtr createWithToken(const std::string &token)
static AuthenticationPtr create(const std::string &authParamsString)
static AuthenticationPtr create(ParamMap &params)
static AuthenticationPtr create(const TokenSupplier &tokenSupplier)
Definition Authentication.h:36
virtual std::string getCommandData()
virtual std::string getTlsPrivateKey()
virtual std::string getHttpHeaders()
virtual std::string getHttpAuthType()
virtual std::string getTlsCertificates()
Definition Authentication.h:88
static ParamMap parseDefaultFormatAuthParams(const std::string &authParamsString)
virtual Result getAuthData(AuthenticationDataPtr &authDataContent)
Definition Authentication.h:104
virtual const std::string getAuthMethodName() const =0
Definition Authentication.h:494
virtual bool isExpired()=0
virtual AuthenticationDataPtr getAuthData()=0
Definition ClientConfiguration.h:29
Definition Authentication.h:468
virtual Oauth2TokenResultPtr authenticate()=0
virtual void initialize()=0
virtual void close()=0
Definition Authentication.h:395
int64_t getExpiresIn() const
const std::string & getAccessToken() const
Oauth2TokenResult & setRefreshToken(const std::string &refreshToken)
Oauth2TokenResult & setIdToken(const std::string &idToken)
Oauth2TokenResult & setExpiresIn(const int64_t expiresIn)
const std::string & getRefreshToken() const
const std::string & getIdToken() const
Oauth2TokenResult & setAccessToken(const std::string &accessToken)
Definition Authentication.h:31
Result
Definition Result.h:32
@ ResultOk
An internal error code used for retry.
Definition Result.h:34