2009-09-07 18:42:15 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009 Scalable Solutions.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package sample.secure
|
|
|
|
|
|
2009-10-17 00:37:56 +02:00
|
|
|
import se.scalablesolutions.akka.actor.{SupervisorFactory, Actor}
|
|
|
|
|
import se.scalablesolutions.akka.config.ScalaConfig._
|
|
|
|
|
import se.scalablesolutions.akka.util.Logging
|
|
|
|
|
import se.scalablesolutions.akka.security.{DigestAuthenticationActor, UserInfo}
|
|
|
|
|
import se.scalablesolutions.akka.state.TransactionalState
|
|
|
|
|
|
|
|
|
|
import javax.annotation.security.RolesAllowed
|
|
|
|
|
import javax.ws.rs.{GET, Path, Produces}
|
2009-09-07 18:42:15 +02:00
|
|
|
|
|
|
|
|
class Boot {
|
|
|
|
|
object factory extends SupervisorFactory {
|
|
|
|
|
override def getSupervisorConfig: SupervisorConfig = {
|
|
|
|
|
SupervisorConfig(
|
|
|
|
|
RestartStrategy(OneForOne, 3, 100),
|
|
|
|
|
Supervise(
|
|
|
|
|
new SimpleAuthenticationService,
|
|
|
|
|
LifeCycle(Permanent, 100)) ::
|
|
|
|
|
Supervise(
|
|
|
|
|
new SecureService,
|
|
|
|
|
LifeCycle(Permanent, 100)):: Nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val supervisor = factory.newSupervisor
|
|
|
|
|
supervisor.startSupervisor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* In akka.conf you can set the FQN of any AuthenticationActor of your wish, under the property name: akka.rest.authenticator
|
|
|
|
|
*/
|
2009-10-19 10:52:18 +02:00
|
|
|
class SimpleAuthenticationService extends DigestAuthenticationActor {
|
2009-09-07 18:42:15 +02:00
|
|
|
//If you want to have a distributed nonce-map, you can use something like below,
|
|
|
|
|
//don't forget to configure your standalone Cassandra instance
|
|
|
|
|
//
|
|
|
|
|
//makeTransactionRequired
|
|
|
|
|
//override def mkNonceMap = PersistentState.newMap(CassandraStorageConfig()).asInstanceOf[scala.collection.mutable.Map[String,Long]]
|
|
|
|
|
|
|
|
|
|
//Use an in-memory nonce-map as default
|
|
|
|
|
override def mkNonceMap = new scala.collection.mutable.HashMap[String,Long]
|
|
|
|
|
//Change this to whatever you want
|
|
|
|
|
override def realm = "test"
|
|
|
|
|
|
|
|
|
|
//Dummy method that allows you to log on with whatever username with the password "bar"
|
|
|
|
|
override def userInfo(username : String) : Option[UserInfo] = Some(UserInfo(username,"bar","ninja" :: "chef" :: Nil))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is merely a secured version of the scala-sample
|
|
|
|
|
*
|
|
|
|
|
* The interesting part is
|
|
|
|
|
* @RolesAllowed
|
|
|
|
|
* @PermitAll
|
|
|
|
|
* @DenyAll
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
@Path("/securecount")
|
|
|
|
|
class SecureService extends Actor with Logging {
|
|
|
|
|
makeTransactionRequired
|
|
|
|
|
|
|
|
|
|
case object Tick
|
|
|
|
|
private val KEY = "COUNTER";
|
|
|
|
|
private var hasStartedTicking = false;
|
2009-10-19 10:52:18 +02:00
|
|
|
private val storage = TransactionalState.newMap[String, Integer]
|
2009-09-07 18:42:15 +02:00
|
|
|
|
|
|
|
|
@GET
|
|
|
|
|
@Produces(Array("text/html"))
|
|
|
|
|
@RolesAllowed(Array("chef"))
|
|
|
|
|
def count = (this !! Tick).getOrElse(<error>Error in counter</error>)
|
|
|
|
|
|
|
|
|
|
override def receive: PartialFunction[Any, Unit] = {
|
|
|
|
|
case Tick => if (hasStartedTicking) {
|
2009-10-19 10:52:18 +02:00
|
|
|
val counter = storage.get(KEY).get.intValue
|
2009-09-07 18:42:15 +02:00
|
|
|
storage.put(KEY, new Integer(counter + 1))
|
|
|
|
|
reply(<success>Tick:{counter + 1}</success>)
|
|
|
|
|
} else {
|
|
|
|
|
storage.put(KEY, new Integer(0))
|
|
|
|
|
hasStartedTicking = true
|
|
|
|
|
reply(<success>Tick: 0</success>)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|