Commits

Joeri Sykora committed 31f6d0d

handle creation of service consumer and authorize/oauth

  • Participants
  • Parent commits 845a505
  • Branches jsf

Comments (0)

Files changed (8)

File oauthprovider/src/main/java/com/mycompany/oauthprovider/Authentication.java

-package com.mycompany.oauthprovider;
-
-import com.lodgon.dali.core.ejb.DaliCoreException;
-import com.lodgon.dali.core.ejb.UserBean;
-import com.lodgon.dali.core.entity.User;
-import com.lodgon.dali.core.oauth.ejb.OAuthBean;
-import com.lodgon.dali.core.oauth.entity.DaliToken;
-import com.lodgon.dali.core.oauth.entity.OAuthUser;
-import javax.faces.bean.ManagedBean;
-import javax.faces.bean.ManagedProperty;
-import javax.faces.bean.RequestScoped;
-import javax.inject.Inject;
-
-/**
- *
- * @author joeri
- */
-@ManagedBean
-@RequestScoped
-public class Authentication {
-	@Inject
-	OAuthBean oauthBean;
-	@Inject
-	UserBean userBean;
-
-	private String oauthToken;
-	private String username;
-	private String password;
-
-	@ManagedProperty(value = "#{authorization}")
-	private Authorization authorization;
-
-	public Authorization getAuthorization() {
-		return authorization;
-	}
-
-	public void setAuthorization(Authorization authorization) {
-		this.authorization = authorization;
-	}
-
-	/**
-	 * Creates a new instance of AuthenticationForm
-	 */
-	public Authentication() {
-	}
-
-	public String getOauthToken() {
-		return oauthToken;
-	}
-
-	public void setOauthToken(String oauthToken) {
-		this.oauthToken = oauthToken;
-	}
-
-	public String getUsername() {
-		return username;
-	}
-
-	public void setUsername(String username) {
-		this.username = username;
-	}
-
-	public String getPassword() {
-		return password;
-	}
-
-	public void setPassword(String password) {
-		this.password = password;
-	}
-
-	public String login() throws DaliCoreException {
-		User user = userBean.validateScreenNameAndPassword(username, password);
-		if (user == null) {
-			user = new OAuthUser();
-			user.setScreenName(username);
-			user = userBean.create(user);
-			userBean.setPassword(user.getId(), password);
-		}
-
-		DaliToken requestToken = oauthBean.findDaliToken(oauthToken, DaliToken.Type.OAUTH_REQUEST_TOKEN);
-
-		authorization.setOauthToken(oauthToken);
-		authorization.setUserUid(user.getUid());
-		authorization.setConsumer(requestToken.getDaliServiceConsumer());
-
-		return "/pages/authorize";
-	}
-}

File oauthprovider/src/main/java/com/mycompany/oauthprovider/Authorization.java

