| 1238 | |
| 1239 | |
| 1240 | process::Future<bool> authorizeEndpoint( |
| 1241 | const string& endpoint, |
| 1242 | const string& method, |
| 1243 | const Option<Authorizer*>& authorizer, |
| 1244 | const Option<Principal>& principal) |
| 1245 | { |
| 1246 | if (authorizer.isNone()) { |
| 1247 | return true; |
| 1248 | } |
| 1249 | |
| 1250 | authorization::Request request; |
| 1251 | |
| 1252 | // TODO(nfnt): Add an additional case when POST requests |
| 1253 | // need to be authorized separately from GET requests. |
| 1254 | if (method == "GET") { |
| 1255 | request.set_action(authorization::GET_ENDPOINT_WITH_PATH); |
| 1256 | } else { |
| 1257 | return Failure("Unexpected request method '" + method + "'"); |
| 1258 | } |
| 1259 | |
| 1260 | if (!authorization::AUTHORIZABLE_ENDPOINTS.contains(endpoint)) { |
| 1261 | return Failure( |
| 1262 | "Endpoint '" + endpoint + "' is not an authorizable endpoint."); |
| 1263 | } |
| 1264 | |
| 1265 | Option<authorization::Subject> subject = |
| 1266 | authorization::createSubject(principal); |
| 1267 | if (subject.isSome()) { |
| 1268 | request.mutable_subject()->CopyFrom(subject.get()); |
| 1269 | } |
| 1270 | |
| 1271 | request.mutable_object()->set_value(endpoint); |
| 1272 | |
| 1273 | LOG(INFO) << "Authorizing principal '" |
| 1274 | << (principal.isSome() ? stringify(principal.get()) : "ANY") |
| 1275 | << "' to " << method |
| 1276 | << " the '" << endpoint << "' endpoint"; |
| 1277 | |
| 1278 | return authorizer.get()->authorized(request); |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | namespace { |
no test coverage detected