ApprovedSiteAPI.java

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

  22. import java.security.Principal;
  23. import java.util.Collection;

  24. import org.mitre.openid.connect.model.ApprovedSite;
  25. import org.mitre.openid.connect.service.ApprovedSiteService;
  26. import org.mitre.openid.connect.view.HttpCodeView;
  27. import org.mitre.openid.connect.view.JsonApprovedSiteView;
  28. import org.mitre.openid.connect.view.JsonEntityView;
  29. import org.mitre.openid.connect.view.JsonErrorView;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.http.HttpStatus;
  34. import org.springframework.http.MediaType;
  35. import org.springframework.security.access.prepost.PreAuthorize;
  36. import org.springframework.stereotype.Controller;
  37. import org.springframework.ui.ModelMap;
  38. import org.springframework.web.bind.annotation.PathVariable;
  39. import org.springframework.web.bind.annotation.RequestMapping;
  40. import org.springframework.web.bind.annotation.RequestMethod;

  41. /**
  42.  * @author jricher
  43.  *
  44.  */
  45. @Controller
  46. @RequestMapping("/" + ApprovedSiteAPI.URL)
  47. @PreAuthorize("hasRole('ROLE_USER')")
  48. public class ApprovedSiteAPI {

  49.     public static final String URL = RootController.API_URL + "/approved";

  50.     @Autowired
  51.     private ApprovedSiteService approvedSiteService;

  52.     /**
  53.      * Logger for this class
  54.      */
  55.     private static final Logger logger = LoggerFactory.getLogger(ApprovedSiteAPI.class);

  56.     /**
  57.      * Get a list of all of this user's approved sites
  58.      * @param m
  59.      * @return
  60.      */
  61.     @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  62.     public String getAllApprovedSites(ModelMap m, Principal p) {

  63.         Collection<ApprovedSite> all = approvedSiteService.getByUserId(p.getName());

  64.         m.put(JsonEntityView.ENTITY, all);

  65.         return JsonApprovedSiteView.VIEWNAME;
  66.     }

  67.     /**
  68.      * Delete an approved site
  69.      *
  70.      */
  71.     @RequestMapping(value="/{id}", method = RequestMethod.DELETE)
  72.     public String deleteApprovedSite(@PathVariable("id") Long id, ModelMap m, Principal p) {
  73.         ApprovedSite approvedSite = approvedSiteService.getById(id);

  74.         if (approvedSite == null) {
  75.             logger.error("deleteApprovedSite failed; no approved site found for id: " + id);
  76.             m.put(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
  77.             m.put(JsonErrorView.ERROR_MESSAGE, "Could not delete approved site. The requested approved site with id: " + id + " could not be found.");
  78.             return JsonErrorView.VIEWNAME;
  79.         } else if (!approvedSite.getUserId().equals(p.getName())) {
  80.             logger.error("deleteApprovedSite failed; principal "
  81.                     + p.getName() + " does not own approved site" + id);
  82.             m.put(HttpCodeView.CODE, HttpStatus.FORBIDDEN);
  83.             m.put(JsonErrorView.ERROR_MESSAGE, "You do not have permission to delete this approved site. The approved site decision will not be deleted.");
  84.             return JsonErrorView.VIEWNAME;
  85.         } else {
  86.             m.put(HttpCodeView.CODE, HttpStatus.OK);
  87.             approvedSiteService.remove(approvedSite);
  88.         }

  89.         return HttpCodeView.VIEWNAME;
  90.     }

  91.     /**
  92.      * Get a single approved site
  93.      */
  94.     @RequestMapping(value="/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  95.     public String getApprovedSite(@PathVariable("id") Long id, ModelMap m, Principal p) {
  96.         ApprovedSite approvedSite = approvedSiteService.getById(id);
  97.         if (approvedSite == null) {
  98.             logger.error("getApprovedSite failed; no approved site found for id: " + id);
  99.             m.put(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
  100.             m.put(JsonErrorView.ERROR_MESSAGE, "The requested approved site with id: " + id + " could not be found.");
  101.             return JsonErrorView.VIEWNAME;
  102.         } else if (!approvedSite.getUserId().equals(p.getName())) {
  103.             logger.error("getApprovedSite failed; principal "
  104.                     + p.getName() + " does not own approved site" + id);
  105.             m.put(HttpCodeView.CODE, HttpStatus.FORBIDDEN);
  106.             m.put(JsonErrorView.ERROR_MESSAGE, "You do not have permission to view this approved site.");
  107.             return JsonErrorView.VIEWNAME;
  108.         } else {
  109.             m.put(JsonEntityView.ENTITY, approvedSite);
  110.             return JsonApprovedSiteView.VIEWNAME;
  111.         }

  112.     }

  113. }