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 <stout/json.hpp>
20 #include <stout/option.hpp>
21 #include <stout/try.hpp>
22 
23 namespace process {
24 namespace http {
25 namespace authentication {
26 
27 // Represents the various errors that can be returned when parsing or
28 // creating JSON Web Tokens. This can be useful to create proper
29 // responses to HTTP requests that included a token.
30 class JWTError : public Error {
31 public:
32  enum class Type {
33  INVALID_TOKEN, // Invalid token.
34  UNKNOWN // Internal error/all other errors.
35  };
36 
37  JWTError(const std::string& message, Type _type)
38  : Error(message), type(_type) {};
39 
40  const Type type;
41 };
42 
43 
55 class JWT
56 {
57 public:
58  enum class Alg
59  {
60  None,
61  HS256
62  };
63 
64  struct Header
65  {
68  };
69 
77  static Try<JWT, JWTError> parse(const std::string& token);
78 
89  const std::string& token,
90  const std::string& secret);
91 
101 
114  static Try<JWT, JWTError> create(
115  const JSON::Object& payload,
116  const std::string& secret);
117 
118  const Header header;
121 
122 private:
123  JWT(const Header& header,
124  const JSON::Object& payload,
126 };
127 
128 std::ostream& operator<<(std::ostream& stream, const JWT& jwt);
129 
130 } // namespace authentication {
131 } // namespace http {
132 } // namespace process {
133 
134 #endif // __PROCESS_JWT_HPP__
JWTError(const std::string &message, Type _type)
Definition: jwt.hpp:37
const Header header
Definition: jwt.hpp:118
Definition: errorbase.hpp:35
Definition: try.hpp:34
A JSON Web Token (JWT) implementation.
Definition: jwt.hpp:55
Definition: json.hpp:154
const Option< std::string > signature
Definition: jwt.hpp:120
Option< std::string > typ
Definition: jwt.hpp:67
static Try< JWT, JWTError > create(const JSON::Object &payload)
Create an unsecured JWT.
const std::string message
Definition: errorbase.hpp:45
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
static Try< JWT, JWTError > parse(const std::string &token)
Parse an unsecured JWT.
const Type type
Definition: jwt.hpp:38
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:119