|
1 | 1 | #pragma once |
2 | 2 |
|
3 | 3 | #include "message_signer.hpp" |
4 | | -#include <jwt-cpp/traits/nlohmann-json/defaults.h> |
5 | | - |
6 | | -#include <nabto/webrtc/device.hpp> |
7 | | -#include <nabto/webrtc/util/logging.hpp> |
8 | | -#include <nabto/webrtc/util/uuid.hpp> |
9 | | - |
10 | | -#include <nlohmann/json.hpp> |
11 | | - |
12 | | -#include <memory> |
13 | 4 |
|
14 | 5 | namespace nabto { |
15 | 6 | namespace webrtc { |
16 | 7 | namespace util { |
17 | 8 |
|
18 | | -class SharedSecretMessageSigner : public MessageSigner { |
| 9 | +class SharedSecretMessageSigner { |
19 | 10 | public: |
20 | | - static MessageSignerPtr create(std::string& secret, std::string& secretId) { |
21 | | - auto sig = std::make_shared<SharedSecretMessageSigner>(secret, secretId); |
22 | | - return sig; |
23 | | - } |
24 | | - |
25 | | - // NOLINTNEXTLINE(bugprone-easily-swappable-parameters) |
26 | | - SharedSecretMessageSigner(std::string& secret, std::string& secretId) |
27 | | - : secret_(secret), secretId_(secretId) { |
28 | | - myNonce_ = generate_uuid_v4(); |
29 | | - } |
30 | | - |
31 | | - static std::string getKeyId(const nlohmann::json& message) { |
32 | | - NPLOGD << "signaling signer handle msg" << message.dump(); |
33 | | - try { |
34 | | - auto jwt = message.at("jwt").get<std::string>(); |
35 | | - auto decoded = jwt::decode<jwt::traits::nlohmann_json>(jwt); |
36 | | - auto kidClaim = decoded.get_header_claim("kid"); |
37 | | - auto keyId = kidClaim.as_string(); |
38 | | - return keyId; |
39 | | - } catch (std::exception& ex) { |
40 | | - NPLOGE << "Failed to get key ID from JWT: " << ex.what(); |
41 | | - return ""; |
42 | | - } |
43 | | - } |
44 | | - |
45 | | - nlohmann::json signMessage(const nlohmann::json& msg) override { |
46 | | - if (nextMessageSignSeq_ != 0 && remoteNonce_.empty()) { |
47 | | - NPLOGE << "Tried to sign message with seq: " << nextMessageSignSeq_ |
48 | | - << " and an empty remote nonce"; |
49 | | - throw std::runtime_error("Invalid State"); |
50 | | - } |
51 | | - NPLOGD << "Signing message: " << msg.dump() |
52 | | - << " with seq: " << nextMessageSignSeq_ |
53 | | - << " and remoteNonce: " << remoteNonce_; |
54 | | - |
55 | | - const uint32_t seq = nextMessageSignSeq_; |
56 | | - nextMessageSignSeq_++; |
57 | | - |
58 | | - auto jwt = jwt::create<jwt::traits::nlohmann_json>() |
59 | | - .set_payload_claim("message", msg) |
60 | | - .set_payload_claim("messageSeq", seq) |
61 | | - .set_payload_claim("signerNonce", myNonce_); |
62 | | - |
63 | | - if (!secretId_.empty()) { |
64 | | - jwt.set_key_id(secretId_); |
65 | | - } |
66 | | - |
67 | | - if (!remoteNonce_.empty()) { |
68 | | - jwt.set_payload_claim("verifierNonce", remoteNonce_); |
69 | | - } |
70 | | - |
71 | | - auto token = jwt.sign(jwt::algorithm::hs256(secret_)); |
72 | | - |
73 | | - nlohmann::json message = {{"type", "JWT"}, {"jwt", token}}; |
74 | | - |
75 | | - return message; |
76 | | - } |
77 | | - |
78 | | - nlohmann::json verifyMessage(const nlohmann::json& msg) override { |
79 | | - NPLOGD << "signaling signer handle msg" << msg.dump(); |
80 | | - try { |
81 | | - auto jwt = msg.at("jwt").get<std::string>(); |
82 | | - auto decoded = jwt::decode(jwt); |
83 | | - |
84 | | - // TODO(tk): handle optional key ID and find secret based on key ID if it |
85 | | - // exists auto keyId = decoded.get_key_id(); |
86 | | - auto verifier = |
87 | | - jwt::verify().allow_algorithm(jwt::algorithm::hs256(secret_)); |
88 | | - verifier.verify(decoded); |
89 | | - const std::string& pl = decoded.get_payload(); |
90 | | - auto data = nlohmann::json::parse(pl); |
91 | | - NPLOGD << "DATA: " << data.dump(); |
92 | | - const uint32_t claimedSeq = data.at("messageSeq").get<uint32_t>(); |
93 | | - if (claimedSeq != nextMessageVerifySeq_) { |
94 | | - throw VerificationError(); |
95 | | - } |
96 | | - const std::string signerNonce = data.at("signerNonce").get<std::string>(); |
97 | | - if (claimedSeq == 0) { |
98 | | - remoteNonce_ = signerNonce; |
99 | | - } else { |
100 | | - if (signerNonce != remoteNonce_) { |
101 | | - throw VerificationError(); |
102 | | - } |
103 | | - const std::string verifyNonce = |
104 | | - data.at("verifierNonce").get<std::string>(); |
105 | | - if (verifyNonce != myNonce_) { |
106 | | - throw VerificationError(); |
107 | | - } |
108 | | - } |
109 | | - nextMessageVerifySeq_++; |
110 | | - auto message = data.at("message"); |
111 | | - return message; |
112 | | - } catch (std::exception& ex) { |
113 | | - NPLOGE << "Failed to validate JWT: " << ex.what(); |
114 | | - throw VerificationError(); |
115 | | - } |
116 | | - } |
117 | | - |
118 | | - private: |
119 | | - std::string secret_; |
120 | | - std::string secretId_; |
121 | | - |
122 | | - uint32_t nextMessageSignSeq_ = 0; |
123 | | - uint32_t nextMessageVerifySeq_ = 0; |
124 | | - std::string myNonce_; |
125 | | - std::string remoteNonce_; |
| 11 | + static MessageSignerPtr create(const std::string& secret, const std::string& secretId); |
| 12 | + static std::string getKeyId(const nlohmann::json& message); |
126 | 13 | }; |
127 | 14 |
|
128 | 15 | } // namespace util |
|
0 commit comments