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 javax.servlet.http.Cookie;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.amber.oauth2.client.demo.Utils;
29  import org.apache.amber.oauth2.client.demo.model.OAuthParams;
30  import org.apache.amber.oauth2.client.response.OAuthAuthzResponse;
31  import org.apache.amber.oauth2.common.exception.OAuthProblemException;
32  import org.springframework.stereotype.Controller;
33  import org.springframework.web.bind.annotation.ModelAttribute;
34  import org.springframework.web.bind.annotation.RequestMapping;
35  import org.springframework.web.bind.annotation.RequestMethod;
36  import org.springframework.web.servlet.ModelAndView;
37  
38  /**
39   *
40   *
41   *
42   */
43  @Controller
44  @RequestMapping("/redirect")
45  public class RedirectController {
46  
47      @RequestMapping(method = RequestMethod.GET)
48      public ModelAndView handleRedirect(@ModelAttribute("oauthParams") OAuthParams oauthParams,
49                                         HttpServletRequest request,
50                                         HttpServletResponse response) {
51  
52  
53          try {
54  
55              // Create the response wrapper
56              OAuthAuthzResponse oar = null;
57              oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
58  
59  
60              // Get Authorization Code
61              String code = oar.getCode();
62  
63              // Get OAuth Info
64              String clientId = Utils.findCookieValue(request, "clientId");
65              String clientSecret = Utils.findCookieValue(request, "clientSecret");
66              String authzEndpoint = Utils.findCookieValue(request, "authzEndpoint");
67              String tokenEndpoint = Utils.findCookieValue(request, "tokenEndpoint");
68              String redirectUri = Utils.findCookieValue(request, "redirectUri");
69              String scope = Utils.findCookieValue(request, "scope");
70  
71              String app = Utils.findCookieValue(request, "app");
72              response.addCookie(new Cookie("app", app));
73  
74              oauthParams.setAuthzCode(code);
75              oauthParams.setClientId(clientId);
76              oauthParams.setClientSecret(clientSecret);
77              oauthParams.setAuthzEndpoint(authzEndpoint);
78              oauthParams.setTokenEndpoint(tokenEndpoint);
79              oauthParams.setRedirectUri(redirectUri);
80              oauthParams.setScope(Utils.isIssued(scope));
81              oauthParams.setApplication(app);
82  
83  
84          } catch (OAuthProblemException e) {
85              StringBuffer sb = new StringBuffer();
86              sb.append("</br>");
87              sb.append("Error code: ").append(e.getError()).append("</br>");
88              sb.append("Error description: ").append(e.getDescription()).append("</br>");
89              sb.append("Error uri: ").append(e.getUri()).append("</br>");
90              sb.append("State: ").append(e.getState()).append("</br>");
91              oauthParams.setErrorMessage(sb.toString());
92              return new ModelAndView("main");
93          }
94  
95          return new ModelAndView("request_token");
96  
97      }
98  }