Skip to content

Commit 3a1d1d5

Browse files
committed
Adding function scitoken_get_claim_string implementation
1 parent 63b946a commit 3a1d1d5

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
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.h

Lines changed: 25 additions & 8 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

@@ -110,33 +111,49 @@ friend class scitokens::Validator;
110111

111112
public:
112113
SciToken(SciTokenKey &signing_algorithm)
113-
: m_builder(jwt::create()),
114-
m_key(signing_algorithm)
114+
: m_key(signing_algorithm)
115115
{}
116116

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

123+
const jwt::claim
124+
get_claim(const std::string &key) {
125+
return m_claims[key];
126+
}
127+
128+
const std::string
129+
get_claim_string(const std::string &key) {
130+
return m_claims[key].as_string();
131+
}
132+
123133
void
124134
set_lifetime(int lifetime) {
125135
m_lifetime = lifetime;
126136
}
127137

128138
std::string
129139
serialize() {
140+
jwt::builder builder(jwt::create());
141+
130142
if (!m_issuer_set) {
131143
throw MissingIssuerException();
132144
}
133145
auto time = std::chrono::system_clock::now();
134-
m_builder.set_issued_at(time);
135-
m_builder.set_not_before(time);
136-
m_builder.set_expires_at(time + std::chrono::seconds(m_lifetime));
146+
builder.set_issued_at(time);
147+
builder.set_not_before(time);
148+
builder.set_expires_at(time + std::chrono::seconds(m_lifetime));
149+
150+
// Set all the payload claims
151+
for (auto it : m_claims) {
152+
builder.set_payload_claim(it.first, it.second);
153+
}
137154

138155
// TODO: handle JTI
139-
return m_key.serialize(m_builder);
156+
return m_key.serialize(builder);
140157
}
141158

142159
void
@@ -145,7 +162,7 @@ friend class scitokens::Validator;
145162
private:
146163
bool m_issuer_set{false};
147164
int m_lifetime{600};
148-
jwt::builder m_builder;
165+
std::unordered_map<std::string, jwt::claim> m_claims;
149166
std::unique_ptr<jwt::decoded_jwt> m_decoded;
150167
SciTokenKey &m_key;
151168
};

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)