Skip to content
This repository has been archived by the owner on Sep 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #116 from akkie/master
Browse files Browse the repository at this point in the history
Improve logging behaviour
  • Loading branch information
akkie committed Mar 1, 2014
2 parents 2caaea6 + fa27559 commit 121b2df
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
*/
package com.mohiva.play.silhouette.core

import play.api.Logger

/**
* An exception thrown when the user denies access to the application
* in the login page of the 3rd party service.
*
* @param msg The exception message.
* @param cause The exception cause.
*/
case class AccessDeniedException(msg: String, cause: Throwable) extends Exception(msg, cause) {
Logger.info(msg)
case class AccessDeniedException(msg: String, cause: Throwable) extends Exception(msg, cause) with Logger {
logger.info(msg)

/**
* Constructs an exception with only a message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@
*/
package com.mohiva.play.silhouette.core

import play.api.Logger

/**
* An exception thrown when there is an error in the authentication flow.
*
* @param msg The exception message.
* @param cause The exception cause.
*/
case class AuthenticationException(msg: String, cause: Throwable) extends Exception(msg, cause) {
Logger.error(msg, cause)
case class AuthenticationException(msg: String, cause: Throwable) extends Exception(msg, cause) with Logger {
logger.error(msg, cause)

/**
* Constructs an exception with only a message.
Expand Down
27 changes: 27 additions & 0 deletions app/com/mohiva/play/silhouette/core/Logger.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright 2014 Mohiva Organisation (license at mohiva dot com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mohiva.play.silhouette.core

/**
* Implement this to get a named logger in scope.
*/
trait Logger {

/**
* A named logger instance.
*/
val logger = play.api.Logger(this.getClass)
}
15 changes: 5 additions & 10 deletions app/com/mohiva/play/silhouette/core/Silhouette.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
package com.mohiva.play.silhouette.core

import play.api.{ Play, Logger }
import play.api.Play
import play.api.mvc._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
Expand All @@ -45,7 +45,7 @@ import com.mohiva.play.silhouette.core.utils.DefaultActionHandler
* @tparam I The type of the identity.
* @tparam T The type of the authenticator.
*/
trait Silhouette[I <: Identity, T <: Authenticator] extends Controller {
trait Silhouette[I <: Identity, T <: Authenticator] extends Controller with Logger {

/**
* Gets the identity service implementation.
Expand Down Expand Up @@ -168,9 +168,7 @@ trait Silhouette[I <: Identity, T <: Authenticator] extends Controller {
* @return The result to send to the client if the user isn't authorized.
*/
def handleNotAuthorized(implicit request: RequestHeader): Future[SimpleResult] = {
if (Logger.isDebugEnabled) {
Logger.debug("[Silhouette] Unauthorized user trying to access '%s'".format(request.uri))
}
logger.debug("[Silhouette] Unauthorized user trying to access '%s'".format(request.uri))

notAuthorized(request).orElse {
Play.current.global match {
Expand All @@ -193,9 +191,7 @@ trait Silhouette[I <: Identity, T <: Authenticator] extends Controller {
* @return The result to send to the client if the user isn't authenticated.
*/
def handleNotAuthenticated(implicit request: RequestHeader): Future[SimpleResult] = {
if (Logger.isDebugEnabled) {
Logger.debug("[Silhouette] Unauthenticated user trying to access '%s'".format(request.uri))
}
logger.debug("[Silhouette] Unauthenticated user trying to access '%s'".format(request.uri))

notAuthenticated(request).orElse {
Play.current.global match {
Expand All @@ -215,9 +211,8 @@ trait Silhouette[I <: Identity, T <: Authenticator] extends Controller {
def invokeBlock[A](request: Request[A], block: SecuredRequest[A] => Future[SimpleResult]) = {
currentIdentity(request).flatMap {
// A user is both authenticated and authorized. The request will be granted.
case Some(identity) if authorize.isEmpty || authorize.get.isAuthorized(identity) => {
case Some(identity) if authorize.isEmpty || authorize.get.isAuthorized(identity) =>
block(SecuredRequest(identity, request))
}
// A user is authenticated but not authorized. The request will be forbidden.
case Some(identity) =>
handleNotAuthorized(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package com.mohiva.play.silhouette.core.providers

import java.util.UUID
import play.api.Logger
import play.api.mvc.{ SimpleResult, RequestHeader, Results }
import play.api.libs.ws.SignatureCalculator
import scala.concurrent.Future
Expand All @@ -44,7 +43,8 @@ abstract class OAuth1Provider(
httpLayer: HTTPLayer,
oAuth1Service: OAuth1Service,
oAuth1Settings: OAuth1Settings)
extends SocialProvider[OAuth1Info] {
extends SocialProvider[OAuth1Info]
with Logger {

/**
* Starts the authentication process.
Expand Down Expand Up @@ -77,9 +77,7 @@ abstract class OAuth1Provider(
val cacheID = UUID.randomUUID().toString
val url = oAuth1Service.redirectUrl(info.token)
val redirect = Results.Redirect(url).withSession(request.session + (CacheKey -> cacheID))
if (Logger.isDebugEnabled) {
Logger.debug("[Silhouette][%s] Redirecting to: %s".format(id, url))
}
logger.debug("[Silhouette][%s] Redirecting to: %s".format(id, url))
cacheLayer.set(cacheID, info, CacheExpiration)
Left(redirect)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package com.mohiva.play.silhouette.core.providers

import java.net.URLEncoder._
import java.util.UUID
import play.api.Logger
import play.api.mvc.{ SimpleResult, RequestHeader, Results }
import play.api.libs.ws.Response
import play.api.libs.json._
Expand All @@ -44,7 +43,8 @@ abstract class OAuth2Provider(
settings: OAuth2Settings,
cacheLayer: CacheLayer,
httpLayer: HTTPLayer)
extends SocialProvider[OAuth2Info] {
extends SocialProvider[OAuth2Info]
with Logger {

/**
* Converts the JSON into a [[com.mohiva.play.silhouette.core.providers.OAuth2Info]] object.
Expand Down Expand Up @@ -88,10 +88,8 @@ abstract class OAuth2Provider(
val encodedParams = params.map { p => encode(p._1, "UTF-8") + "=" + encode(p._2, "UTF-8") }
val url = settings.authorizationURL + encodedParams.mkString("?", "&", "")
val redirect = Results.Redirect(url).withSession(request.session + (CacheKey -> cacheID))
if (Logger.isDebugEnabled) {
Logger.debug("[Silhouette][%s] Use authorization URL: %s".format(id, settings.authorizationURL))
Logger.debug("[Silhouette][%s] Redirecting to: %s".format(id, url))
}
logger.debug("[Silhouette][%s] Use authorization URL: %s".format(id, settings.authorizationURL))
logger.debug("[Silhouette][%s] Redirecting to: %s".format(id, url))
cacheLayer.set(cacheID, state, CacheExpiration)
Future.successful(Left(redirect))
}
Expand All @@ -110,9 +108,7 @@ abstract class OAuth2Provider(
GrantType -> Seq(AuthorizationCode),
Code -> Seq(code),
RedirectURI -> Seq(settings.redirectURL)) ++ settings.accessTokenParams.mapValues(Seq(_))).map { response =>
if (Logger.isDebugEnabled) {
Logger.debug("[Silhouette][%s] Access token response: [%s]".format(id, response.body))
}
logger.debug("[Silhouette][%s] Access token response: [%s]".format(id, response.body))
buildInfo(response)
}
}
Expand Down

0 comments on commit 121b2df

Please sign in to comment.