WhitelistAPI.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.WhitelistedSite;
  25. import org.mitre.openid.connect.service.WhitelistedSiteService;
  26. import org.mitre.openid.connect.view.HttpCodeView;
  27. import org.mitre.openid.connect.view.JsonEntityView;
  28. import org.mitre.openid.connect.view.JsonErrorView;
  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.http.MediaType;
  34. import org.springframework.security.access.prepost.PreAuthorize;
  35. import org.springframework.stereotype.Controller;
  36. import org.springframework.ui.ModelMap;
  37. import org.springframework.web.bind.annotation.PathVariable;
  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.Gson;
  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("/" + WhitelistAPI.URL)
  51. @PreAuthorize("hasRole('ROLE_USER')")
  52. public class WhitelistAPI {

  53.     public static final String URL = RootController.API_URL + "/whitelist";

  54.     @Autowired
  55.     private WhitelistedSiteService whitelistService;

  56.     /**
  57.      * Logger for this class
  58.      */
  59.     private static final Logger logger = LoggerFactory.getLogger(WhitelistAPI.class);

  60.     private Gson gson = new Gson();
  61.     private JsonParser parser = new JsonParser();

  62.     /**
  63.      * Get a list of all whitelisted sites
  64.      * @param m
  65.      * @return
  66.      */
  67.     @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  68.     public String getAllWhitelistedSites(ModelMap m) {

  69.         Collection<WhitelistedSite> all = whitelistService.getAll();

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

  71.         return JsonEntityView.VIEWNAME;
  72.     }

  73.     /**
  74.      * Create a new whitelisted site
  75.      * @param jsonString
  76.      * @param m
  77.      * @param p
  78.      * @return
  79.      */
  80.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  81.     @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  82.     public String addNewWhitelistedSite(@RequestBody String jsonString, ModelMap m, Principal p) {

  83.         JsonObject json;

  84.         WhitelistedSite whitelist = null;
  85.         try {
  86.             json = parser.parse(jsonString).getAsJsonObject();
  87.             whitelist = gson.fromJson(json, WhitelistedSite.class);

  88.         } catch (JsonParseException e) {
  89.             logger.error("addNewWhitelistedSite failed due to JsonParseException", e);
  90.             m.addAttribute(HttpCodeView.CODE, HttpStatus.BAD_REQUEST);
  91.             m.addAttribute(JsonErrorView.ERROR_MESSAGE, "Could not save new whitelisted site. The server encountered a JSON syntax exception. Contact a system administrator for assistance.");
  92.             return JsonErrorView.VIEWNAME;
  93.         } catch (IllegalStateException e) {
  94.             logger.error("addNewWhitelistedSite failed due to IllegalStateException", e);
  95.             m.addAttribute(HttpCodeView.CODE, HttpStatus.BAD_REQUEST);
  96.             m.addAttribute(JsonErrorView.ERROR_MESSAGE, "Could not save new whitelisted site. The server encountered an IllegalStateException. Refresh and try again - if the problem persists, contact a system administrator for assistance.");
  97.             return JsonErrorView.VIEWNAME;
  98.         }

  99.         // save the id of the person who created this
  100.         whitelist.setCreatorUserId(p.getName());

  101.         WhitelistedSite newWhitelist = whitelistService.saveNew(whitelist);

  102.         m.put(JsonEntityView.ENTITY, newWhitelist);

  103.         return JsonEntityView.VIEWNAME;

  104.     }

  105.     /**
  106.      * Update an existing whitelisted site
  107.      */
  108.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  109.     @RequestMapping(value="/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  110.     public String updateWhitelistedSite(@PathVariable("id") Long id, @RequestBody String jsonString, ModelMap m, Principal p) {

  111.         JsonObject json;

  112.         WhitelistedSite whitelist = null;
  113.         try {
  114.             json = parser.parse(jsonString).getAsJsonObject();
  115.             whitelist = gson.fromJson(json, WhitelistedSite.class);

  116.         } catch (JsonParseException e) {
  117.             logger.error("updateWhitelistedSite failed due to JsonParseException", e);
  118.             m.put(HttpCodeView.CODE, HttpStatus.BAD_REQUEST);
  119.             m.put(JsonErrorView.ERROR_MESSAGE, "Could not update whitelisted site. The server encountered a JSON syntax exception. Contact a system administrator for assistance.");
  120.             return JsonErrorView.VIEWNAME;
  121.         } catch (IllegalStateException e) {
  122.             logger.error("updateWhitelistedSite failed due to IllegalStateException", e);
  123.             m.put(HttpCodeView.CODE, HttpStatus.BAD_REQUEST);
  124.             m.put(JsonErrorView.ERROR_MESSAGE, "Could not update whitelisted site. The server encountered an IllegalStateException. Refresh and try again - if the problem persists, contact a system administrator for assistance.");
  125.             return JsonErrorView.VIEWNAME;
  126.         }

  127.         WhitelistedSite oldWhitelist = whitelistService.getById(id);

  128.         if (oldWhitelist == null) {
  129.             logger.error("updateWhitelistedSite failed; whitelist with id " + id + " could not be found.");
  130.             m.put(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
  131.             m.put(JsonErrorView.ERROR_MESSAGE, "Could not update whitelisted site. The requested whitelisted site with id " + id + "could not be found.");
  132.             return JsonErrorView.VIEWNAME;
  133.         } else {

  134.             WhitelistedSite newWhitelist = whitelistService.update(oldWhitelist, whitelist);

  135.             m.put(JsonEntityView.ENTITY, newWhitelist);

  136.             return JsonEntityView.VIEWNAME;
  137.         }
  138.     }

  139.     /**
  140.      * Delete a whitelisted site
  141.      *
  142.      */
  143.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  144.     @RequestMapping(value="/{id}", method = RequestMethod.DELETE)
  145.     public String deleteWhitelistedSite(@PathVariable("id") Long id, ModelMap m) {
  146.         WhitelistedSite whitelist = whitelistService.getById(id);

  147.         if (whitelist == null) {
  148.             logger.error("deleteWhitelistedSite failed; whitelist with id " + id + " could not be found.");
  149.             m.put(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
  150.             m.put(JsonErrorView.ERROR_MESSAGE, "Could not delete whitelisted site. The requested whitelisted site with id " + id + "could not be found.");
  151.             return JsonErrorView.VIEWNAME;
  152.         } else {
  153.             m.put(HttpCodeView.CODE, HttpStatus.OK);
  154.             whitelistService.remove(whitelist);
  155.         }

  156.         return HttpCodeView.VIEWNAME;
  157.     }

  158.     /**
  159.      * Get a single whitelisted site
  160.      */
  161.     @RequestMapping(value="/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  162.     public String getWhitelistedSite(@PathVariable("id") Long id, ModelMap m) {
  163.         WhitelistedSite whitelist = whitelistService.getById(id);
  164.         if (whitelist == null) {
  165.             logger.error("getWhitelistedSite failed; whitelist with id " + id + " could not be found.");
  166.             m.put(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
  167.             m.put(JsonErrorView.ERROR_MESSAGE, "The requested whitelisted site with id " + id + "could not be found.");
  168.             return JsonErrorView.VIEWNAME;
  169.         } else {

  170.             m.put(JsonEntityView.ENTITY, whitelist);

  171.             return JsonEntityView.VIEWNAME;
  172.         }

  173.     }

  174. }