Skip to content
Browse files

Choose your authentication method

  • Loading branch information...
1 parent 423c707 commit 61f1510427b6e611a05148cad44946ce51f0149e @jfp committed Dec 10, 2011
View
5 app/controllers/modules/search/Administration.java
@@ -6,21 +6,20 @@
import play.libs.Codec;
import play.modules.search.Search;
import play.modules.search.store.ManagedIndex;
-import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.Http;
+import play.mvc.With;
+@With(Authentication.class)
public class Administration extends Controller {
- @Before
protected static void check () {
Http.Header auth = request.headers.get("authorization");
if(auth != null) {
String encodedPassword = auth.value().substring("Basic ".length());
String password = new String(Codec.decodeBASE64(encodedPassword));
String user = password.substring(0, password.indexOf(':'));
String pwd = password.substring((user + ":").length());
- System.out.println("user is " + user + " , passwd is " + pwd);
if (! pwd.equals(Play.configuration.getProperty("play.search.password","search")))
unauthorized("You are not authorized");
} else {
View
54 app/controllers/modules/search/Authentication.java
@@ -0,0 +1,54 @@
+package controllers.modules.search;
+
+import play.Play;
+import play.libs.Codec;
+import play.mvc.Before;
+import play.mvc.Controller;
+import play.mvc.Http;
+
+
+public class Authentication extends Controller {
+
+ @Before(unless={"showAuth","checkAuth"})
+ protected static void check () {
+ if (Play.configuration.getProperty("play.search.auth.method", "http").equals("session")) {
+ checkSession ();
+ } else {
+ checkHttp ();
+ }
+ }
+
+ protected static void checkSession () {
+ if (!session.contains("play.search.login")) {
+ showAuth();
+ }
+ }
+
+ public static void showAuth () {
+ render();
+ }
+
+ public static void checkAuth (String login, String password) {
+ String confLogin = Play.configuration.getProperty("play.search.login","search");
+ String confPassword = Play.configuration.getProperty("play.search.password","search");
+ if (confLogin.equals(login) && confPassword.equals(password)) {
+ session.put("play.search.login", login);
+ Administration.index();
+ }
+ showAuth();
+ }
+
+ protected static void checkHttp () {
+ Http.Header auth = request.headers.get("authorization");
+ if(auth != null) {
+ String encodedPassword = auth.value().substring("Basic ".length());
+ String password = new String(Codec.decodeBASE64(encodedPassword));
+ String user = password.substring(0, password.indexOf(':'));
+ String pwd = password.substring((user + ":").length());
+ if (! pwd.equals(Play.configuration.getProperty("play.search.password","search")))
+ unauthorized("You are not authorized");
+ } else {
+ unauthorized("You are not authorized");
+ }
+ }
+}
View
17 app/views/modules/search/Authentication/showAuth.html
@@ -0,0 +1,17 @@
+#{extends 'modules/search/layout.html'/}
+
+<h1>Please provide your credentials :</h1>
+<form action="@@{checkAuth}" method="post">
+<dl>
+ <dt>Login:</dt>
+ <dd><input type="text" name="login" />
+</dl>
+<dl>
+ <dt>Password:</dt>
+ <dd><input type="password" name="password" />
+</dl>
+<dl>
+ <dt></dt>
+ <dd><input type="submit" />
+</dl>
+</form>

0 comments on commit 61f1510

Please sign in to comment.
Something went wrong with that request. Please try again.