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.consumer.token;
18  
19  import org.springframework.security.core.AuthenticationException;
20  import org.springframework.security.oauth.consumer.OAuthConsumerToken;
21  import org.springframework.security.oauth.consumer.OAuthSecurityContext;
22  import org.springframework.security.oauth.consumer.OAuthSecurityContextHolder;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpSession;
26  
27  /**
28   * Stores the tokens in an HTTP session.
29   *
30   * @author Ryan Heaton
31   */
32  public class HttpSessionBasedTokenServices implements OAuthConsumerTokenServices {
33  
34    public static final String KEY_PREFIX = "OAUTH_TOKEN";
35  
36  
37    public OAuthConsumerToken getToken(String resourceId) throws AuthenticationException {
38      HttpSession session = getSession();
39      OAuthConsumerTokenrg/springframework/security/oauth/consumer/OAuthConsumerToken.html#OAuthConsumerToken">OAuthConsumerToken consumerToken = (OAuthConsumerToken) session.getAttribute(KEY_PREFIX + "#" + resourceId);
40      if (consumerToken != null) {
41        Long expiration = (Long) session.getAttribute(KEY_PREFIX + "#" + resourceId + "#EXPIRATION");
42        if (expiration != null && (System.currentTimeMillis() > expiration)) {
43          //token expired; remove it
44          removeToken(resourceId);
45          consumerToken = null;
46        }
47      }
48  
49      return consumerToken;
50    }
51  
52    public void storeToken(String resourceId, OAuthConsumerToken token) {
53      HttpSession session = getSession();
54      session.setAttribute(KEY_PREFIX + "#" + resourceId, token);
55  
56      //adding support for oauth session extension (https://oauth.googlecode.com/svn/spec/ext/session/1.0/drafts/1/spec.html)
57      Long expiration = null;
58      String expiresInValue = token.getAdditionalParameters() != null ? token.getAdditionalParameters().get("oauth_expires_in") : null;
59      if (expiresInValue != null) {
60        try {
61          expiration = System.currentTimeMillis() + (Integer.parseInt(expiresInValue) * 1000);
62        }
63        catch (NumberFormatException e) {
64          //fall through.
65        }
66      }
67  
68      if (expiration != null) {
69        session.setAttribute(KEY_PREFIX + "#" + resourceId + "#EXPIRATION", expiration);
70      }
71    }
72  
73    public void removeToken(String resourceId) {
74      getSession().removeAttribute(KEY_PREFIX + "#" + resourceId);
75    }
76  
77    protected HttpSession getSession() {
78      OAuthSecurityContext context = OAuthSecurityContextHolder.getContext();
79      if (context == null) {
80        throw new IllegalStateException("A security context must be established.");
81      }
82  
83      HttpServletRequest request;
84      try {
85        request = (HttpServletRequest) context.getDetails();
86      }
87      catch (ClassCastException e) {
88        throw new IllegalStateException("The security context must have the HTTP servlet request as its details.");
89      }
90  
91      if (request == null) {
92        throw new IllegalStateException("The security context must have the HTTP servlet request as its details.");
93      }
94  
95      HttpSession session = request.getSession(true);
96      if (session == null) {
97        throw new IllegalStateException("Unable to create a session in which to store the tokens.");
98      }
99  
100     return session;
101   }
102 
103 }