Author: chirino
Date: Thu Aug 25 00:08:50 2011
New Revision: 1161330
URL: http://svn.apache.org/viewvc?rev=1161330&view=rev
Log:
Added cookie based session login/logout support to the web console.
Modified:
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/BrokerResource.scala
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/Support.scala
Modified:
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/BrokerResource.scala
URL:
http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/BrokerResource.scala?rev=1161330&r1=1161329&r2=1161330&view=diff
==============================================================================
---
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/BrokerResource.scala
(original)
+++
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/BrokerResource.scala
Thu Aug 25 00:08:50 2011
@@ -21,24 +21,18 @@ import java.{lang => jl}
import org.fusesource.hawtdispatch._
import org.apache.activemq.apollo.broker._
import scala.collection.Iterable
-import scala.Some
-import security.{SecurityContext, Authorizer}
import org.apache.activemq.apollo.util.path.PathParser
-import org.apache.activemq.apollo.web.resources.Resource._
import org.apache.activemq.apollo.util._
-import collection.mutable.ListBuffer
import javax.ws.rs._
-import core.Response
-import Response.Status._
-import org.josql.expressions.SelectItemExpression
-import org.apache.activemq.apollo.util.BaseService._
+import javax.ws.rs.core.Context
+import javax.ws.rs.core.Response.Status._
import management.ManagementFactory
import javax.management.ObjectName
import javax.management.openmbean.CompositeData
-import javax.management.remote.rmi._RMIConnection_Stub
import org.josql.{QueryResults, Query}
-import java.util.Collections
import java.util.regex.Pattern
+import javax.servlet.http.HttpServletResponse
+import java.util.ArrayList
/**
* <p>
@@ -51,6 +45,62 @@ import java.util.regex.Pattern
@Produces(Array("application/json", "application/xml","text/xml",
"text/html;qs=5"))
case class BrokerResource() extends Resource {
+ @GET
+ @Path("whoami")
+ def whoami():java.util.List[PrincipalDTO] = {
+ val rc: Set[PrincipalDTO] = with_broker { broker =>
+ val rc = FutureResult[Set[PrincipalDTO]]()
+ if(broker.authenticator!=null) {
+ authenticate(broker.authenticator) { security_context =>
+ if(security_context!=null) {
+ rc.set(Success(security_context.principles))
+ } else {
+ rc.set(Success(Set[PrincipalDTO]()))
+ }
+ }
+ } else {
+ rc.set(Success(Set[PrincipalDTO]()))
+ }
+ rc
+ }
+ new
ArrayList[PrincipalDTO](collection.JavaConversions.asJavaCollection(rc))
+ }
+
+ @GET
+ @Path("signin")
+ def get_signin(@Context response:HttpServletResponse,
@QueryParam("username") username:String, @QueryParam("password")
password:String):Boolean = {
+ post_signin(response, username, password)
+ }
+
+ @POST
+ @Path("signin")
+ def post_signin(@Context response:HttpServletResponse,
@FormParam("username") username:String, @FormParam("password")
password:String):Boolean = {
+ val session = http_request.getSession(true)
+ session.setAttribute("username", username);
+ session.setAttribute("password", password);
+ try {
+ unwrap_future_result[Boolean] {
+ with_broker { broker =>
+ monitoring(broker) {
+ true
+ }
+ }
+ }
+ } catch {
+ case e:WebApplicationException => // this happens if user is not
authorized
+ false
+ }
+ }
+
+ @GET
+ @Path("signout")
+ def signout():Unit = {
+ val session = http_request.getSession(false)
+ if( session !=null ) {
+ session.invalidate();
+ }
+ }
+
@Path("config")
def config_resource:ConfigurationResource = {
with_broker { broker =>
@@ -60,6 +110,7 @@ case class BrokerResource() extends Reso
}
}
+
@GET
def get_broker():BrokerStatusDTO = {
with_broker { broker =>
Modified:
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/Support.scala
URL:
http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/Support.scala?rev=1161330&r1=1161329&r2=1161330&view=diff
==============================================================================
---
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/Support.scala
(original)
+++
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/Support.scala
Thu Aug 25 00:08:50 2011
@@ -96,7 +96,7 @@ abstract class Resource(parent:Resource=
}
- protected def authorize[T](authenticator:Authenticator,
authorizer:Authorizer, block: =>FutureResult[T])(func: (Authorizer,
SecurityContext)=>Boolean):FutureResult[T] = {
+ def authorize[T](authenticator:Authenticator, authorizer:Authorizer, block:
=>FutureResult[T])(func: (Authorizer,
SecurityContext)=>Boolean):FutureResult[T] = {
if ( authenticator != null ) {
val rc = FutureResult[T]()
authenticate(authenticator) { security_context =>
@@ -169,19 +169,24 @@ abstract class Resource(parent:Resource=
security_context.remote_address = new
InetSocketAddress(http_request.getRemoteAddr, http_request.getRemotePort)
security_context.certificates =
http_request.getAttribute("javax.servlet.request.X509Certificate").asInstanceOf[Array[X509Certificate]]
- var auth_header = http_request.getHeader(HEADER_AUTHORIZATION)
- if (auth_header != null && auth_header.length > 0) {
- auth_header = auth_header.trim
- var blank = auth_header.indexOf(' ')
- if (blank > 0) {
- var auth_type = auth_header.substring(0, blank)
- var auth_info = auth_header.substring(blank).trim
- if (auth_type.equalsIgnoreCase(AUTHENTICATION_SCHEME_BASIC)) {
- try {
- var srcString = decode_base64(auth_info)
- var i = srcString.indexOf(':')
- var username: String = srcString.substring(0, i)
- var password: String = srcString.substring(i + 1)
+ val session = http_request.getSession(false)
+ if( session !=null ) {
+ security_context.user =
session.getAttribute("username").asInstanceOf[String];
+ security_context.password =
session.getAttribute("password").asInstanceOf[String];
+ } else {
+ var auth_header = http_request.getHeader(HEADER_AUTHORIZATION)
+ if (auth_header != null && auth_header.length > 0) {
+ auth_header = auth_header.trim
+ var blank = auth_header.indexOf(' ')
+ if (blank > 0) {
+ var auth_type = auth_header.substring(0, blank)
+ var auth_info = auth_header.substring(blank).trim
+ if (auth_type.equalsIgnoreCase(AUTHENTICATION_SCHEME_BASIC)) {
+ try {
+ var srcString = decode_base64(auth_info)
+ var i = srcString.indexOf(':')
+ var username: String = srcString.substring(0, i)
+ var password: String = srcString.substring(i + 1)
// connection.transport match {
@@ -189,16 +194,16 @@ abstract class Resource(parent:Resource=
// security_context.certificates =
Option(t.getPeerX509Certificates).getOrElse(Array[X509Certificate]())
// case _ => None
// }
- security_context.user = username
- security_context.password = password
+ security_context.user = username
+ security_context.password = password
- } catch {
- case e: Exception =>
+ } catch {
+ case e: Exception =>
+ }
}
}
}
}
-
reset {
if( authenticator.authenticate(security_context) ) {
http_request.setAttribute(SECURITY_CONTEXT_ATTRIBUTE,
security_context)
@@ -211,12 +216,13 @@ abstract class Resource(parent:Resource=
}
protected def unauthroized = {
- // TODO: perhaps get the realm from the authenticator
- var http_realm = "Apollo"
- throw new WebApplicationException(Response.
- status(HttpServletResponse.SC_UNAUTHORIZED).
- header(HEADER_WWW_AUTHENTICATE, AUTHENTICATION_SCHEME_BASIC + "
realm=\"" + http_realm + "\"").
- build())
+ val response = Response.status(HttpServletResponse.SC_UNAUTHORIZED)
+ if( http_request.getHeader("AuthPrompt")!="false" &&
http_request.getSession(false)==null ) {
+ // TODO: perhaps get the realm from the authenticator
+ var http_realm = "Apollo"
+ response.header(HEADER_WWW_AUTHENTICATE, AUTHENTICATION_SCHEME_BASIC + "
realm=\"" + http_realm + "\"")
+ }
+ throw new WebApplicationException(response.build())
}
type FutureResult[T] = Future[Result[T, Throwable]]