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