<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page trimDirectiveWhitespaces="true" %> <%@ page import="java.io.IOException" %> <%@ page import="java.io.PrintWriter" %> <%@ page import="javax.servlet.ServletConfig" %> <%@ page import="javax.servlet.ServletException" %> <%@ page import="javax.servlet.http.HttpServlet" %> <%@ page import="javax.servlet.http.HttpServletRequest" %> <%@ page import="javax.servlet.http.HttpServletResponse" %> <%@ page import="net.oauth.OAuth" %> <%@ page import="net.oauth.OAuthAccessor" %> <%@ page import="net.oauth.OAuthMessage" %> <%@ page import="net.oauth.example.provider.core.SampleOAuthProvider" %> <%@ page import="net.oauth.server.OAuthServlet" %> <% /* * Copyright 2007 AOL, LLC. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Autherization request handler. * * @author Praveen Alavilli */ String method = request.getMethod(); System.out.println("authorize - method: " + method); if (method == "GET") { try { OAuthMessage requestMessage = OAuthServlet.getMessage(request, null); OAuthAccessor accessor = SampleOAuthProvider.getAccessor(requestMessage); System.out.println("requestMessage: " + requestMessage); if (Boolean.TRUE.equals(accessor.getProperty("authorized"))) { // already authorized send the user back returnToConsumer(request, response, accessor); } else { sendToAuthorizePage(request, response, accessor); } } catch (Exception e){ e.printStackTrace(); SampleOAuthProvider.handleException(e, request, response, true); } } else if (method == "POST") { try { OAuthMessage requestMessage = OAuthServlet.getMessage(request, null); OAuthAccessor accessor = SampleOAuthProvider.getAccessor(requestMessage); String userId = request.getParameter("userId"); if (userId == null){ sendToAuthorizePage(request, response, accessor); } // set userId in accessor and mark it as authorized SampleOAuthProvider.markAsAuthorized(accessor, userId); returnToConsumer(request, response, accessor); } catch (Exception e){ e.printStackTrace(); SampleOAuthProvider.handleException(e, request, response, true); } } %> <%! private void sendToAuthorizePage(HttpServletRequest request, HttpServletResponse response, OAuthAccessor accessor) throws IOException, ServletException{ String callback = request.getParameter("oauth_callback"); if(callback == null || callback.length() <=0) { callback = "none"; } String consumer_description = (String)accessor.consumer.getProperty("description"); request.setAttribute("CONS_DESC", consumer_description); request.setAttribute("CALLBACK", callback); request.setAttribute("TOKEN", accessor.requestToken); request.getRequestDispatcher // ("/authorize.jsp").forward(request, response); } private void returnToConsumer(HttpServletRequest request, HttpServletResponse response, OAuthAccessor accessor) throws IOException, ServletException{ // send the user back to site's callBackUrl String callback = request.getParameter("oauth_callback"); if("none".equals(callback) && accessor.consumer.callbackURL != null && accessor.consumer.callbackURL.length() > 0){ // first check if we have something in our properties file callback = accessor.consumer.callbackURL; } if( "none".equals(callback) ) { // no call back it must be a client response.setContentType("text/plain"); PrintWriter out = response.getWriter(); out.println("You have successfully authorized '" + accessor.consumer.getProperty("description") + "'. Please close this browser window and click continue" + " in the client."); out.close(); } else { // if callback is not passed in, use the callback from config if(callback == null || callback.length() <=0 ) callback = accessor.consumer.callbackURL; String token = accessor.requestToken; if (token != null) { callback = OAuth.addParameters(callback, "oauth_token", token); } response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", callback); } } %>