| 181 | |
| 182 | |
| 183 | Try<JWT, JWTError> JWT::parse(const std::string& token) |
| 184 | { |
| 185 | const vector<string> components = strings::split(token, "."); |
| 186 | |
| 187 | if (components.size() != 3) { |
| 188 | return JWTError( |
| 189 | "Expected 3 components in token, got " + stringify(components.size()), |
| 190 | JWTError::Type::INVALID_TOKEN); |
| 191 | } |
| 192 | |
| 193 | Try<JWT::Header> header = parse_header(components[0]); |
| 194 | |
| 195 | if (header.isError()) { |
| 196 | return JWTError(header.error(), JWTError::Type::INVALID_TOKEN); |
| 197 | } |
| 198 | |
| 199 | if (header->alg != JWT::Alg::None) { |
| 200 | return JWTError( |
| 201 | "Token 'alg' value \"" + stringify(header->alg) + |
| 202 | "\" does not match, expected \"none\"", |
| 203 | JWTError::Type::INVALID_TOKEN); |
| 204 | } |
| 205 | |
| 206 | Try<JSON::Object> payload = parse_payload(components[1]); |
| 207 | |
| 208 | if (payload.isError()) { |
| 209 | return JWTError(payload.error(), JWTError::Type::INVALID_TOKEN); |
| 210 | } |
| 211 | |
| 212 | if (!components[2].empty()) { |
| 213 | return JWTError( |
| 214 | "Unsecured JWT contains a signature", |
| 215 | JWTError::Type::INVALID_TOKEN); |
| 216 | } |
| 217 | |
| 218 | return JWT(header.get(), payload.get(), None()); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | Try<JWT, JWTError> JWT::parse(const string& token, const string& secret) |
nothing calls this directly
no test coverage detected