Apache Mesos
jwt.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_JWT_HPP__
14 #define __PROCESS_JWT_HPP__
15 
16 #include <ostream>
17 #include <string>
18 
19 #ifdef __WINDOWS__
20 // NOTE: This must be included before the OpenSSL headers as it includes
21 // `WinSock2.h` and `Windows.h` in the correct order.
22 #include <stout/windows.hpp>
23 #endif // __WINDOWS__
24 
25 #include <openssl/rsa.h>
26 
27 #include <stout/json.hpp>
28 #include <stout/option.hpp>
29 #include <stout/try.hpp>
30 
31 namespace process {
32 namespace http {
33 namespace authentication {
34 
35 // Represents the various errors that can be returned when parsing or
36 // creating JSON Web Tokens. This can be useful to create proper
37 // responses to HTTP requests that included a token.
38 class JWTError : public Error {
39 public:
40  enum class Type {
41  INVALID_TOKEN, // Invalid token.
42  UNKNOWN // Internal error/all other errors.
43  };
44 
45  JWTError(const std::string& message, Type _type)
46  : Error(message), type(_type) {};
47 
48  const Type type;
49 };
50 
51 
63 class JWT
64 {
65 public:
66  enum class Alg
67  {
68  None,
69  HS256,
70  RS256
71  };
72 
73  struct Header
74  {
77  };
78 
86  static Try<JWT, JWTError> parse(const std::string& token);
87 
98  const std::string& token,
99  const std::string& secret);
100 
110  static Try<JWT, JWTError> parse(
111  const std::string& token,
112  std::shared_ptr<RSA> publicKey);
113 
122  static Try<JWT, JWTError> create(const JSON::Object& payload);
123 
136  static Try<JWT, JWTError> create(
137  const JSON::Object& payload,
138  const std::string& secret);
139 
152  static Try<JWT, JWTError> create(
153  const JSON::Object& payload,
154  std::shared_ptr<RSA> privateKey);
155 
156  const Header header;
159 
160 private:
161  JWT(const Header& header,
162  const JSON::Object& payload,
163  const Option<std::string>& signature);
164 };
165 
166 std::ostream& operator<<(std::ostream& stream, const JWT& jwt);
167 
168 } // namespace authentication {
169 } // namespace http {
170 } // namespace process {
171 
172 #endif // __PROCESS_JWT_HPP__
JWTError(const std::string &message, Type _type)
Definition: jwt.hpp:45
const Header header
Definition: jwt.hpp:156
Definition: errorbase.hpp:36
Definition: check.hpp:33
A JSON Web Token (JWT) implementation.
Definition: jwt.hpp:63
Definition: json.hpp:158
const Option< std::string > signature
Definition: jwt.hpp:158
Option< std::string > typ
Definition: jwt.hpp:76
Try< ImageManifest > parse(const std::string &value)
Definition: parse.hpp:36
const std::string message
Definition: errorbase.hpp:46
Definition: none.hpp:27
Definition: executor.hpp:48
const Type type
Definition: jwt.hpp:46
Try< Nothing > create(const std::string &hierarchy, const std::string &cgroup, bool recursive=false)
URI http(const std::string &host, const std::string &path="/", const Option< int > &port=None(), const Option< std::string > &query=None(), const Option< std::string > &fragment=None(), const Option< std::string > &user=None(), const Option< std::string > &password=None())
Creates an http URI with the given parameters.
Definition: http.hpp:35
std::ostream & operator<<(std::ostream &stream, const Principal &principal)
const JSON::Object payload
Definition: jwt.hpp:157