-package com.mycompany.oauthprovider;
-
-import com.lodgon.dali.core.ejb.DaliCoreException;
-import com.lodgon.dali.core.ejb.UserBean;
-import com.lodgon.dali.core.entity.User;
-import com.lodgon.dali.core.oauth.ejb.OAuthBean;
-import com.lodgon.dali.core.oauth.entity.DaliServiceConsumer;
-import com.lodgon.dali.core.oauth.entity.DaliToken;
-import com.sun.jersey.oauth.signature.OAuthParameters;
-import java.io.IOException;
-import java.net.URLEncoder;
-import javax.faces.bean.ManagedBean;
-import javax.faces.bean.SessionScoped;
-import javax.faces.context.FacesContext;
-import javax.inject.Inject;
-
-/**
- *
- * @author joeri
- */
-@ManagedBean
-@SessionScoped
-public class Authorization {
-	@Inject
-	OAuthBean oauthBean;
-	@Inject
-	UserBean userBean;
-
-	private String verifier;
-	private String message;
-	private String oauthToken;
-	private String userUid;
-	private DaliServiceConsumer consumer;
-
-	public String getVerifier() {
-		return verifier;
-	}
-
-	public void setVerifier(String verifier) {
-		this.verifier = verifier;
-	}
-
-	public String getMessage() {
-		return message;
-	}
-
-	public void setMessage(String message) {
-		this.message = message;
-	}
-
-	public String getOauthToken() {
-		return oauthToken;
-	}
-
-	public void setOauthToken(String oauthToken) {
-		this.oauthToken = oauthToken;
-	}
-
-	public String getUserUid() {
-		return userUid;
-	}
-
-	public void setUserUid(String userUid) {
-		this.userUid = userUid;
-	}
-
-	public DaliServiceConsumer getConsumer() {
-		return consumer;
-	}
-
-	public void setConsumer(DaliServiceConsumer consumer) {
-		this.consumer = consumer;
-	}
-
-	public String allow() throws DaliCoreException, IOException {
-		DaliToken requestToken = oauthBean.findDaliToken(oauthToken, DaliToken.Type.OAUTH_REQUEST_TOKEN);
-
-		User user = userBean.getByUid(userUid);
-		if (user != null) {
-			this.verifier = oauthBean.authorizeServiceConsumer(user.getId(), oauthToken, true);
-			if ("oob".equals(requestToken.getCallback())) {
-				return "/pages/verifier";
-			} else {
-				FacesContext.getCurrentInstance().getExternalContext().redirect(requestToken.getCallback() + "?" + OAuthParameters.TOKEN + "=" + URLEncoder.encode(oauthToken, "UTF-8") + "&" + OAuthParameters.VERIFIER + "=" + URLEncoder.encode(verifier, "UTF-8"));
-			}
-		} else {
-			return "/pages/login";
-		}
-
-		return null;
-	}
-
-	public String deny() throws IOException {
-		DaliToken requestToken = oauthBean.findDaliToken(oauthToken, DaliToken.Type.OAUTH_REQUEST_TOKEN);
-
-		if ("oob".equals(requestToken.getCallback())) {
-			this.verifier = "Denied.";
-			return "/pages/verifier";
-		} else {
-			FacesContext.getCurrentInstance().getExternalContext().redirect(requestToken.getCallback());
-		}
-
-		return null;
-	}
-}

File oauthprovider/src/main/java/com/mycompany/oauthprovider/ServiceConsumerHandler.java

-package com.mycompany.oauthprovider;
-
-import com.lodgon.dali.core.oauth.ejb.OAuthBean;
-import com.lodgon.dali.core.oauth.entity.DaliServiceConsumer;
-import com.lodgon.dali.core.oauth.util.StringUtil;
-import javax.annotation.ManagedBean;
-import javax.inject.Inject;
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-
-@Path("serviceconsumer")
-@ManagedBean
-public class ServiceConsumerHandler {
-	@Inject
-	OAuthBean oauthBean;
-
-	@GET
-	@Produces("text/html")
-	public String create(@QueryParam("name") String name) {
-		if (name != null && ! name.trim().isEmpty()) {
-			DaliServiceConsumer daliServiceConsumer = new DaliServiceConsumer();
-			daliServiceConsumer.setName(name);
-			daliServiceConsumer.setConsumerKey(StringUtil.getSecureRandomString(16));
-			daliServiceConsumer.setConsumerSecret(StringUtil.getSecureRandomString(16));
-			daliServiceConsumer.setStatus(DaliServiceConsumer.Status.ACTIVE);
-			oauthBean.createDaliServiceConsumer(daliServiceConsumer);
-
-			return "consumer key: <code>" + daliServiceConsumer.getKey() + "</code><br/>consumer secret: <code>" + daliServiceConsumer.getSecret() + "</code>";
-		}
-
-		return "No name provided. Add ?name=service_consumer_name at the end of the URL.";
-	}
-}

File oauthprovider/src/main/java/com/mycompany/oauthprovider/servlet/OAuthServlet.java

