10Duke Scale C++ Client
All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
OAuthStateImpl.h
1#ifndef TENDUKE_OAUTH_OAUTHSTATEIMPL_H
2#define TENDUKE_OAUTH_OAUTHSTATEIMPL_H
3
4#include "./OAuthState.h"
5
6#include <map>
7
8namespace tenduke { namespace oauth {
9
14{
15public:
25 std::string accessToken,
26 std::string refreshToken,
27 int64_t expiresAt,
28 bool infinite,
29 std::map<std::string, std::string> additionalProperties
30 ) : accessToken(std::move(accessToken)), refreshToken(std::move(refreshToken)), expiresAt(expiresAt), infinite(infinite),
31 additionalProperties(std::move(additionalProperties))
32 {}
33
42 std::string accessToken,
43 std::string refreshToken,
44 int64_t expiresAt,
45 std::map<std::string, std::string> additionalProperties
46 ) : OAuthStateImpl(std::move(accessToken), std::move(refreshToken), expiresAt,
47 (expiresAt == tenduke::oauth::TOKEN_DOES_NOT_EXPIRE), std::move(additionalProperties))
48 {}
49
50 const std::string &getAccessToken() const override
51 {
52 return accessToken;
53 }
54
55 const std::string &getRefreshToken() const override
56 {
57 return refreshToken;
58 }
59
60 std::int64_t getExpiresAt() const override
61 {
62 return expiresAt;
63 }
64
65 const std::map<std::string, std::string> & getAdditionalProperties() const override
66 {
67 return additionalProperties;
68 }
69
70 const std::string *getAdditionalProperty(const std::string &name) const override
71 {
72 auto iterator = additionalProperties.find(name);
73
74 if (iterator == additionalProperties.end()) {
75 return nullptr;
76 }
77
78 return &(iterator->second);
79 }
80
81 bool hasAdditionalProperty(const std::string &name) const override
82 {
83 auto iterator = additionalProperties.find(name);
84
85 return !(iterator == additionalProperties.end());
86 }
87
88 bool hasAccessToken() const override
89 {
90 return !accessToken.empty();
91 }
92
93 bool doesExpire() const override
94 {
95 return !infinite;
96 }
97
98 bool isRefreshable() const override
99 {
100 return !refreshToken.empty();
101 }
102
103 void setAccessToken(const std::string &newAccessToken) override
104 {
105 this->accessToken = newAccessToken;
106 }
107
108 void setExpiresAt(std::int64_t newExpiresAt) override
109 {
110 this->expiresAt = newExpiresAt;
111 this->infinite = (expiresAt == tenduke::oauth::TOKEN_DOES_NOT_EXPIRE);
112 }
113
114 void setRefreshToken(const std::string &newRefreshToken) override
115 {
116 this->refreshToken = newRefreshToken;
117 }
118
119 void refreshFrom(const OAuthState &state) override
120 {
121 setExpiresAt(state.getExpiresAt());
123
124 // Only set refresh token if present in other state
125 // This assumes that if new refresh-token was not returned from the backend, the
126 // original refresh-token is to be re-used.
127 const std::string &newRefreshToken = state.getRefreshToken();
128 if (! newRefreshToken.empty()) {
129 setRefreshToken(newRefreshToken);
130 }
131 }
132
133 void removeAdditionalProperty(const std::string &propertyName) override
134 {
135 additionalProperties.erase(propertyName);
136 }
137
138private:
139 std::string accessToken;
140 std::string refreshToken;
141 std::int64_t expiresAt;
142 bool infinite; // If true, the token does not expire.
143 std::map<std::string, std::string> additionalProperties;
144};
145
146}}
147
148#endif //TENDUKE_OAUTH_OAUTHSTATEIMPL_H
Implementation of OAuthState.
Definition OAuthStateImpl.h:14
bool hasAccessToken() const override
Returns true if the state contains an access token.
Definition OAuthStateImpl.h:88
OAuthStateImpl(std::string accessToken, std::string refreshToken, int64_t expiresAt, std::map< std::string, std::string > additionalProperties)
Constructs new instance.
Definition OAuthStateImpl.h:41
void removeAdditionalProperty(const std::string &propertyName) override
Removes given additional property.
Definition OAuthStateImpl.h:133
const std::map< std::string, std::string > & getAdditionalProperties() const override
Returns all additional properties returned by the server.
Definition OAuthStateImpl.h:65
const std::string & getRefreshToken() const override
Returns the refresh token.
Definition OAuthStateImpl.h:55
OAuthStateImpl(std::string accessToken, std::string refreshToken, int64_t expiresAt, bool infinite, std::map< std::string, std::string > additionalProperties)
Constructs new instance.
Definition OAuthStateImpl.h:24
bool isRefreshable() const override
Returns true if the state is refreshable (i.e.
Definition OAuthStateImpl.h:98
void refreshFrom(const OAuthState &state) override
Refreshes the state from another state, This is called after refreshing the state with the service.
Definition OAuthStateImpl.h:119
bool doesExpire() const override
Returns true if the access token expires.
Definition OAuthStateImpl.h:93
std::int64_t getExpiresAt() const override
Returns timestamp (as epoch seconds) when the current access token expires.
Definition OAuthStateImpl.h:60
const std::string & getAccessToken() const override
Returns the access token.
Definition OAuthStateImpl.h:50
bool hasAdditionalProperty(const std::string &name) const override
Checks if the state has additional property.
Definition OAuthStateImpl.h:81
void setExpiresAt(std::int64_t newExpiresAt) override
Set the "expires at".
Definition OAuthStateImpl.h:108
void setRefreshToken(const std::string &newRefreshToken) override
Set the refresh token.
Definition OAuthStateImpl.h:114
const std::string * getAdditionalProperty(const std::string &name) const override
Returns additional property returned by the server.
Definition OAuthStateImpl.h:70
void setAccessToken(const std::string &newAccessToken) override
Set the access token.
Definition OAuthStateImpl.h:103
OAuth session state.
Definition OAuthState.h:17
virtual std::int64_t getExpiresAt() const =0
Returns timestamp (as epoch seconds) when the current access token expires.
virtual const std::string & getAccessToken() const =0
Returns the access token.
virtual const std::string & getRefreshToken() const =0
Returns the refresh token.
OAuth services.
Definition AccessTokenRequestAuthenticator.h:8
const std::int64_t TOKEN_DOES_NOT_EXPIRE
Magic value to denote that the access token does not expire.
Definition OAuthState.h:12
Root for classes, functions and globals of 10Duke C++ Client.
Definition BackendConfiguration.h:7