XRootD
Loading...
Searching...
No Matches
Macaroons::Handler Class Reference

#include <XrdMacaroonsHandler.hh>

Inheritance diagram for Macaroons::Handler:
Collaboration diagram for Macaroons::Handler:

Public Types

enum  AuthzBehavior {
  PASSTHROUGH ,
  ALLOW ,
  DENY
}

Public Member Functions

 Handler (XrdSysError *log, const char *config, XrdOucEnv *myEnv, XrdAccAuthorize *chain)
virtual ~Handler ()
virtual int Init (const char *cfgfile) override
 Initializes the external request handler.
virtual bool MatchesPath (const char *verb, const char *path) override
 Tells if the incoming path is recognized as one of the paths that have to be processed.
virtual int ProcessReq (XrdHttpExtReq &req) override
Public Member Functions inherited from XrdHttpExtHandler
 XrdHttpExtHandler ()
 Constructor.
virtual ~XrdHttpExtHandler ()
 Destructor.

Static Public Member Functions

static bool Config (const char *config, XrdOucEnv *env, XrdSysError *log, std::string &location, std::string &secret, ssize_t &max_duration, AuthzBehavior &behavior)

Detailed Description

Definition at line 24 of file XrdMacaroonsHandler.hh.

Member Enumeration Documentation

◆ AuthzBehavior

Enumerator
PASSTHROUGH 
ALLOW 
DENY 

Definition at line 39 of file XrdMacaroonsHandler.hh.

Constructor & Destructor Documentation

◆ Handler()

Macaroons::Handler::Handler ( XrdSysError * log,
const char * config,
XrdOucEnv * myEnv,
XrdAccAuthorize * chain )
inline

Definition at line 26 of file XrdMacaroonsHandler.hh.

27 :
28 m_max_duration(86400),
29 m_chain(chain),
30 m_log(log)
31 {
32 AuthzBehavior behavior;
33 if (!Config(config, myEnv, m_log, m_location, m_secret, m_max_duration, behavior))
34 {
35 throw std::runtime_error("Macaroon handler config failed.");
36 }
37 }
static bool Config(const char *config, XrdOucEnv *env, XrdSysError *log, std::string &location, std::string &secret, ssize_t &max_duration, AuthzBehavior &behavior)

References Config().

Here is the call graph for this function:

◆ ~Handler()

Handler::~Handler ( )
virtual

Definition at line 66 of file XrdMacaroonsHandler.cc.

67{
68 delete m_chain;
69}

Member Function Documentation

◆ Config()

bool Handler::Config ( const char * config,
XrdOucEnv * env,
XrdSysError * log,
std::string & location,
std::string & secret,
ssize_t & max_duration,
AuthzBehavior & behavior )
static

Definition at line 36 of file XrdMacaroonsConfigure.cc.

39{
40 XrdOucStream config_obj(log, getenv("XRDINSTANCE"), env, "=====> ");
41
42 // Open and attach the config file
43 //
44 int cfg_fd;
45 if ((cfg_fd = open(config, O_RDONLY, 0)) < 0) {
46 return log->Emsg("Config", errno, "open config file", config);
47 }
48 config_obj.Attach(cfg_fd);
49 static const char *cvec[] = { "*** macaroons plugin config:", 0 };
50 config_obj.Capture(cvec);
51
52 // Set default mask for logging.
54
55 // Set default maximum duration (24 hours).
56 max_duration = 24*3600;
57
58 // Process items
59 //
60 char *orig_var, *var;
61 bool success = true, ismine;
62 while ((orig_var = config_obj.GetMyFirstWord())) {
63 var = orig_var;
64 if ((ismine = !strncmp("all.sitename", var, 12))) var += 4;
65 else if ((ismine = !strncmp("macaroons.", var, 10)) && var[10]) var += 10;
66
67
68
69 if (!ismine) {continue;}
70
71 if (!strcmp("secretkey", var)) {success = xsecretkey(config_obj, log, secret);}
72 else if (!strcmp("sitename", var)) {success = xsitename(config_obj, log, location);}
73 else if (!strcmp("trace", var)) {success = xtrace(config_obj, log);}
74 else if (!strcmp("maxduration", var)) {success = xmaxduration(config_obj, log, max_duration);}
75 else if (!strcmp("onmissing", var)) {success = xonmissing(config_obj, log, behavior);}
76 else {
77 log->Say("Config warning: ignoring unknown directive '", orig_var, "'.");
78 config_obj.Echo();
79 continue;
80 }
81 if (!success) {
82 config_obj.Echo();
83 break;
84 }
85 }
86
87 if (success && !location.size())
88 {
89 log->Emsg("Config", "all.sitename must be specified to use macaroons.");
90 return false;
91 }
92
93 return success;
94}
static bool xonmissing(XrdOucStream &config_obj, XrdSysError *log, Handler::AuthzBehavior &behavior)
#define open
Definition XrdPosix.hh:78
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
void Say(const char *text1, const char *text2=0, const char *txt3=0, const char *text4=0, const char *text5=0, const char *txt6=0)
void setMsgMask(int mask)

