Skip to content

Commit 3157b7d

Browse files
committed
minor tweeks from code review
1 parent fec3b3a commit 3157b7d

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

src/ArduinoJsonJWT.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,18 @@ void ArduinoJsonJWT::parseJWT(String jwt, JsonDocument &jsonDocument) {
6060
// clear json document before we begin, jsonDocument wil be null on failure
6161
jsonDocument.clear();
6262

63-
// must be of minimum length or greater
64-
if (jwt.length() <= JWT_SIG_SIZE + JWT_HEADER_SIZE + 2) {
65-
return;
66-
}
6763
// must have the correct header and delimiter
6864
if (!jwt.startsWith(JWT_HEADER) || jwt.indexOf('.') != JWT_HEADER_SIZE) {
6965
return;
7066
}
71-
// must have signature of correct length
72-
int signatureDelimiterIndex = jwt.length() - JWT_SIG_SIZE - 1;
73-
if (jwt.lastIndexOf('.') != signatureDelimiterIndex) {
67+
68+
// check there is a signature delimieter
69+
int signatureDelimiterIndex = jwt.lastIndexOf('.');
70+
if (signatureDelimiterIndex == JWT_HEADER_SIZE) {
7471
return;
7572
}
7673

77-
// signature must be correct
74+
// check the signature is valid
7875
String signature = jwt.substring(signatureDelimiterIndex + 1);
7976
jwt = jwt.substring(0, signatureDelimiterIndex);
8077
if (sign(jwt) != signature){

src/ArduinoJsonJWT.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111
#include <bearssl/bearssl_hmac.h>
1212
#endif
1313

14-
#define JWT_HEADER_SIZE 36
15-
#define JWT_SIG_SIZE 43
16-
1714
class ArduinoJsonJWT {
1815

1916
private:
2017
String _secret;
2118

22-
// {"alg": "HS256", "typ": "JWT"}
2319
const String JWT_HEADER = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
24-
20+
const size_t JWT_HEADER_SIZE = JWT_HEADER.length();
21+
2522
String sign(String &value);
2623

2724
static String encode(const char *cstr, int len);

0 commit comments

Comments
 (0)