Permalink
Browse files

adding oauth provider

  • Loading branch information...
1 parent ebc5a9c commit d66a2ba023d6392793231a907272bd415566bb58 Felipe Oliveira committed Apr 3, 2011
Showing with 1,602 additions and 172 deletions.
  1. +1 −1 .classpath
  2. +4 −1 .project
  3. +17 −0 app/Bootstrap.java
  4. +29 −0 app/controllers/Admin.java
  5. +11 −10 app/controllers/Authorization.java
  6. +128 −0 app/controllers/Debug.java
  7. +31 −0 app/controllers/Echo.java
  8. +1 −1 app/controllers/OAuthAccessors.java
  9. +1 −1 app/controllers/OAuthConsumers.java
  10. +1 −1 app/controllers/OAuthServiceProviders.java
  11. +31 −0 app/controllers/Security.java
  12. +5 −0 app/controllers/Users.java
  13. +5 −87 app/mashup/fm/oauth/provider/MashupOAuthProvider.java
  14. +2 −0 app/models/OAuthAccessor.java
  15. +18 −9 app/models/OAuthConsumer.java
  16. +9 −0 app/models/OAuthServiceProvider.java
  17. +43 −0 app/models/User.java
  18. +3 −0 app/views/Admin/index.html
  19. +1 −2 app/views/Application/index.html
  20. +23 −0 app/views/CRUD/layout.html
  21. +1 −0 app/views/Debug/messages.html
  22. +1 −0 app/views/Echo/echo.html
  23. +23 −0 app/views/OAuthAccessors/list.html
  24. +23 −0 app/views/OAuthConsumers/list.html
  25. +23 −0 app/views/OAuthServiceProviders/list.html
  26. +61 −0 app/views/admin.html
  27. +1 −2 conf/application.conf
  28. +13 −0 conf/initial-data.yml
  29. +25 −0 conf/messages
  30. +9 −5 conf/routes
  31. +331 −48 db/db.log
  32. +1 −1 db/db.properties
  33. +7 −3 db/db.script
  34. BIN eclipse/classes/Bootstrap.class
  35. BIN eclipse/classes/controllers/Admin.class
  36. BIN eclipse/classes/controllers/Authorization.class
  37. BIN eclipse/classes/controllers/Check.class
  38. BIN eclipse/classes/controllers/Debug.class
  39. BIN eclipse/classes/controllers/Echo.class
  40. BIN eclipse/classes/controllers/OAuthAccessors.class
  41. BIN eclipse/classes/controllers/OAuthConsumers.class
  42. BIN eclipse/classes/controllers/OAuthServiceProviders.class
  43. BIN eclipse/classes/controllers/Secure$Security.class
  44. BIN eclipse/classes/controllers/Secure.class
  45. BIN eclipse/classes/controllers/Security.class
  46. BIN eclipse/classes/controllers/Users.class
  47. BIN eclipse/classes/mashup/fm/oauth/provider/MashupOAuthProvider.class
  48. BIN eclipse/classes/models/OAuthAccessor.class
  49. BIN eclipse/classes/models/OAuthConsumer.class
  50. BIN eclipse/classes/models/OAuthServiceProvider.class
  51. BIN eclipse/classes/models/User.class
  52. +12 −0 eclipse/classes/views/Secure/layout.html
  53. +36 −0 eclipse/classes/views/Secure/login.html
  54. +3 −0 eclipse/classes/views/tags/secure/check.tag
  55. BIN public/images/favicon.png
  56. +19 −0 public/javascripts/jquery-1.3.2.min.js
  57. +20 −0 public/javascripts/jquery.tools.min.js
  58. +547 −0 public/stylesheets/main.css
  59. +82 −0 public/stylesheets/secure.css
