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 #include <openssl/rsa.h>
20 
21 #include <stout/json.hpp>
22 #include <stout/option.hpp>
23 #include <stout/try.hpp>
24 
25 namespace process {
26 namespace http {
27 namespace authentication {
28 
29 // Represents the various errors that can be returned when parsing or
30 // creating JSON Web Tokens. This can be useful to create proper
31 // responses to HTTP requests that included a token.
32 class JWTError : public Error {
33 public:
34  enum class Type {
35  INVALID_TOKEN, // Invalid token.
36  UNKNOWN // Internal error/all other errors.
37  };
38 
39  JWTError(const std::string& message, Type _type)
40  : Error(message), type(_type) {};
41 
42  const Type type;
43 };
44 
45 
57 class JWT
58 {
59 public:
60  enum class Alg
61  {
62  None,
63  HS256,
64  RS256
65  };
66 
67  struct Header
68  {
71  };
72 
80  static Try<JWT, JWTError> parse(const std::string& token);
81 
92  const std::string& token,
93  const std::string& secret);
94 
104  static Try<JWT, JWTError> parse(
105  const std::string& token,
106  std::shared_ptr<RSA> publicKey);
107 
116  static Try<JWT, JWTError> create(const JSON::Object& payload);
117 
130  static Try<JWT, JWTError> create(
131  const JSON::Object& payload,
132  const std::string& secret);
133 
146  static Try<JWT, JWTError> create(
147  const JSON::Object& payload,
148  std::shared_ptr<RSA> privateKey);
149 
150  const Header header;
153 
154 private:
155  JWT(const Header& header,
156  const JSON::Object& payload,
157  const Option<std::string>& signature);
158 };
159 
160 std::ostream& operator<<(std::ostream& stream, const JWT& jwt);
161 
162 } // namespace authentication {
163 } // namespace http {
164 } // namespace process {
165 
166 #endif // __PROCESS_JWT_HPP__
JWTError(const std::string &message, Type _type)
Definition: jwt.hpp:39
const Header header
Definition: jwt.hpp:150
Definition: errorbase.hpp:36
Definition: check.hpp:33
A JSON Web Token (JWT) implementation.
Definition: jwt.hpp:57
Definition: json.hpp:158
const Option< std::string > signature
Definition: jwt.hpp:152
Option< std::string > typ
Definition: jwt.hpp:70
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:40
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:151