AuthenticationUtilities.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.oauth2.web;

  17. import org.springframework.security.core.Authentication;
  18. import org.springframework.security.core.GrantedAuthority;
  19. import org.springframework.security.oauth2.common.exceptions.InsufficientScopeException;
  20. import org.springframework.security.oauth2.provider.OAuth2Authentication;

  21. import com.google.common.collect.ImmutableSet;

  22. /**
  23.  *
  24.  * Utility class to enforce OAuth scopes in authenticated requests.
  25.  *
  26.  * @author jricher
  27.  *
  28.  */
  29. public abstract class AuthenticationUtilities {

  30.     /**
  31.      * Makes sure the authentication contains the given scope, throws an exception otherwise
  32.      * @param auth the authentication object to check
  33.      * @param scope the scope to look for
  34.      * @throws InsufficientScopeException if the authentication does not contain that scope
  35.      */
  36.     public static void ensureOAuthScope(Authentication auth, String scope) {
  37.         // if auth is OAuth, make sure we've got the right scope
  38.         if (auth instanceof OAuth2Authentication) {
  39.             OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) auth;
  40.             if (oAuth2Authentication.getOAuth2Request().getScope() == null
  41.                     || !oAuth2Authentication.getOAuth2Request().getScope().contains(scope)) {
  42.                 throw new InsufficientScopeException("Insufficient scope", ImmutableSet.of(scope));
  43.             }
  44.         }
  45.     }

  46.     /**
  47.      * Check to see if the given auth object has ROLE_ADMIN assigned to it or not
  48.      * @param auth
  49.      * @return
  50.      */
  51.     public static boolean isAdmin(Authentication auth) {
  52.         for (GrantedAuthority grantedAuthority : auth.getAuthorities()) {
  53.             if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
  54.                 return true;
  55.             }
  56.         }
  57.         return false;
  58.     }


  59.     public static boolean hasRole(Authentication auth, String role) {
  60.         for (GrantedAuthority grantedAuthority : auth.getAuthorities()) {
  61.             if (grantedAuthority.getAuthority().equals(role)) {
  62.                 return true;
  63.             }
  64.         }
  65.         return false;

  66.     }

  67. }