View Javadoc
1   /*
2    * Copyright 2008 Web Cohesion
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    *   https://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  
17  package org.springframework.security.oauth.provider.attributes;
18  
19  import org.springframework.security.core.Authentication;
20  import org.springframework.security.core.GrantedAuthority;
21  import org.springframework.security.access.AccessDecisionVoter;
22  import org.springframework.security.access.ConfigAttribute;
23  import org.springframework.security.oauth.provider.OAuthAuthenticationDetails;
24  
25  import java.util.List;
26  import java.util.Collection;
27  
28  /**
29   * @author Ryan Heaton
30   * @author Andrew McCall
31   */
32  public class ConsumerSecurityVoter implements AccessDecisionVoter<Object> {
33  
34    /**
35     * The config attribute is supported if it's an instance of {@link org.springframework.security.oauth.provider.attributes.ConsumerSecurityConfig}.
36     *
37     * @param attribute The attribute.
38     * @return Whether the attribute is an instance of {@link org.springframework.security.oauth.provider.attributes.ConsumerSecurityConfig}.
39     */
40    public boolean supports(ConfigAttribute attribute) {
41      return attribute instanceof ConsumerSecurityConfig;
42    }
43  
44    /**
45     * All classes are supported.
46     *
47     * @param clazz The class.
48     * @return true.
49     */
50    public boolean supports(Class<?> clazz) {
51      return true;
52    }
53  
54    /**
55     * Votes on giving access to the specified authentication based on the security attributes.
56     *
57     * @param authentication The authentication.
58     * @param object The object.
59     * @param configAttributes the ConfigAttributes.
60     * @return The vote.
61     */
62    public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) {
63      int result = ACCESS_ABSTAIN;
64  
65      if (authentication.getDetails() instanceof OAuthAuthenticationDetails) {
66        OAuthAuthenticationDetails/springframework/security/oauth/provider/OAuthAuthenticationDetails.html#OAuthAuthenticationDetails">OAuthAuthenticationDetails details = (OAuthAuthenticationDetails) authentication.getDetails();
67        for (Object configAttribute : configAttributes) {
68          ConfigAttribute attribute = (ConfigAttribute) configAttribute;
69  
70          if (ConsumerSecurityConfig.PERMIT_ALL_ATTRIBUTE.equals(attribute)) {
71            return ACCESS_GRANTED;
72          }
73          else if (ConsumerSecurityConfig.DENY_ALL_ATTRIBUTE.equals(attribute)) {
74            return ACCESS_DENIED;
75          }
76          else if (supports(attribute)) {
77            ConsumerSecurityConfig./org/springframework/security/oauth/provider/attributes/ConsumerSecurityConfig.html#ConsumerSecurityConfig">ConsumerSecurityConfig config = (ConsumerSecurityConfig) attribute;
78            if ((config.getSecurityType() == ConsumerSecurityConfig.ConsumerSecurityType.CONSUMER_KEY)
79              && (config.getAttribute().equals(details.getConsumerDetails().getConsumerKey()))) {
80              return ACCESS_GRANTED;
81            }
82            else if (config.getSecurityType() == ConsumerSecurityConfig.ConsumerSecurityType.CONSUMER_ROLE) {
83              List<GrantedAuthority> authorities = details.getConsumerDetails().getAuthorities();
84              if (authorities != null) {
85                for (GrantedAuthority authority : authorities) {
86                  if (authority.getAuthority().equals(config.getAttribute())) {
87                    return ACCESS_GRANTED;
88                  }
89                }
90              }
91            }
92          }
93        }
94      }
95  
96      return result;
97    }
98  }