Skip to content

Commit fdb6116

Browse files
authored
Merge pull request #4 from djw8605/add-scitoken-get
Adding function scitoken_get_claim_string implementation
2 parents 18ebd2e + 6337f5b commit fdb6116

File tree

4 files changed

+61
-17
lines changed

4 files changed

+61
-17
lines changed

src/scitokens.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,18 @@ int scitoken_set_claim_string(SciToken token, const char *key, const char *value
6767
}
6868

6969
int scitoken_get_claim_string(const SciToken token, const char *key, char **value, char **err_msg) {
70-
if (err_msg) {
71-
*err_msg = strdup("This function is not implemented");
70+
scitokens::SciToken *real_token = reinterpret_cast<scitokens::SciToken*>(token);
71+
std::string claim_str;
72+
try {
73+
claim_str = real_token->get_claim_string(key);
74+
} catch (std::exception &exc) {
75+
if (err_msg) {
76+
*err_msg = strdup(exc.what());
77+
}
78+
return -1;
7279
}
73-
return -1;
80+
*value = strdup(claim_str.c_str());
81+
return 0;
7482
}
7583

7684

src/scitokens_internal.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ SciToken::deserialize(const std::string &data, const std::vector<std::string> al
316316
val.add_allowed_issuers(allowed_issuers);
317317
val.set_validate_all_claims_scitokens_1(false);
318318
val.verify(*m_decoded);
319+
320+
// Set all the claims
321+
m_claims = m_decoded->get_payload_claims();
319322
}
320323

321324

src/scitokens_internal.h

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#include <memory>
33
#include <sstream>
4+
#include <unordered_map>
45

56
#include <jwt-cpp/jwt.h>
67
#include <uuid/uuid.h>
@@ -111,39 +112,57 @@ friend class scitokens::Validator;
111112

112113
public:
113114
SciToken(SciTokenKey &signing_algorithm)
114-
: m_builder(jwt::create()),
115-
m_key(signing_algorithm)
115+
: m_key(signing_algorithm)
116116
{}
117117

118118
void
119119
set_claim(const std::string &key, const jwt::claim &value) {
120-
m_builder.set_payload_claim(key, value);
120+
m_claims[key] = value;
121121
if (key == "iss") {m_issuer_set = true;}
122122
}
123123

124+
const jwt::claim
125+
get_claim(const std::string &key) {
126+
return m_claims[key];
127+
}
128+
129+
// Return a claim as a string
130+
// If the claim is not a string, it can throw
131+
// a std::bad_cast() exception.
132+
const std::string
133+
get_claim_string(const std::string &key) {
134+
return m_claims[key].as_string();
135+
}
136+
124137
void
125138
set_lifetime(int lifetime) {
126139
m_lifetime = lifetime;
127140
}
128141

129142
std::string
130143
serialize() {
144+
jwt::builder builder(jwt::create());
145+
131146
if (!m_issuer_set) {
132147
throw MissingIssuerException();
133148
}
134149
auto time = std::chrono::system_clock::now();
135-
m_builder.set_issued_at(time);
136-
m_builder.set_not_before(time);
137-
m_builder.set_expires_at(time + std::chrono::seconds(m_lifetime));
150+
builder.set_issued_at(time);
151+
builder.set_not_before(time);
152+
builder.set_expires_at(time + std::chrono::seconds(m_lifetime));
138153

139154
uuid_t uuid;
140155
uuid_generate(uuid);
141156
char uuid_str[37];
142157
uuid_unparse_lower(uuid, uuid_str);
143-
m_builder.set_payload_claim("jti", std::string(uuid_str));
158+
m_claims["jti"] = std::string(uuid_str);
159+
160+
// Set all the payload claims
161+
for (auto it : m_claims) {
162+
builder.set_payload_claim(it.first, it.second);
163+
}
144164

145-
// TODO: handle JTI
146-
return m_key.serialize(m_builder);
165+
return m_key.serialize(builder);
147166
}
148167

149168
void
@@ -152,7 +171,7 @@ friend class scitokens::Validator;
152171
private:
153172
bool m_issuer_set{false};
154173
int m_lifetime{600};
155-
jwt::builder m_builder;
174+
std::unordered_map<std::string, jwt::claim> m_claims;
156175
std::unique_ptr<jwt::decoded_jwt> m_decoded;
157176
SciTokenKey &m_key;
158177
};

src/test.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <iostream>
22
#include <fstream>
3+
#include <stdio.h>
34

45
#include <jwt-cpp/jwt.h>
56

@@ -16,13 +17,17 @@ int main(int argc, const char** argv) {
1617
for (auto& e : decoded.get_payload_claims())
1718
std::cout << e.first << " = " << e.second.to_json() << std::endl;
1819

19-
std::ifstream ifs("test.pem");
20-
std::string contents( (std::istreambuf_iterator<char>(ifs)),
20+
std::ifstream priv_ifs("test.pem");
21+
std::string private_contents( (std::istreambuf_iterator<char>(priv_ifs)),
22+
(std::istreambuf_iterator<char>())
23+
);
24+
std::ifstream pub_ifs("test.pem.pub");
25+
std::string public_contents( (std::istreambuf_iterator<char>(pub_ifs)),
2126
(std::istreambuf_iterator<char>())
2227
);
2328

2429
char *err_msg;
25-
SciTokenKey key = scitoken_key_create("key-es356", "RS256", contents.c_str(), contents.c_str(), &err_msg);
30+
SciTokenKey key = scitoken_key_create("key-es356", "RS256", public_contents.c_str(), private_contents.c_str(), &err_msg);
2631
if (!key) {
2732
std::cout << "Failed to generate a key: " << err_msg << std::endl;
2833
return 1;
@@ -31,9 +36,18 @@ int main(int argc, const char** argv) {
3136
if (scitoken_set_claim_string(scitoken, "iss", "https://demo.scitokens.org", &err_msg)) {
3237
std::cout << "Failed to set a claim: " << err_msg << std::endl;
3338
}
39+
40+
// Test setting and getting a claim
3441
char *value;
42+
if (scitoken_get_claim_string(scitoken, "iss", &value, &err_msg)) {
43+
std::cout << "Failed to get a claim: " << err_msg << std::endl;
44+
}
45+
if (strcmp(value, "https://demo.scitokens.org") != 0) {
46+
std::cout << "Failed to get same claim a claim: " << err_msg << std::endl;
47+
}
48+
3549
if (scitoken_serialize(scitoken, &value, &err_msg)) {
36-
std::cout << "Failed to generate a key: " << err_msg << std::endl;
50+
std::cout << "Failed to generate a token: " << err_msg << std::endl;
3751
return 1;
3852
}
3953
std::cout << "SciToken: " << value << std::endl;

0 commit comments

Comments
 (0)