View Javadoc
1   /*
2    * Copyright 2008-2009 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;
18  
19  import org.springframework.security.authentication.AbstractAuthenticationToken;
20  
21  import java.util.Map;
22  
23  /**
24   * Authentication for an OAuth consumer.
25   * 
26   * @author Ryan Heaton
27   */
28  @SuppressWarnings("serial")
29  public class ConsumerAuthentication extends AbstractAuthenticationToken {
30  
31    private final ConsumerDetails consumerDetails;
32    private final ConsumerCredentials consumerCredentials;
33    private final Map<String, String> oauthParameters;
34    private boolean signatureValidated = false;
35  
36    public ConsumerAuthentication(ConsumerDetails consumerDetails, ConsumerCredentials consumerCredentials) {
37      super(consumerDetails.getAuthorities());
38      this.consumerDetails = consumerDetails;
39      this.consumerCredentials = consumerCredentials;
40      this.oauthParameters = null;
41    }
42  
43    public ConsumerAuthentication(ConsumerDetails consumerDetails, ConsumerCredentials consumerCredentials, Map<String, String> oauthParams) {
44      super(consumerDetails.getAuthorities());
45      this.consumerDetails = consumerDetails;
46      this.consumerCredentials = consumerCredentials;
47      this.oauthParameters = oauthParams;
48    }
49  
50    /**
51     * The credentials.
52     *
53     * @return The credentials.
54     * @see #getConsumerCredentials()
55     */
56    public Object getCredentials() {
57      return getConsumerCredentials();
58    }
59  
60    /**
61     * The credentials of this authentication.
62     *
63     * @return The credentials of this authentication.
64     */
65    public ConsumerCredentials getConsumerCredentials() {
66      return consumerCredentials;
67    }
68  
69    /**
70     * The principal ({@link #getConsumerDetails() consumer details}).
71     *
72     * @return The principal.
73     * @see #getConsumerDetails()
74     */
75    public Object getPrincipal() {
76      return getConsumerDetails();
77    }
78  
79    /**
80     * The consumer details.
81     *
82     * @return The consumer details.
83     */
84    public ConsumerDetails getConsumerDetails() {
85      return consumerDetails;
86    }
87  
88    /**
89     * Get the oauth parameters supplied in the request.
90     *
91     * @return The oauth parameters.
92     */
93    public Map<String, String> getOAuthParameters() {
94      return oauthParameters;
95    }
96  
97    /**
98     * The name of this principal is the consumer key.
99     *
100    * @return The name of this principal is the consumer key.
101    */
102   public String getName() {
103     return getConsumerDetails() != null ? getConsumerDetails().getConsumerKey() : null;
104   }
105 
106   /**
107    * Whether the signature has been validated.
108    *
109    * @return Whether the signature has been validated.
110    */
111   public boolean isSignatureValidated() {
112     return signatureValidated;
113   }
114 
115   /**
116    * Whether the signature has been validated.
117    *
118    * @param signatureValidated Whether the signature has been validated.
119    */
120   public void setSignatureValidated(boolean signatureValidated) {
121     this.signatureValidated = signatureValidated;
122   }
123 
124   /**
125    * Whether the signature has been validated.
126    *
127    * @return Whether the signature has been validated.
128    */
129   @Override
130   public boolean isAuthenticated() {
131     return isSignatureValidated();
132   }
133 
134   /**
135    * Whether the signature has been validated.
136    *
137    * @param authenticated Whether the signature has been validated.
138    */
139   @Override
140   public void setAuthenticated(boolean authenticated) {
141     setSignatureValidated(authenticated);
142   }
143 
144 }