References XrdOucStream::Attach(), XrdOucStream::Capture(), XrdOucStream::Echo(), XrdSysError::Emsg(), Macaroons::Error, XrdOucStream::GetMyFirstWord(), open, XrdSysError::Say(), XrdSysError::setMsgMask(), Macaroons::Warning, and xonmissing().

Referenced by Macaroons::Authz::Authz(), and Handler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Init()

virtual int Macaroons::Handler::Init ( const char * cfgfile)
inlineoverridevirtual

Initializes the external request handler.

Implements XrdHttpExtHandler.

Definition at line 50 of file XrdMacaroonsHandler.hh.

50{return 0;}

◆ MatchesPath()

bool Handler::MatchesPath ( const char * verb,
const char * path )
overridevirtual

Tells if the incoming path is recognized as one of the paths that have to be processed.

Implements XrdHttpExtHandler.

Definition at line 135 of file XrdMacaroonsHandler.cc.

136{
137 return !strcmp(verb, "POST") || !strncmp(path, "/.well-known/", 13) ||
138 !strncmp(path, "/.oauth2/", 9);
139}

◆ ProcessReq()

int Handler::ProcessReq ( XrdHttpExtReq & )
overridevirtual

Process an HTTP request and send the response using the calling XrdHttpProtocol instance directly Returns 0 if ok, non0 if errors

Implements XrdHttpExtHandler.

Definition at line 291 of file XrdMacaroonsHandler.cc.

