Skip to content

Commit

Permalink
Add authentication logging
Browse files Browse the repository at this point in the history
  • Loading branch information
wyozi committed Dec 25, 2017
1 parent 97e8401 commit 9cdf691
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
15 changes: 11 additions & 4 deletions app/auth/Authentication.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package auth

import com.google.inject.Inject
import play.api.Configuration
import play.api.{Configuration, Logger}
import play.api.mvc._

import scala.concurrent.{ExecutionContext, Future}

class Authentication @Inject() (val config: Configuration, actionBuilder: DefaultActionBuilder) {
private lazy val password: Option[String] = config.getOptional[String]("cyan.password")
private val authLogger = Logger("cyan.authentication")

private def isAuthorized(req: RequestHeader): Boolean = req.session.get("loggedIn") match {
case Some("true") => true
Expand Down Expand Up @@ -35,9 +36,15 @@ class Authentication @Inject() (val config: Configuration, actionBuilder: Defaul
}
}
} match {
case Some(true) => action(request).map(res => withAuthorization(res))
case Some(false) => Future.successful(Results.Unauthorized.withHeaders("WWW-Authenticate" -> """Basic realm="Invalid login; try again.""""))
case _ => Future.successful(Results.Unauthorized.withHeaders("WWW-Authenticate" -> """Basic realm="Cyan login""""))
case Some(true) =>
authLogger.info(s"${request.remoteAddress} successfully authenticated at ${request.uri}")
action(request).map(res => withAuthorization(res))
case Some(false) =>
authLogger.debug(s"${request.remoteAddress} nth authentication at ${request.uri}")
Future.successful(Results.Unauthorized.withHeaders("WWW-Authenticate" -> """Basic realm="Invalid login; try again.""""))
case _ =>
authLogger.debug(s"${request.remoteAddress} first authentication at ${request.uri}")
Future.successful(Results.Unauthorized.withHeaders("WWW-Authenticate" -> """Basic realm="Cyan login""""))
}
}
}
Expand Down
1 change: 1 addition & 0 deletions conf/logback.dev.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<logger name="play" level="INFO" />
<logger name="application" level="DEBUG" />
<logger name="slick.jdbc" level="INFO"/>
<logger name="cyan.authentication" level="DEBUG"/>

<!-- Off these ones as they are annoying, and anyway we manage configuration ourselves -->
<logger name="com.avaje.ebean.config.PropertyMapLoader" level="OFF" />
Expand Down
1 change: 1 addition & 0 deletions conf/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<logger name="play" level="INFO" />
<logger name="application" level="INFO" />
<logger name="slick.jdbc" level="WARN"/>
<logger name="cyan.authentication" level="INFO"/>

<!-- Off these ones as they are annoying, and anyway we manage configuration ourselves -->
<logger name="com.avaje.ebean.config.PropertyMapLoader" level="OFF" />
Expand Down

0 comments on commit 9cdf691

Please sign in to comment.