+package com.mycompany.oauthprovider.servlet;
+
+import java.io.IOException;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ *
+ * @author joeri
+ */
+@WebServlet(name = "OAuthServlet", urlPatterns = {"/oauth"})
+public class OAuthServlet extends HttpServlet {
+
+	/**
+	 * Processes requests for both HTTP
+	 * <code>GET</code> and
+	 * <code>POST</code> methods.
+	 *
+	 * @param request servlet request
+	 * @param response servlet response
+	 * @throws ServletException if a servlet-specific error occurs
+	 * @throws IOException if an I/O error occurs
+	 */
+	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
+					throws ServletException, IOException {
+		String oauthToken = request.getParameter("oauth_token");
+
+		request.getRequestDispatcher("/pages/login.faces?oauth_token=" + oauthToken).forward(request, response);
+	}
+
+	// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
+	/**
+	 * Handles the HTTP
+	 * <code>GET</code> method.
+	 *
+	 * @param request servlet request
+	 * @param response servlet response
+	 * @throws ServletException if a servlet-specific error occurs
+	 * @throws IOException if an I/O error occurs
+	 */
+	@Override
+	protected void doGet(HttpServletRequest request, HttpServletResponse response)
+					throws ServletException, IOException {
+		processRequest(request, response);
+	}
+
+	/**
+	 * Handles the HTTP
+	 * <code>POST</code> method.
+	 *
+	 * @param request servlet request
+	 * @param response servlet response
+	 * @throws ServletException if a servlet-specific error occurs
+	 * @throws IOException if an I/O error occurs
+	 */
+	@Override
+	protected void doPost(HttpServletRequest request, HttpServletResponse response)
+					throws ServletException, IOException {
+		processRequest(request, response);
+	}
+
+	/**
+	 * Returns a short description of the servlet.
+	 *
+	 * @return a String containing servlet description
+	 */
+	@Override
+	public String getServletInfo() {
+		return "Short description";
+	}// </editor-fold>
+}

File oauthprovider/src/main/java/com/mycompany/oauthprovider/servlet/ServiceConsumerServlet.java

+package com.mycompany.oauthprovider.servlet;
+
+import com.lodgon.dali.core.oauth.ejb.OAuthBean;
+import com.lodgon.dali.core.oauth.entity.DaliServiceConsumer;
+import com.lodgon.dali.core.oauth.util.StringUtil;
+import java.io.IOException;
+import java.io.PrintWriter;
+import javax.inject.Inject;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ *
+ * @author joeri
+ */
+@WebServlet(name = "ServiceConsumerServlet", urlPatterns = {"/serviceconsumer"})
+public class ServiceConsumerServlet extends HttpServlet {
+	@Inject
+	OAuthBean oauthBean;
+
+	/**
+	 * Processes requests for both HTTP
+	 * <code>GET</code> and
+	 * <code>POST</code> methods.
+	 *
+	 * @param request servlet request
+	 * @param response servlet response
+	 * @throws ServletException if a servlet-specific error occurs
+	 * @throws IOException if an I/O error occurs
+	 */
+	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
+					throws ServletException, IOException {
+		response.setContentType("text/html;charset=UTF-8");
+
+		PrintWriter out = response.getWriter();
+
+		try {
+			out.println("<html>");
+			out.println("<head>");
+			out.println("<title>dalicore-oauth service provider</title>");
+			out.println("</head>");
+			out.println("<body>");
+
+			out.println("<h4>Create Service Consumer</h4>");
+
+			String name = request.getParameter("name");
+			if (name != null && ! name.trim().isEmpty()) {
+				DaliServiceConsumer daliServiceConsumer = new DaliServiceConsumer();
+				daliServiceConsumer.setName(name);
+				daliServiceConsumer.setConsumerKey(StringUtil.getSecureRandomString(16));
+				daliServiceConsumer.setConsumerSecret(StringUtil.getSecureRandomString(16));
+				daliServiceConsumer.setStatus(DaliServiceConsumer.Status.ACTIVE);
+				oauthBean.createDaliServiceConsumer(daliServiceConsumer);
+
+				out.println("<p>Service Consumer by name " + name + " successfully created.</p>");
+				out.println("<ul>");
+				out.println("<li>Consumer Key: <code>" + daliServiceConsumer.getKey() + "</code></li>");
+				out.println("<li>Consumer Secret: <code>" + daliServiceConsumer.getSecret() + "</code></li>");
+				out.println("</ul>");
+			} else {
+				out.println("<p>No name provided. Add <code>?name=service_consumer_name</code> at the end of the URL.</p>");
+			}
+
+			out.println("</body>");
+			out.println("</html>");
+		} finally {			
+			out.close();
+		}
+	}
+
+	// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
+	/**
+	 * Handles the HTTP
+	 * <code>GET</code> method.
+	 *
+	 * @param request servlet request
+	 * @param response servlet response
+	 * @throws ServletException if a servlet-specific error occurs
+	 * @throws IOException if an I/O error occurs
+	 */
+	@Override
+	protected void doGet(HttpServletRequest request, HttpServletResponse response)
+					throws ServletException, IOException {
+		processRequest(request, response);
+	}
+
+	/**
+	 * Handles the HTTP
+	 * <code>POST</code> method.
+	 *
+	 * @param request servlet request
+	 * @param response servlet response
+	 * @throws ServletException if a servlet-specific error occurs
+	 * @throws IOException if an I/O error occurs
+	 */
+	@Override
+	protected void doPost(HttpServletRequest request, HttpServletResponse response)
+					throws ServletException, IOException {
+		processRequest(request, response);
+	}
+
+	/**
+	 * Returns a short description of the servlet.
+	 *
+	 * @return a String containing servlet description
+	 */
+	@Override
+	public String getServletInfo() {
+		return "Short description";
+	}// </editor-fold>
+}