292{
293 if (req.resource == "/.well-known/oauth-authorization-server") {
294 return ProcessOAuthConfig(req);
295 } else if (req.resource == "/.oauth2/token") {
296 return ProcessTokenRequest(req);
297 }
298
299 auto header = XrdOucTUtils::caseInsensitiveFind(req.headers,"content-type");
300 if (header == req.headers.end() || header->second != "application/macaroon-request")
301 return req.SendSimpleResp(415, nullptr, "accept: application/macaroon-request",
302 "Content-Type must be 'application/macaroon-request' to request a macaroon", false);
303
304 header = XrdOucTUtils::caseInsensitiveFind(req.headers,"content-length");
305 if (header == req.headers.end())
306 return req.SendSimpleResp(411, nullptr, nullptr, "Content-Length missing; not a valid POST", false);
307
308 ssize_t blen = std::strtoll(header->second.c_str(), nullptr, 10);
309
310 if (blen <= 0)
311 return req.SendSimpleResp(400, nullptr, nullptr, "Content-Length has invalid value.", false);
312
313 if (blen > 4096)
314 return req.SendSimpleResp(413, nullptr, nullptr, "Macaroon request too large (must be less than 4KB)", false);
315
316 // request_data is not necessarily null-terminated; hence, we use the more advanced _ex variant
317 // of the tokener to avoid making a copy of the character buffer.
318 char *request_data;
319 if (req.BuffgetData(blen, &request_data, true) != blen)
320 {
321 return req.SendSimpleResp(400, nullptr, nullptr, "Missing or invalid body of request.", 0);
322 }
323 json_tokener *tokener = json_tokener_new();
324 if (!tokener)
325 {
326 return req.SendSimpleResp(500, nullptr, nullptr, "Internal error when allocating token parser.", 0);
327 }
328 json_object *macaroon_req = json_tokener_parse_ex(tokener, request_data, blen);
329 enum json_tokener_error err = json_tokener_get_error(tokener);
330 json_tokener_free(tokener);
331 if (err != json_tokener_success)
332 {
333 if (macaroon_req) json_object_put(macaroon_req);
334 return req.SendSimpleResp(400, nullptr, nullptr, "Invalid JSON serialization of macaroon request.", 0);
335 }
336 json_object *validity_obj;
337 if (!json_object_object_get_ex(macaroon_req, "validity", &validity_obj))
338 {
339 json_object_put(macaroon_req);
340 return req.SendSimpleResp(400, nullptr, nullptr, "JSON request does not include a `validity`", 0);
341 }
342 const char *validity_cstr = json_object_get_string(validity_obj);
343 if (!validity_cstr)
344 {
345 json_object_put(macaroon_req);
346 return req.SendSimpleResp(400, nullptr, nullptr, "validity key cannot be cast to a string", 0);
347 }
348 std::string validity_str(validity_cstr);
349 ssize_t validity = determine_validity(validity_str);
350 if (validity <= 0)
351 {
352 json_object_put(macaroon_req);
353 return req.SendSimpleResp(400, nullptr, nullptr, "Invalid ISO 8601 duration for validity key", 0);
354 }
355 json_object *caveats_obj;
356 std::vector<std::string> other_caveats;
357 if (json_object_object_get_ex(macaroon_req, "caveats", &caveats_obj))
358 {
359 if (json_object_is_type(caveats_obj, json_type_array))
360 { // Caveats were provided. Let's record them.
361 // TODO - could just add these in-situ. No need for the other_caveats vector.
362 int array_length = json_object_array_length(caveats_obj);
363 other_caveats.reserve(array_length);
364 for (int idx=0; idx<array_length; idx++)
365 {
366 json_object *caveat_item = json_object_array_get_idx(caveats_obj, idx);
367 if (caveat_item)
368 {
369 const char *caveat_item_str = json_object_get_string(caveat_item);
370
371 if (!caveat_item_str) {
372 json_object_put(macaroon_req);
373 return req.SendSimpleResp(400, nullptr, nullptr, "Malformed or invalid caveat", 0);
374 }
375
376 if (is_reserved_caveat(caveat_item_str)) {
377 json_object_put(macaroon_req);
378 return req.SendSimpleResp(400, nullptr, nullptr,
379 "Cannot accept caveat with reserved key (name, path, before)\n", 0);
380 }
381
382 if (!is_supported_caveat(caveat_item_str)) {
383 json_object_put(macaroon_req);
384 return req.SendSimpleResp(400, nullptr, nullptr,
385 "Cannot accept caveat of unsupported type (supported types: activity)\n", 0);
386 }
387
388 other_caveats.emplace_back(caveat_item_str);
389 }
390 }
391 }
392 }
393 json_object_put(macaroon_req);
394
395 return GenerateMacaroonResponse(req, req.resource, other_caveats, validity, false);
396}
static bool is_supported_caveat(const std::string &cv)
static bool is_reserved_caveat(const std::string &cv)
static std::map< std::string, T >::const_iterator caseInsensitiveFind(const std::map< std::string, T > &m, const std::string &lowerCaseSearchKey)
ssize_t determine_validity(const std::string &input)

References XrdHttpExtReq::BuffgetData(), XrdOucTUtils::caseInsensitiveFind(), Macaroons::determine_validity(), XrdHttpExtReq::headers, is_reserved_caveat(), is_supported_caveat(), XrdHttpExtReq::resource, and XrdHttpExtReq::SendSimpleResp().

Here is the call graph for this function:

The documentation for this class was generated from the following files: