Commit ecf2caae authored by Jean Aunis's avatar Jean Aunis
Browse files

Remove class TokenAuthenticatedUser, replace with a parameter

parent 345bb4d9
package controllers
import configuration.AuthConfig
import models.{TokenAuthenticatedUser, AuthenticatedUser}
import models.AuthenticatedUser
import play.api.mvc.{Action, AnyContent, Call, Request, RequestHeader, Result, Results, Security}
trait Secured {
......@@ -10,9 +10,9 @@ trait Secured {
private def username(request: RequestHeader): Option[AuthenticatedUser] = {
request.session.get("username") match {
case Some(username) => Some(new AuthenticatedUser(username))
case Some(username) => Some(AuthenticatedUser(username, request.session.get("isSuperAdmin").contains("true")))
case None => request.headers.get("X-Auth-Token") match {
case Some(value) if value == AuthConfig.token => Some(new TokenAuthenticatedUser(AuthConfig.expectedLogin))
case Some(value) if value == AuthConfig.token => Some(AuthenticatedUser(AuthConfig.expectedLogin, true))
case _ => None
}
}
......
......@@ -2,14 +2,7 @@ package models
import models.authentication.AuthenticationProvider
class AuthenticatedUser(val username: String) {
override def equals(o: Any) = o match {
case u: AuthenticatedUser => u.username == username
case _ => false
}
}
class TokenAuthenticatedUser(override val username: String) extends AuthenticatedUser(username)
case class AuthenticatedUser(username: String, superAdmin: Boolean=false)
object AuthenticatedUser {
def authenticate(username: String, password: String): Option[AuthenticatedUser] = AuthenticationProvider.authenticate(username, password)
......
......@@ -19,7 +19,7 @@ class LdapDriver extends AuthenticationDriver {
val loginDN = entries.getSearchEntries.get(0).getDN
try {
new LDAPConnection(ldapUri.getHost, ldapUri.getPort, loginDN, password)
Some(new AuthenticatedUser(login))
Some(AuthenticatedUser(login))
} catch {
case e: LDAPException => logger.warn(s"Login failed for user $login")
None
......
......@@ -11,7 +11,7 @@ class LoginDriver extends AuthenticationDriver {
println("password: " + AuthConfig.expectedPassword)
if (login.equals(AuthConfig.expectedLogin) && password.equals(AuthConfig.expectedPassword)) {
logger.info(s"Login successful for $login")
Some(new AuthenticatedUser(login))
Some(AuthenticatedUser(login))
}
else {
logger.warn(s"Echec d'authentification pour l'utilisateur $login")
......
......@@ -22,7 +22,7 @@ protected class RealXivoDriver(val connector: RestapiConnector) extends Authenti
for (user <- jsonUsers.value)
if ((user \ "username").asOpt[String].contains(login) && (user \ "password").asOpt[String].contains(password)) {
logger.info(s"Login successful for $login")
return Some(new AuthenticatedUser(login))
return Some(AuthenticatedUser(login))
}
logger.warn(s"Failed to authenticate user $login")
None
......
......@@ -2,7 +2,7 @@ organization := "xivo"
name := "play-authentication"
version := "2.1"
version := "2.2"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
......
......@@ -54,7 +54,7 @@ class AuthenticationProviderSpec extends PlaySpecification with Mockito {
}
"should stop after calling first driver which send back a user" in {
authSecondDriver.authenticate("username","password") returns Some(new AuthenticatedUser("username"))
authSecondDriver.authenticate("username","password") returns Some(AuthenticatedUser("username"))
running(FakeApplication(additionalConfiguration = AuthentConfig)) {
TestAllDrivers.authenticate("username", "password")
......
......@@ -33,8 +33,8 @@ class LdapDriverSpec extends PlaySpecification {
"return a User if the login and the password are correct" in {
running(FakeApplication(additionalConfiguration = TestConfig.testConfig)) {
val driver = new LdapDriver()
driver.authenticate("j.doe@test.com", "nobody") shouldEqual Some(new AuthenticatedUser("j.doe@test.com"))
driver.authenticate("b.danny@test.com", "nobody") shouldEqual Some(new AuthenticatedUser("b.danny@test.com"))
driver.authenticate("j.doe@test.com", "nobody") shouldEqual Some(AuthenticatedUser("j.doe@test.com"))
driver.authenticate("b.danny@test.com", "nobody") shouldEqual Some(AuthenticatedUser("b.danny@test.com"))
}
}
......
......@@ -10,7 +10,7 @@ class LoginDriverSpec extends PlaySpecification {
"return a User if the login and the password are correct" in {
running(FakeApplication(additionalConfiguration = TestConfig.testConfig)) {
val driver = new LoginDriver()
driver.authenticate("john", "doe") shouldEqual Some(new AuthenticatedUser("john"))
driver.authenticate("john", "doe") shouldEqual Some(AuthenticatedUser("john"))
}
}
......
......@@ -15,7 +15,7 @@ class XivoDriverSpec extends PlaySpecification with Mockito {
val driver = new RealXivoDriver(connector)
connector.getUsers() returns Json.toJson(List(Map("username" -> "john", "password" -> "doe"),
Map("username" -> "pierre", "password" -> "dupond"))).asInstanceOf[JsArray]
driver.authenticate("john", "doe") shouldEqual Some(new AuthenticatedUser("john"))
driver.authenticate("john", "doe") shouldEqual Some(AuthenticatedUser("john"))
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment