]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/rgw_token.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rgw / rgw_token.h
CommitLineData
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
1e59de90 16#pragma once
7c673cae
FG
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
26namespace 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";
7c673cae
FG
55 case TOKEN_LDAP:
56 return "ldap";
7c673cae
FG
57 case TOKEN_KEYSTONE:
58 return "keystone";
7c673cae
FG
59 default:
60 return "none";
61 };
62 }
63
64 token_type type;
65 string id;
66 string key;
67
68 virtual uint32_t version() const { return 1; };
69
70 bool valid() const{
71 return ((type != TOKEN_NONE) &&
72 (! id.empty()) &&
73 (! key.empty()));
74 }
75
76 RGWToken()
77 : type(TOKEN_NONE) {};
78
79 RGWToken(enum token_type _type, const std::string& _id,
80 const std::string& _key)
81 : type(_type), id(_id), key(_key) {};
82
f67539c2 83 explicit RGWToken(const string& json) {
7c673cae
FG
84 JSONParser p;
85 p.parse(json.c_str(), json.length());
86 JSONDecoder::decode_json(RGWToken::type_name, *this, &p);
87 }
88
f67539c2
TL
89 RGWToken& operator=(const std::string& json) {
90 JSONParser p;
91 p.parse(json.c_str(), json.length());
92 JSONDecoder::decode_json(RGWToken::type_name, *this, &p);
93 return *this;
94 }
95
7c673cae
FG
96 void encode(bufferlist& bl) const {
97 uint32_t ver = version();
98 string typestr{from_type(type)};
99 ENCODE_START(1, 1, bl);
11fdf7f2
TL
100 encode(type_name, bl);
101 encode(ver, bl);
102 encode(typestr, bl);
103 encode(id, bl);
104 encode(key, bl);
7c673cae
FG
105 ENCODE_FINISH(bl);
106 }
107
11fdf7f2 108 void decode(bufferlist::const_iterator& bl) {
7c673cae
FG
109 string name;
110 string typestr;
111 uint32_t version;
112 DECODE_START(1, bl);
11fdf7f2
TL
113 decode(name, bl);
114 decode(version, bl);
115 decode(typestr, bl);
7c673cae 116 type = to_type(typestr);
11fdf7f2
TL
117 decode(id, bl);
118 decode(key, bl);
7c673cae
FG
119 DECODE_FINISH(bl);
120 }
121
122 void dump(Formatter* f) const {
123 ::encode_json("version", uint32_t(version()), f);
124 ::encode_json("type", from_type(type), f);
125 ::encode_json("id", id, f);
126 ::encode_json("key", key, f);
127 }
128
129 void encode_json(Formatter* f) {
130 RGWToken& token = *this;
131 f->open_object_section(type_name);
132 ::encode_json(type_name, token, f);
133 f->close_section();
134 }
135
136 void decode_json(JSONObj* obj) {
137 uint32_t version;
138 string type_name;
139 string typestr;
140 JSONDecoder::decode_json("version", version, obj);
141 JSONDecoder::decode_json("type", typestr, obj);
142 type = to_type(typestr);
143 JSONDecoder::decode_json("id", id, obj);
144 JSONDecoder::decode_json("key", key, obj);
145 }
146
147 std::string encode_json_base64(Formatter* f) {
148 encode_json(f);
149 std::ostringstream os;
150 f->flush(os);
151 return to_base64(std::move(os.str()));
152 }
153
20effc67 154 friend inline std::ostream& operator<<(std::ostream& os, const RGWToken& token);
7c673cae
FG
155
156 virtual ~RGWToken() {};
157 };
158 WRITE_CLASS_ENCODER(RGWToken)
159
20effc67 160 inline std::ostream& operator<<(std::ostream& os, const RGWToken& token)
7c673cae
FG
161 {
162 os << "<<RGWToken"
163 << " type=" << RGWToken::from_type(token.type)
164 << " id=" << token.id
165 << " key=" << token.key
166 << ">>";
167 return os;
168 }
169
170} /* namespace rgw */