alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (SecuritySpec.scala)

This example Play Framework source code file (SecuritySpec.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

actionbuilder, api, authenticated, authenticatedbuilder, authenticateddbrequest, concurrent, connection, mvc, ok, play, play framework, request, string, test, testaction, user

The SecuritySpec.scala Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.it.auth

import play.api.test._
import play.api.mvc.Security.{AuthenticatedRequest, AuthenticatedBuilder}
import play.api.mvc._
import scala.concurrent.Future
import play.api.test.FakeApplication

object SecuritySpec extends PlaySpecification {

  "AuthenticatedBuilder" should {
    "block unauthenticated requests" in withApplication {
      status(TestAction { req =>
        Results.Ok(req.user)
      }(FakeRequest())) must_== UNAUTHORIZED
    }
    "allow authenticated requests" in withApplication {
      val result = TestAction { req =>
        Results.Ok(req.user)
      }(FakeRequest().withSession("username" -> "john"))
      status(result) must_== OK
      contentAsString(result) must_== "john"
    }

    "allow use as an ActionBuilder" in withApplication {
      val result = Authenticated { req =>
        Results.Ok(s"${req.conn.name}:${req.user.name}")
      }(FakeRequest().withSession("user" -> "Phil"))
      status(result) must_== OK
      contentAsString(result) must_== "fake:Phil"
    }
  }

  val TestAction = AuthenticatedBuilder()

  case class User(name: String)
  def getUserFromRequest(req: RequestHeader) = req.session.get("user") map (User(_))

  class AuthenticatedDbRequest[A](val user: User, val conn: Connection, request: Request[A]) extends WrappedRequest[A](request)

  object Authenticated extends ActionBuilder[AuthenticatedDbRequest] {
    def invokeBlock[A](request: Request[A], block: (AuthenticatedDbRequest[A]) => Future[Result]) = {
      AuthenticatedBuilder(req => getUserFromRequest(req)).authenticate(request, { authRequest: AuthenticatedRequest[A, User] =>
        DB.withConnection { conn =>
          block(new AuthenticatedDbRequest[A](authRequest.user, conn, request))
        }
      })
    }
  }

  object DB {
    def withConnection[A](block: Connection => A) = {
      block(FakeConnection)
    }
  }
  object FakeConnection extends Connection("fake")
  case class Connection(name: String)

  def withApplication[T](block: => T) =
    running(FakeApplication(additionalConfiguration = Map("application.secret" -> "foobar")))(block)
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework SecuritySpec.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.