001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.server.guard;
018
019import org.apache.juneau.http.response.*;
020import org.apache.juneau.rest.server.*;
021
022/**
023 * REST method guard.
024 *
025 * <h5 class='topic'>Description</h5>
026 *
027 * Implements a guard mechanism for REST method calls that allows requests to be rejected before invocation of the REST
028 * method.
029 * For example, guards can be used to ensure that only administrators can call certain methods.
030 *
031 * <p>
032 * Guards are applied to REST methods declaratively through the {@link Rest#guards() @Rest(guards)} or
033 * {@link RestOp#guards() @RestOp(guards)} annotations.
034 *
035 * <p>
036 * If multiple guards are specified, ALL guards must pass in order for the request to proceed.
037 *
038 * <h5 class='topic'>How to implement</h5>
039 *
040 * Typically, guards will be used for permissions checking on the user making the request, but it can also be used for
041 * other purposes like pre-call validation of a request.
042 *
043 * <p>
044 * Implementers should simply throw a {@link BasicHttpException} from the {@link #guard(RestRequest, RestResponse)}
045 * method to abort processing on the current request.
046 *
047 * <p>
048 * Guards must implement a no-args constructor.
049 *
050 * <h5 class='topic'>Example usage:</h5>
051 * <p class='bjava'>
052 *    <jk>public</jk> MyResource <jk>extends</jk> BasicRestServlet {
053 *
054 *       <jc>// Delete method with guard that only allows Billy to call it.</jc>
055 *       <ja>@RestDelete</ja>(guards=BillyGuard.<jk>class</jk>)
056 *       <jk>public</jk> doDelete(RestRequest <jv>req</jv>, RestResponse <jv>res</jv>) <jk>throws</jk> Exception {...}
057 *    }
058 * </p>
059 *
060 * <h5 class='topic'>Example implementation:</h5>
061 * <p class='bjava'>
062 *    <jc>// Define a guard that only lets Billy make a request</jc>
063 *    <jk>public</jk> BillyGuard <jk>extends</jk> RestGuard {
064 *
065 *       <ja>@Override</ja>
066 *       <jk>public boolean</jk> isRequestAllowed(RestRequest <jv>req</jv>) {
067 *          <jk>return</jk> <jv>req</jv>.getUserPrincipal().getName().contains(<js>"Billy"</js>);
068 *       }
069 *    }
070 * </p>
071 *
072 * <h5 class='section'>See Also:</h5><ul>
073 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Guards">Guards</a>
074 * </ul>
075 */
076public abstract class RestGuard {
077
078   /**
079    * Checks the current HTTP request and throws a {@link BasicHttpException} if the guard does not permit the request.
080    *
081    * <p>
082    * By default, throws an <jsf>SC_FORBIDDEN</jsf> exception if {@link #isRequestAllowed(RestRequest)} returns
083    * <jk>false</jk>.
084    *
085    * <p>
086    * Subclasses are free to override this method to tailor the behavior of how to handle unauthorized requests.
087    *
088    * @param req The servlet request.
089    * @param res The servlet response.
090    * @throws BasicHttpException Thrown to abort processing on current request.
091    * @return
092    *    <jk>true</jk> if request can proceed.
093    *    Specify <jk>false</jk> if you're doing something like a redirection to a login page.
094    */
095   public boolean guard(RestRequest req, RestResponse res) throws BasicHttpException {
096      if (! isRequestAllowed(req))
097         throw new Forbidden("Access denied by guard");
098      return true;
099   }
100
101   /**
102    * Returns <jk>true</jk> if the specified request can pass through this guard.
103    *
104    * @param req The servlet request.
105    * @return <jk>true</jk> if the specified request can pass through this guard.
106    */
107   public abstract boolean isRequestAllowed(RestRequest req);
108}