View
@@ -2,7 +2,7 @@
<classpath>
<classpathentry excluding="views/**" kind="src" path="app"/>
<classpathentry kind="src" path="test"/>
- <classpathentry kind="src" path="crud"/>
+ <classpathentry kind="src" path="secure"/><classpathentry kind="src" path="crud"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="/Users/felipera/Downloads/play-1.1/framework/play.jar" sourcepath="/Users/felipera/Downloads/play-1.1/framework" />
<classpathentry kind="lib" path="/Users/felipera/development/playframework-oauthprovider/lib/commons-codec-1.4.jar"/>
View
@@ -15,7 +15,10 @@
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.playframework.playclipse.playNature</nature>
</natures>
- <linkedResources><link><name>crud</name><type>2</type><location>/Users/felipera/Downloads/play-1.1/modules/crud/app</location></link>
+ <linkedResources><link><name>secure</name><type>2</type><location>/Users/felipera/Downloads/play-1.1/modules/secure/app</location></link>
+<link><name>conf/secure</name><type>2</type><location>/Users/felipera/Downloads/play-1.1/modules/secure/conf</location></link>
+<link><name>public/secure</name><type>2</type><location>/Users/felipera/Downloads/play-1.1/modules/secure/public</location></link>
+<link><name>crud</name><type>2</type><location>/Users/felipera/Downloads/play-1.1/modules/crud/app</location></link>
<link><name>conf/crud</name><type>2</type><location>/Users/felipera/Downloads/play-1.1/modules/crud/conf</location></link>
<link><name>public/crud</name><type>2</type><location>/Users/felipera/Downloads/play-1.1/modules/crud/public</location></link>
</linkedResources>
View
@@ -0,0 +1,17 @@
+import play.*;
+import play.jobs.*;
+import play.test.*;
+
+import models.*;
+
+@OnApplicationStart
+public class Bootstrap extends Job {
+
+ public void doJob() {
+ // Check if the database is empty
+ if(User.count() == 0) {
+ Fixtures.load("initial-data.yml");
+ }
+ }
+
+}
@@ -0,0 +1,29 @@
+package controllers;
+
+import play.*;
+import play.mvc.*;
+import play.data.validation.*;
+
+import java.util.*;
+
+import models.*;
+
+@With(Secure.class)
+public class Admin extends CRUD {
+
+ @Before
+ static void setConnectedUser() {
+ if(Security.isConnected()) {
+ Logger.info("Connected User: %s", Security.connected());
+ User user = User.findConnectedUser(Security.connected());
+ renderArgs.put("user", user.name);
+ String userInfo = String.format("%s (%s)", user.name, user.userName);
+ renderArgs.put("userInfo", userInfo);
+ }
+ }
+
+ public static void index() {
+ render();
+ }
+
+}
@@ -31,13 +31,15 @@
import net.oauth.OAuth;
import net.oauth.OAuthMessage;
import play.mvc.Controller;
+import play.mvc.With;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
// TODO: Auto-generated Javadoc
/**
* The Class Authorization.
*/
+//@With(Secure.class)
public class Authorization extends Controller {
/**
@@ -48,7 +50,7 @@
* @param response
* the response
*/
- public static void confirm() {
+ public static void confirm() throws Throwable {
try {
OAuthMessage requestMessage = MashupOAuthUtil.getMessage(request,
@@ -78,7 +80,7 @@ public static void confirm() {
* @param response
* the response
*/
- public static void request() {
+ public static void request() throws Throwable {
try {
OAuthMessage requestMessage = MashupOAuthUtil.getMessage(request,
@@ -87,13 +89,12 @@ public static void request() {
OAuthAccessor accessor = MashupOAuthProvider
.getAccessor(requestMessage);
- // String userId = request.params.get("userId");
- // if (userId == null) {
- // sendToAuthorizePage(request, response, accessor);
- // }
+ if(!Security.isConnected()) {
+ sendToAuthorizePage(request, response, accessor);
+ }
// set userId in accessor and mark it as authorized
- accessor = MashupOAuthProvider.markAsAuthorized(accessor);
+ accessor = MashupOAuthProvider.markAsAuthorized(accessor, Security.connected());
returnToConsumer(request, response, accessor);
@@ -117,7 +118,7 @@ public static void request() {
* the servlet exception
*/
private static void sendToAuthorizePage(Request request, Response response,
- OAuthAccessor accessor) throws IOException, ServletException {
+ OAuthAccessor accessor) throws IOException, ServletException, Throwable {
String callback = request.params.get("oauth_callback");
if (callback == null || callback.length() <= 0) {
callback = "none";
@@ -126,7 +127,7 @@ private static void sendToAuthorizePage(Request request, Response response,
request.params.put("CONS_DESC", consumer_description);
request.params.put("CALLBACK", callback);
request.params.put("TOKEN", accessor.requestToken);
- Authorization.request();
+ Secure.login();
}
/**
@@ -158,7 +159,7 @@ private static void returnToConsumer(Request request, Response response,
response.setContentTypeIfNotSet(("text/plain"));
PrintWriter out = new PrintWriter(response.out);
out.println("You have successfully authorized '"
- + accessor.consumer.description
+ + accessor.consumer.name
+ "'. Please close this browser window and click continue"
+ " in the client.");
out.close();
@@ -0,0 +1,128 @@
+/**
+ * Copyright 2011 The Apache Software Foundation
+ *
+ * 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.
+ *
+ * @author Felipe Oliveira (http://mashup.fm)
+ *
+ */
+package controllers;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import play.Logger;
+import play.Play;
+import play.classloading.ApplicationClasses;
+import play.classloading.ApplicationClasses.ApplicationClass;
+import play.db.Model;
+import play.mvc.Controller;
+
+/**
+ * The Class Debug.
+ */
+public class Debug extends Controller {
+
+ /**
+ * Messages.
+ */
+ public static void messages() {
+ ApplicationClasses appClasses = Play.classes;
+ Set<String> list = new HashSet<String>();
+ List<ApplicationClass> classes = appClasses
+ .getAssignableClasses(Model.class);
+ for (ApplicationClass clazz : classes) {
+ // list.add("# Class: " + clazz.name);
+ List<Field> fields = getAllFields(clazz.javaClass);
+ if (fields != null) {
+ for (Field f : fields) {
+ list.add(f.getName() + "=" + getFieldLabel(f.getName()));
+ }
+ }
+ list.add("");
+ list.add("");
+ }
+ response.contentType = "plain/text";
+ String content = "";
+ for (String s : list) {
+ content = content + s + "\n";
+ }
+ render(content);
+ }
+
+ /**
+ * Gets the field label.
+ *
+ * @param name the name
+ * @return the field label
+ */
+ private static String getFieldLabel(String name) {
+ StringBuilder sb = new StringBuilder();
+ int count = 0;
+ for (char c : name.toCharArray()) {
+ count++;
+ if (count == 1) {
+ sb.append(String.valueOf(c).toUpperCase());
+
+ } else {
+ if (Character.isUpperCase(c)) {
+ sb.append(" ");
+ }
+ sb.append(c);
+ }
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Gets the all fields.
+ *
+ * @param originalClass the original class
+ * @return the all fields
+ */
+ private static List<Field> getAllFields(final Class<?> originalClass) {
+ Class<?> clazz = originalClass;
+
+ // Init Counter
+ int count = 0;
+
+ // Init List
+ final List<Field> fields = new ArrayList<Field>();
+
+ // Get all the fields including superclasses
+ while (clazz != null) {
+ fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
+ clazz = clazz.getSuperclass();
+ count++;
+ }
+
+ // Check Count
+ if (count > 10) {
+ Logger.warn("Too many iterations on ReflectionUtil.getAllFields() - class: "
+ + originalClass);
+ }
+
+ // make the list unmodifiable
+ List<Field> unmodifiableFields = Collections
+ .<Field> unmodifiableList(fields);
+
+ // Return List
+ return unmodifiableFields;
+ }
+
+}
@@ -0,0 +1,31 @@
+package controllers;
+
+import java.io.OutputStream;
+import java.util.Map;
+
+import mashup.fm.oauth.provider.MashupOAuthProvider;
+import mashup.fm.oauth.provider.util.MashupOAuthUtil;
+import models.OAuthAccessor;
+import net.oauth.OAuthMessage;
+import play.mvc.Controller;
+
+public class Echo extends Controller {
+
+ public static void echo() throws Exception {
+ OAuthMessage requestMessage = MashupOAuthUtil.getMessage(request, null);
+ OAuthAccessor accessor = MashupOAuthProvider.getAccessor(requestMessage);
+ MashupOAuthProvider.VALIDATOR.validateMessage(requestMessage, accessor);
+ response.contentType = "text/plain";
+ OutputStream out = response.out;
+ out.write(("[Your UserId:" + accessor.consumer.user.toString() + "]").getBytes());
+ for (Object item : request.args.entrySet()) {
+ Map.Entry parameter = (Map.Entry) item;
+ String[] values = (String[]) parameter.getValue();
+ for (String value : values) {
+ out.write((parameter.getKey() + ": " + value).getBytes());
+ }
+ }
+ out.close();
+ }
+
+}
@@ -21,6 +21,6 @@
/**
* The Class OAuthAccessors.
*/
-public class OAuthAccessors extends CRUD {
+public class OAuthAccessors extends Admin {
}
@@ -21,6 +21,6 @@
/**
* The Class OAuthConsumers.
*/
-public class OAuthConsumers extends CRUD {
+public class OAuthConsumers extends Admin {
}
@@ -21,6 +21,6 @@
/**
* The Class OAuthServiceProviders.
*/
-public class OAuthServiceProviders extends CRUD {
+public class OAuthServiceProviders extends Admin {
}
@@ -0,0 +1,31 @@
+package controllers;
+
+import org.apache.commons.lang.StringUtils;
+
+import models.User;
+import play.Logger;
+
+public class Security extends Secure.Security {
+
+ static boolean authentify(String username, String password) {
+ return User.connect(username, password) != null;
+ }
+
+ static boolean check(String profile) {
+ Logger.info("Check Profile: %s", profile);
+ if("admin".equals(profile)) {
+ return User.findConnectedUser(connected()).isAdmin;
+ }
+ return false;
+ }
+
+ static void onDisconnected() {
+ Application.index();
+ }
+
+ static void onAuthenticated() {
+ Admin.index();
+ }
+
+}
+
@@ -0,0 +1,5 @@
+package controllers;
+
+public class Users extends Admin {
+
+}
Oops, something went wrong.

0 comments on commit d66a2ba

Please sign in to comment.