File oauthprovider/src/main/java/com/mycompany/oauthprovider/view/Authentication.java

+package com.mycompany.oauthprovider.view;
+
+import com.lodgon.dali.core.ejb.DaliCoreException;
+import com.lodgon.dali.core.ejb.UserBean;
+import com.lodgon.dali.core.entity.User;
+import com.lodgon.dali.core.oauth.ejb.OAuthBean;
+import com.lodgon.dali.core.oauth.entity.DaliToken;
+import com.lodgon.dali.core.oauth.entity.OAuthUser;
+import javax.faces.bean.ManagedBean;
+import javax.faces.bean.ManagedProperty;
+import javax.faces.bean.RequestScoped;
+import javax.inject.Inject;
+
+/**
+ *
+ * @author joeri
+ */
+@ManagedBean
+@RequestScoped
+public class Authentication {
+	@Inject
+	OAuthBean oauthBean;
+	@Inject
+	UserBean userBean;
+
+	@ManagedProperty(value = "#{param.oauth_token}")
+	private String oauthToken;
+	private String username;
+	private String password;
+
+	@ManagedProperty(value = "#{authorization}")
+	private Authorization authorization;
+
+	public Authorization getAuthorization() {
+		return authorization;
+	}
+
+	public void setAuthorization(Authorization authorization) {
+		this.authorization = authorization;
+	}
+
+	/**
+	 * Creates a new instance of AuthenticationForm
+	 */
+	public Authentication() {
+	}
+
+	public String getOauthToken() {
+		return oauthToken;
+	}
+
+	public void setOauthToken(String oauthToken) {
+		this.oauthToken = oauthToken;
+	}
+
+	public String getUsername() {
+		return username;
+	}
+
+	public void setUsername(String username) {
+		this.username = username;
+	}
+
+	public String getPassword() {
+		return password;
+	}
+
+	public void setPassword(String password) {
+		this.password = password;
+	}
+
+	public String login() throws DaliCoreException {
+		System.out.println("oauthToken = " + oauthToken);
+
+		User user = userBean.validateScreenNameAndPassword(username, password);
+		if (user == null) {
+			user = new OAuthUser();
+			user.setScreenName(username);
+			user = userBean.create(user);
+			userBean.setPassword(user.getId(), password);
+		}
+
+		DaliToken requestToken = oauthBean.findDaliToken(oauthToken, DaliToken.Type.OAUTH_REQUEST_TOKEN);
+
+		authorization.setOauthToken(oauthToken);
+		authorization.setUserUid(user.getUid());
+		authorization.setConsumer(requestToken.getDaliServiceConsumer());
+
+		return "/pages/authorize";
+	}
+}

