]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
9f95a23c | 2 | // vim: ts=8 sw=2 smarttab ft=cpp |
11fdf7f2 | 3 | |
7c673cae FG |
4 | /* |
5 | * Ceph - scalable distributed file system | |
6 | * | |
7 | * Copyright (C) 2016 Red Hat, Inc | |
8 | * | |
9 | * This is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License version 2.1, as published by the Free Software | |
12 | * Foundation. See file COPYING. | |
13 | * | |
14 | */ | |
15 | ||
16 | #ifndef RGW_TOKEN_H | |
17 | #define RGW_TOKEN_H | |
18 | ||
19 | #include <stdint.h> | |
20 | #include <boost/algorithm/string.hpp> | |
21 | #include <sstream> | |
22 | ||
23 | #include "common/ceph_json.h" | |
24 | #include "common/Formatter.h" | |
25 | #include "rgw/rgw_b64.h" | |
26 | ||
27 | namespace rgw { | |
28 | ||
29 | using std::string; | |
30 | ||
31 | class RGWToken { | |
32 | public: | |
33 | static constexpr auto type_name = "RGW_TOKEN"; | |
34 | ||
35 | enum token_type : uint32_t { | |
36 | TOKEN_NONE, | |
37 | TOKEN_AD, | |
38 | TOKEN_KEYSTONE, | |
39 | TOKEN_LDAP, | |
40 | }; | |
41 | ||
42 | static enum token_type to_type(const string& s) { | |
43 | if (boost::iequals(s, "ad")) | |
44 | return TOKEN_AD; | |
45 | if (boost::iequals(s, "ldap")) | |
46 | return TOKEN_LDAP; | |
47 | if (boost::iequals(s, "keystone")) | |
48 | return TOKEN_KEYSTONE; | |
49 | return TOKEN_NONE; | |
50 | } | |
51 | ||
52 | static const char* from_type(enum token_type type) { | |
53 | switch (type) { | |
54 | case TOKEN_AD: | |
55 | return "ad"; | |
56 | break; | |
57 | case TOKEN_LDAP: | |
58 | return "ldap"; | |
59 | break; | |
60 | case TOKEN_KEYSTONE: | |
61 | return "keystone"; | |
62 | break; | |
63 | default: | |
64 | return "none"; | |
65 | }; | |
66 | } | |
67 | ||
68 | token_type type; | |
69 | string id; | |
70 | string key; | |
71 | ||
72 | virtual uint32_t version() const { return 1; }; | |
73 | ||
74 | bool valid() const{ | |
75 | return ((type != TOKEN_NONE) && | |
76 | (! id.empty()) && | |
77 | (! key.empty())); | |
78 | } | |
79 | ||
80 | RGWToken() | |
81 | : type(TOKEN_NONE) {}; | |
82 | ||
83 | RGWToken(enum token_type _type, const std::string& _id, | |
84 | const std::string& _key) | |
85 | : type(_type), id(_id), key(_key) {}; | |
86 | ||
87 | RGWToken(const string& json) { | |
88 | JSONParser p; | |
89 | p.parse(json.c_str(), json.length()); | |
90 | JSONDecoder::decode_json(RGWToken::type_name, *this, &p); | |
91 | } | |
92 | ||
93 | void encode(bufferlist& bl) const { | |
94 | uint32_t ver = version(); | |
95 | string typestr{from_type(type)}; | |
96 | ENCODE_START(1, 1, bl); | |
11fdf7f2 TL |
97 | encode(type_name, bl); |
98 | encode(ver, bl); | |
99 | encode(typestr, bl); | |
100 | encode(id, bl); | |
101 | encode(key, bl); | |
7c673cae FG |
102 | ENCODE_FINISH(bl); |
103 | } | |
104 | ||
11fdf7f2 | 105 | void decode(bufferlist::const_iterator& bl) { |
7c673cae FG |
106 | string name; |
107 | string typestr; | |
108 | uint32_t version; | |
109 | DECODE_START(1, bl); | |
11fdf7f2 TL |
110 | decode(name, bl); |
111 | decode(version, bl); | |
112 | decode(typestr, bl); | |
7c673cae | 113 | type = to_type(typestr); |
11fdf7f2 TL |
114 | decode(id, bl); |
115 | decode(key, bl); | |
7c673cae FG |
116 | DECODE_FINISH(bl); |
117 | } | |
118 | ||
119 | void dump(Formatter* f) const { | |
120 | ::encode_json("version", uint32_t(version()), f); | |
121 | ::encode_json("type", from_type(type), f); | |
122 | ::encode_json("id", id, f); | |
123 | ::encode_json("key", key, f); | |
124 | } | |
125 | ||
126 | void encode_json(Formatter* f) { | |
127 | RGWToken& token = *this; | |
128 | f->open_object_section(type_name); | |
129 | ::encode_json(type_name, token, f); | |
130 | f->close_section(); | |
131 | } | |
132 | ||
133 | void decode_json(JSONObj* obj) { | |
134 | uint32_t version; | |
135 | string type_name; | |
136 | string typestr; | |
137 | JSONDecoder::decode_json("version", version, obj); | |
138 | JSONDecoder::decode_json("type", typestr, obj); | |
139 | type = to_type(typestr); | |
140 | JSONDecoder::decode_json("id", id, obj); | |
141 | JSONDecoder::decode_json("key", key, obj); | |
142 | } | |
143 | ||
144 | std::string encode_json_base64(Formatter* f) { | |
145 | encode_json(f); | |
146 | std::ostringstream os; | |
147 | f->flush(os); | |
148 | return to_base64(std::move(os.str())); | |
149 | } | |
150 | ||
151 | friend inline ostream& operator<<(ostream& os, const RGWToken& token); | |
152 | ||
153 | virtual ~RGWToken() {}; | |
154 | }; | |
155 | WRITE_CLASS_ENCODER(RGWToken) | |
156 | ||
157 | inline ostream& operator<<(ostream& os, const RGWToken& token) | |
158 | { | |
159 | os << "<<RGWToken" | |
160 | << " type=" << RGWToken::from_type(token.type) | |
161 | << " id=" << token.id | |
162 | << " key=" << token.key | |
163 | << ">>"; | |
164 | return os; | |
165 | } | |
166 | ||
167 | } /* namespace rgw */ | |
168 | ||
169 | #endif /* RGW_TOKEN_H */ |