View Javadoc

1   /**
2    *       Copyright 2010 Newcastle University
3    *
4    *          http://research.ncl.ac.uk/smart/
5    *
6    * Licensed to the Apache Software Foundation (ASF) under one or more
7    * contributor license agreements.  See the NOTICE file distributed with
8    * this work for additional information regarding copyright ownership.
9    * The ASF licenses this file to You under the Apache License, Version 2.0
10   * (the "License"); you may not use this file except in compliance with
11   * the License.  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   */
21  
22  package org.apache.amber.oauth2.client.demo.controller;
23  
24  import java.io.IOException;
25  
26  import javax.servlet.http.HttpServletRequest;
27  
28  import org.apache.amber.oauth2.client.OAuthClient;
29  import org.apache.amber.oauth2.client.URLConnectionClient;
30  import org.apache.amber.oauth2.client.demo.Utils;
31  import org.apache.amber.oauth2.client.demo.exception.ApplicationException;
32  import org.apache.amber.oauth2.client.demo.model.OAuthParams;
33  import org.apache.amber.oauth2.client.request.OAuthClientRequest;
34  import org.apache.amber.oauth2.client.response.GitHubTokenResponse;
35  import org.apache.amber.oauth2.client.response.OAuthAccessTokenResponse;
36  import org.apache.amber.oauth2.client.response.OAuthJSONAccessTokenResponse;
37  import org.apache.amber.oauth2.common.exception.OAuthProblemException;
38  import org.apache.amber.oauth2.common.exception.OAuthSystemException;
39  import org.apache.amber.oauth2.common.message.types.GrantType;
40  import org.springframework.stereotype.Controller;
41  import org.springframework.web.bind.annotation.ModelAttribute;
42  import org.springframework.web.bind.annotation.RequestMapping;
43  import org.springframework.web.servlet.ModelAndView;
44  
45  
46  /**
47   *
48   *
49   *
50   */
51  @Controller
52  @RequestMapping("/get_token")
53  public class TokenController {
54  
55      @RequestMapping
56      public ModelAndView authorize(@ModelAttribute("oauthParams") OAuthParams oauthParams,
57                                    HttpServletRequest req) throws OAuthSystemException, IOException {
58  
59          try {
60   
61              Utils.validateTokenParams(oauthParams);
62  
63              OAuthClientRequest request = OAuthClientRequest
64                  .tokenLocation(oauthParams.getTokenEndpoint())
65                  .setClientId(oauthParams.getClientId())
66                  .setClientSecret(oauthParams.getClientSecret())
67                  .setRedirectURI(oauthParams.getRedirectUri())
68                  .setCode(oauthParams.getAuthzCode())
69                  .setGrantType(GrantType.AUTHORIZATION_CODE)
70                  .buildBodyMessage();
71  
72              OAuthClient client = new OAuthClient(new URLConnectionClient());
73              String app = Utils.findCookieValue(req, "app");
74    
75              OAuthAccessTokenResponse oauthResponse = null;
76              Class<? extends OAuthAccessTokenResponse> cl = OAuthJSONAccessTokenResponse.class;
77  
78              if (Utils.FACEBOOK.equals(app)) {
79                  cl = GitHubTokenResponse.class;
80              } else if (Utils.GITHUB.equals(app)) {
81                  cl = GitHubTokenResponse.class;
82              }
83  
84              oauthResponse = client.accessToken(request, cl);
85  
86              oauthParams.setAccessToken(oauthResponse.getAccessToken());
87              oauthParams.setExpiresIn(oauthResponse.getExpiresIn());
88              oauthParams.setRefreshToken(Utils.isIssued(oauthResponse.getRefreshToken()));
89  
90              return new ModelAndView("get_resource");
91  
92          } catch (ApplicationException e) {
93              oauthParams.setErrorMessage(e.getMessage());
94              return new ModelAndView("request_token");
95          } catch (OAuthProblemException e) {
96              StringBuffer sb = new StringBuffer();
97              sb.append("</br>");
98              sb.append("Error code: ").append(e.getError()).append("</br>");
99              sb.append("Error description: ").append(e.getDescription()).append("</br>");
100             sb.append("Error uri: ").append(e.getUri()).append("</br>");
101             sb.append("State: ").append(e.getState()).append("</br>");
102             oauthParams.setErrorMessage(sb.toString());
103             return new ModelAndView("get_authz");
104         }
105     }
106 }