001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *******************************************************************************/
016
017package org.mitre.oauth2.web;
018
019import org.springframework.security.core.Authentication;
020import org.springframework.security.core.GrantedAuthority;
021import org.springframework.security.oauth2.common.exceptions.InsufficientScopeException;
022import org.springframework.security.oauth2.provider.OAuth2Authentication;
023
024import com.google.common.collect.ImmutableSet;
025
026/**
027 *
028 * Utility class to enforce OAuth scopes in authenticated requests.
029 *
030 * @author jricher
031 *
032 */
033public abstract class AuthenticationUtilities {
034
035        /**
036         * Makes sure the authentication contains the given scope, throws an exception otherwise
037         * @param auth the authentication object to check
038         * @param scope the scope to look for
039         * @throws InsufficientScopeException if the authentication does not contain that scope
040         */
041        public static void ensureOAuthScope(Authentication auth, String scope) {
042                // if auth is OAuth, make sure we've got the right scope
043                if (auth instanceof OAuth2Authentication) {
044                        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) auth;
045                        if (oAuth2Authentication.getOAuth2Request().getScope() == null
046                                        || !oAuth2Authentication.getOAuth2Request().getScope().contains(scope)) {
047                                throw new InsufficientScopeException("Insufficient scope", ImmutableSet.of(scope));
048                        }
049                }
050        }
051
052        /**
053         * Check to see if the given auth object has ROLE_ADMIN assigned to it or not
054         * @param auth
055         * @return
056         */
057        public static boolean isAdmin(Authentication auth) {
058                for (GrantedAuthority grantedAuthority : auth.getAuthorities()) {
059                        if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
060                                return true;
061                        }
062                }
063                return false;
064        }
065
066
067        public static boolean hasRole(Authentication auth, String role) {
068                for (GrantedAuthority grantedAuthority : auth.getAuthorities()) {
069                        if (grantedAuthority.getAuthority().equals(role)) {
070                                return true;
071                        }
072                }
073                return false;
074
075        }
076
077}