PermissionRegistrationEndpoint.java

  1. /*******************************************************************************
  2.  * Copyright 2017 The MIT Internet Trust Consortium
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *******************************************************************************/

  16. package org.mitre.uma.web;

  17. import static org.mitre.oauth2.web.AuthenticationUtilities.ensureOAuthScope;
  18. import static org.mitre.util.JsonUtils.getAsLong;
  19. import static org.mitre.util.JsonUtils.getAsStringSet;

  20. import java.util.Set;

  21. import org.mitre.oauth2.model.SystemScope;
  22. import org.mitre.oauth2.service.SystemScopeService;
  23. import org.mitre.openid.connect.view.JsonEntityView;
  24. import org.mitre.openid.connect.view.JsonErrorView;
  25. import org.mitre.uma.model.PermissionTicket;
  26. import org.mitre.uma.model.ResourceSet;
  27. import org.mitre.uma.service.PermissionService;
  28. import org.mitre.uma.service.ResourceSetService;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.http.HttpStatus;
  33. import org.springframework.security.access.prepost.PreAuthorize;
  34. import org.springframework.security.core.Authentication;
  35. import org.springframework.stereotype.Controller;
  36. import org.springframework.ui.Model;
  37. import org.springframework.util.MimeTypeUtils;
  38. import org.springframework.web.bind.annotation.RequestBody;
  39. import org.springframework.web.bind.annotation.RequestMapping;
  40. import org.springframework.web.bind.annotation.RequestMethod;

  41. import com.google.gson.JsonElement;
  42. import com.google.gson.JsonObject;
  43. import com.google.gson.JsonParseException;
  44. import com.google.gson.JsonParser;

  45. /**
  46.  * @author jricher
  47.  *
  48.  */
  49. @Controller
  50. @RequestMapping("/" + PermissionRegistrationEndpoint.URL)
  51. @PreAuthorize("hasRole('ROLE_USER')")
  52. public class PermissionRegistrationEndpoint {
  53.     // Logger for this class
  54.     private static final Logger logger = LoggerFactory.getLogger(PermissionRegistrationEndpoint.class);

  55.     public static final String URL = "permission";

  56.     @Autowired
  57.     private PermissionService permissionService;

  58.     @Autowired
  59.     private ResourceSetService resourceSetService;

  60.     @Autowired
  61.     private SystemScopeService scopeService;

  62.     private JsonParser parser = new JsonParser();

  63.     @RequestMapping(method = RequestMethod.POST, consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
  64.     public String getPermissionTicket(@RequestBody String jsonString, Model m, Authentication auth) {

  65.         ensureOAuthScope(auth, SystemScopeService.UMA_PROTECTION_SCOPE);

  66.         try {

  67.             // parse the permission request

  68.             JsonElement el = parser.parse(jsonString);
  69.             if (el.isJsonObject()) {
  70.                 JsonObject o = el.getAsJsonObject();

  71.                 Long rsid = getAsLong(o, "resource_set_id");
  72.                 Set<String> scopes = getAsStringSet(o, "scopes");

  73.                 if (rsid == null || scopes == null || scopes.isEmpty()){
  74.                     // missing information
  75.                     m.addAttribute("code", HttpStatus.BAD_REQUEST);
  76.                     m.addAttribute("errorMessage", "Missing required component of permission registration request.");
  77.                     return JsonErrorView.VIEWNAME;
  78.                 }

  79.                 // trim any restricted scopes
  80.                 Set<SystemScope> scopesRequested = scopeService.fromStrings(scopes);
  81.                 scopesRequested = scopeService.removeRestrictedAndReservedScopes(scopesRequested);
  82.                 scopes = scopeService.toStrings(scopesRequested);

  83.                 ResourceSet resourceSet = resourceSetService.getById(rsid);

  84.                 // requested resource set doesn't exist
  85.                 if (resourceSet == null) {
  86.                     m.addAttribute("code", HttpStatus.NOT_FOUND);
  87.                     m.addAttribute("errorMessage", "Requested resource set not found: " + rsid);
  88.                     return JsonErrorView.VIEWNAME;
  89.                 }

  90.                 // authorized user of the token doesn't match owner of the resource set
  91.                 if (!resourceSet.getOwner().equals(auth.getName())) {
  92.                     m.addAttribute("code", HttpStatus.FORBIDDEN);
  93.                     m.addAttribute("errorMessage", "Party requesting permission is not owner of resource set, expected " + resourceSet.getOwner() + " got " + auth.getName());
  94.                     return JsonErrorView.VIEWNAME;
  95.                 }

  96.                 // create the permission
  97.                 PermissionTicket permission = permissionService.createTicket(resourceSet, scopes);

  98.                 if (permission != null) {
  99.                     // we've created the permission, return the ticket
  100.                     JsonObject out = new JsonObject();
  101.                     out.addProperty("ticket", permission.getTicket());
  102.                     m.addAttribute("entity", out);

  103.                     m.addAttribute("code", HttpStatus.CREATED);

  104.                     return JsonEntityView.VIEWNAME;
  105.                 } else {
  106.                     // there was a failure creating the permission object

  107.                     m.addAttribute("code", HttpStatus.INTERNAL_SERVER_ERROR);
  108.                     m.addAttribute("errorMessage", "Unable to save permission and generate ticket.");

  109.                     return JsonErrorView.VIEWNAME;
  110.                 }

  111.             } else {
  112.                 // malformed request
  113.                 m.addAttribute("code", HttpStatus.BAD_REQUEST);
  114.                 m.addAttribute("errorMessage", "Malformed JSON request.");
  115.                 return JsonErrorView.VIEWNAME;
  116.             }
  117.         } catch (JsonParseException e) {
  118.             // malformed request
  119.             m.addAttribute("code", HttpStatus.BAD_REQUEST);
  120.             m.addAttribute("errorMessage", "Malformed JSON request.");
  121.             return JsonErrorView.VIEWNAME;
  122.         }

  123.     }

  124. }