File oauthprovider/src/main/java/com/mycompany/oauthprovider/view/Authorization.java

+package com.mycompany.oauthprovider.view;
+
+import com.lodgon.dali.core.ejb.DaliCoreException;
+import com.lodgon.dali.core.ejb.UserBean;
+import com.lodgon.dali.core.entity.User;
+import com.lodgon.dali.core.oauth.ejb.OAuthBean;
+import com.lodgon.dali.core.oauth.entity.DaliServiceConsumer;
+import com.lodgon.dali.core.oauth.entity.DaliToken;
+import com.sun.jersey.oauth.signature.OAuthParameters;
+import java.io.IOException;
+import java.net.URLEncoder;
+import javax.faces.bean.ManagedBean;
+import javax.faces.bean.SessionScoped;
+import javax.faces.context.FacesContext;
+import javax.inject.Inject;
+
+/**
+ *
+ * @author joeri
+ */
+@ManagedBean
+@SessionScoped
+public class Authorization {
+	@Inject
+	OAuthBean oauthBean;
+	@Inject
+	UserBean userBean;
+
+	private String verifier;
+	private String message;
+	private String oauthToken;
+	private String userUid;
+	private DaliServiceConsumer consumer;
+
+	public String getVerifier() {
+		return verifier;
+	}
+
+	public void setVerifier(String verifier) {
+		this.verifier = verifier;
+	}
+
+	public String getMessage() {
+		return message;
+	}
+
+	public void setMessage(String message) {
+		this.message = message;
+	}
+
+	public String getOauthToken() {
+		return oauthToken;
+	}
+
+	public void setOauthToken(String oauthToken) {
+		this.oauthToken = oauthToken;
+	}
+
+	public String getUserUid() {
+		return userUid;
+	}
+
+	public void setUserUid(String userUid) {
+		this.userUid = userUid;
+	}
+
+	public DaliServiceConsumer getConsumer() {
+		return consumer;
+	}
+
+	public void setConsumer(DaliServiceConsumer consumer) {
+		this.consumer = consumer;
+	}
+
+	public String allow() throws DaliCoreException, IOException {
+		DaliToken requestToken = oauthBean.findDaliToken(oauthToken, DaliToken.Type.OAUTH_REQUEST_TOKEN);
+
+		User user = userBean.getByUid(userUid);
+		if (user != null) {
+			this.verifier = oauthBean.authorizeServiceConsumer(user.getId(), oauthToken, true);
+			if ("oob".equals(requestToken.getCallback())) {
+				return "/pages/verifier";
+			} else {
+				FacesContext.getCurrentInstance().getExternalContext().redirect(requestToken.getCallback() + "?" + OAuthParameters.TOKEN + "=" + URLEncoder.encode(oauthToken, "UTF-8") + "&" + OAuthParameters.VERIFIER + "=" + URLEncoder.encode(verifier, "UTF-8"));
+			}
+		} else {
+			return "/pages/login";
+		}
+
+		return null;
+	}
+
+	public String deny() throws IOException {
+		DaliToken requestToken = oauthBean.findDaliToken(oauthToken, DaliToken.Type.OAUTH_REQUEST_TOKEN);
+
+		if ("oob".equals(requestToken.getCallback())) {
+			this.verifier = "Denied.";
+			return "/pages/verifier";
+		} else {
+			FacesContext.getCurrentInstance().getExternalContext().redirect(requestToken.getCallback());
+		}
+
+		return null;
+	}
+}

File oauthprovider/src/main/webapp/pages/authorize.xhtml

 		</ui:composition>
 	</h:body>
 </html>
-