Skip to content

Commit 3c07a1b

Browse files
committed
Add API to fetch expiry time.
1 parent a893f27 commit 3c07a1b

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/scitokens.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ int scitoken_set_claim_string(SciToken token, const char *key, const char *value
6666
return 0;
6767
}
6868

69+
6970
int scitoken_get_claim_string(const SciToken token, const char *key, char **value, char **err_msg) {
7071
scitokens::SciToken *real_token = reinterpret_cast<scitokens::SciToken*>(token);
7172
std::string claim_str;
@@ -82,6 +83,27 @@ int scitoken_get_claim_string(const SciToken token, const char *key, char **valu
8283
}
8384

8485

86+
int scitoken_get_expiration(const SciToken token, long long *expiry, char **err_msg) {
87+
scitokens::SciToken *real_token = reinterpret_cast<scitokens::SciToken*>(token);
88+
if (!real_token->has_claim("exp")) {
89+
*expiry = -1;
90+
return 0;
91+
}
92+
93+
long long result;
94+
try {
95+
result = real_token->get_claim("exp").as_int();
96+
} catch (std::exception &exc) {
97+
if (err_msg) {
98+
*err_msg = strdup(exc.what());
99+
}
100+
return -1;
101+
}
102+
*expiry = result;
103+
return 0;
104+
}
105+
106+
85107
void scitoken_set_lifetime(SciToken token, int lifetime) {
86108
if (token == nullptr) {return;}
87109
scitokens::SciToken *real_token = reinterpret_cast<scitokens::SciToken*>(token);

src/scitokens.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ int scitoken_set_claim_string(SciToken token, const char *key, const char *value
3232

3333
int scitoken_get_claim_string(const SciToken token, const char *key, char **value, char **err_msg);
3434

35+
int scitoken_get_expiration(const SciToken token, long long *value, char **err_msg);
36+
3537
void scitoken_set_lifetime(SciToken token, int lifetime);
3638

3739
int scitoken_serialize(const SciToken token, char **value, char **err_msg);

src/scitokens_internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ friend class scitokens::Validator;
126126
return m_claims[key];
127127
}
128128

129+
bool
130+
has_claim(const std::string &key) const {
131+
return m_claims.find(key) != m_claims.end();
132+
}
133+
129134
// Return a claim as a string
130135
// If the claim is not a string, it can throw
131136
// a std::bad_cast() exception.

0 commit comments

Comments
 (0)