diff --git a/.gitignore b/.gitignore index 8570daf..89a2a7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,68 @@ -logs -project/project -project/target -target -tmp +# Created by https://www.gitignore.io + +### OSX ### +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +### Scala ### +*.class +*.log + +# sbt specific +.cache .history -dist -/.idea -/*.iml -/out -/.idea_modules +.lib/ +dist/* +target/ +lib_managed/ +src_managed/ +project/boot/ +project/plugins/project/ + +# Scala-IDE specific +.scala_dependencies +.worksheet + + +### SBT ### +# Simple Build Tool +# http://www.scala-sbt.org/release/docs/Getting-Started/Directories.html#configuring-version-control + +target/ +lib_managed/ +src_managed/ +project/boot/ +.history +.cache + + +### PlayFramework ### +# Ignore Play! working directory # +bin/ +/db +.eclipse +/lib/ +/logs/ +/modules +/project/project +/project/target +/target +tmp/ +test-result +server.pid +*.iml +*.eml +/dist/ +.cache + + /.classpath /.project -/RUNNING_PID /.settings + + diff --git a/app/controllers/security/rest/RestCredentialsAuthController.scala b/app/controllers/security/rest/RestCredentialsAuthController.scala index 77bcd11..51e459f 100644 --- a/app/controllers/security/rest/RestCredentialsAuthController.scala +++ b/app/controllers/security/rest/RestCredentialsAuthController.scala @@ -5,15 +5,17 @@ import services.UserService import play.api.mvc._ import play.api.libs.json._ import play.api.libs.concurrent.Execution.Implicits._ + import com.mohiva.play.silhouette.api._ -import com.mohiva.play.silhouette.api.exceptions.AuthenticationException +import com.mohiva.play.silhouette.api.exceptions.ConfigurationException import com.mohiva.play.silhouette.api.services.AuthInfoService import com.mohiva.play.silhouette.api.util.PasswordHasher -import com.mohiva.play.silhouette.impl.authenticators.JWTAuthenticator import com.mohiva.play.silhouette.impl.providers.CredentialsProvider import com.mohiva.play.silhouette.impl.authenticators.JWTAuthenticator +import com.mohiva.play.silhouette.impl.exceptions.IdentityNotFoundException import com.mohiva.play.silhouette.api.Silhouette import com.mohiva.play.silhouette.api.util.Credentials + import modules.cake.HeaderEnvironmentModule import scala.concurrent.{ Future } @@ -43,28 +45,24 @@ class RestCredentialsAuthController extends Silhouette[User, JWTAuthenticator] * * @return The result to display. */ - def authenticate = Action.async(parse.json) { implicit request => - request.body.validate[Credentials].map { credentials => - (env.providers.get(CredentialsProvider.ID) match { - case Some(p: CredentialsProvider) => p.authenticate(credentials) - case _ => Future.failed(new AuthenticationException(s"Cannot find credentials provider")) - }).flatMap { loginInfo => - userService.retrieve(loginInfo).flatMap { - case Some(user) => env.authenticatorService.create(user.loginInfo).flatMap { authenticator => - env.eventBus.publish(LoginEvent(user, request, request2lang)) - env.authenticatorService.init(authenticator).flatMap { token => - env.authenticatorService.embed(token, Future.successful { - Ok(Json.toJson(Token(token = token, expiresOn = authenticator.expirationDate))) - }) - } + def authenticate = Action.async(parse.json[Credentials]) { implicit request => + (env.providers.get(CredentialsProvider.ID) match { + case Some(p: CredentialsProvider) => p.authenticate(request.body) + case _ => Future.failed(new ConfigurationException(s"Cannot find credentials provider")) + }).flatMap { loginInfo => + userService.retrieve(loginInfo).flatMap { + case Some(user) => env.authenticatorService.create(user.loginInfo).flatMap { authenticator => + env.eventBus.publish(LoginEvent(user, request, request2lang)) + env.authenticatorService.init(authenticator).flatMap { token => + env.authenticatorService.embed(token, Future.successful { + Ok(Json.toJson(Token(token = token, expiresOn = authenticator.expirationDate))) + }) } - case None => - Future.failed(new AuthenticationException("Couldn't find user")) } - }.recoverWith(exceptionHandler) - }.recoverTotal { - case error => Future.successful(BadRequest(Json.obj("message" -> JsError.toFlatJson(error)))) - } + case None => + Future.failed(new IdentityNotFoundException("Couldn't find user")) + } + }.recoverWith(exceptionHandler) } } diff --git a/app/controllers/security/rest/RestSocialAuthController.scala b/app/controllers/security/rest/RestSocialAuthController.scala index 9f35310..ae0dfcb 100644 --- a/app/controllers/security/rest/RestSocialAuthController.scala +++ b/app/controllers/security/rest/RestSocialAuthController.scala @@ -7,7 +7,7 @@ import play.api.libs.json._ import play.api.libs.concurrent.Execution.Implicits._ import com.mohiva.play.silhouette.api._ import com.mohiva.play.silhouette.api.services._ -import com.mohiva.play.silhouette.api.exceptions.AuthenticationException +import com.mohiva.play.silhouette.api.exceptions.ConfigurationException import com.mohiva.play.silhouette.api.services.AuthInfoService import com.mohiva.play.silhouette.impl.authenticators.JWTAuthenticator import com.mohiva.play.silhouette.impl.providers._ @@ -48,7 +48,7 @@ class RestSocialAuthController extends Silhouette[User, JWTAuthenticator] with H result } } - case _ => Future.failed(new AuthenticationException(s"Cannot authenticate with unexpected social provider $provider")) + case _ => Future.failed(new ConfigurationException(s"Cannot authenticate with unexpected social provider $provider")) }).recoverWith(exceptionHandler) } @@ -96,7 +96,7 @@ class RestSocialAuthController extends Silhouette[User, JWTAuthenticator] with H case Some(p: OAuth2Provider with CommonSocialProfileBuilder) => //for OAuth2 provider type val authInfo = OAuth2Info(accessToken = socialAuth.token, expiresIn = socialAuth.expiresIn) p.retrieveProfile(authInfo).map(profile => (profile, authInfo)) - case _ => Future.successful(new AuthenticationException(s"Cannot retrive information with unexpected social provider $provider")) + case _ => Future.successful(new ConfigurationException(s"Cannot retrive information with unexpected social provider $provider")) } } diff --git a/app/modules/cake/AuthenticatorServiceModule.scala b/app/modules/cake/AuthenticatorServiceModule.scala index bdc0241..5fdf812 100644 --- a/app/modules/cake/AuthenticatorServiceModule.scala +++ b/app/modules/cake/AuthenticatorServiceModule.scala @@ -1,7 +1,5 @@ package modules.cake -package modules.cake - import play.api.Play import play.api.Play.current /** mohiva module import */ diff --git a/app/modules/cake/EnvironmentModule.scala b/app/modules/cake/EnvironmentModule.scala index 1dc5f7a..1e34ef0 100644 --- a/app/modules/cake/EnvironmentModule.scala +++ b/app/modules/cake/EnvironmentModule.scala @@ -7,7 +7,6 @@ import com.mohiva.play.silhouette.impl.util.SecureRandomIDGenerator import com.mohiva.play.silhouette.api.Environment import com.mohiva.play.silhouette.api.EventBus import com.mohiva.play.silhouette.api.util.PlayHTTPLayer -import modules.cake.{ HeaderAuthenticatorServiceModule/*, CookieAuthenticatorServiceModule */} import models.users.User import models.daos._ diff --git a/build.sbt b/build.sbt index 55d960a..bd25fb1 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ name := """silhouette-rest-seed""" -version := "2.0-RC1" +version := "2.0" lazy val root = (project in file(".")).enablePlugins(PlayScala) @@ -9,7 +9,8 @@ scalaVersion := "2.11.1" libraryDependencies ++= Seq( cache, ws, - "com.mohiva" %% "play-silhouette" % "2.0-RC1", + "com.mohiva" %% "play-silhouette" % "2.0", + "com.mohiva" %% "play-silhouette-testkit" % "2.0" % "test", "com.typesafe.play.plugins" %% "play-plugins-mailer" % "2.3.0" ) diff --git a/test/ApplicationSpec.scala b/test/ApplicationSpec.scala deleted file mode 100644 index 6e20bd5..0000000 --- a/test/ApplicationSpec.scala +++ /dev/null @@ -1,30 +0,0 @@ -import org.specs2.mutable._ -import org.specs2.runner._ -import org.junit.runner._ - -import play.api.test._ -import play.api.test.Helpers._ - -/** - * Add your spec here. - * You can mock out a whole application including requests, plugins etc. - * For more information, consult the wiki. - */ -@RunWith(classOf[JUnitRunner]) -class ApplicationSpec extends Specification { - - "Application" should { - - "send 404 on a bad request" in new WithApplication{ - route(FakeRequest(GET, "/boum")) must beNone - } - - "render the index page" in new WithApplication{ - val home = route(FakeRequest(GET, "/")).get - - status(home) must equalTo(OK) - contentType(home) must beSome.which(_ == "text/html") - contentAsString(home) must contain ("Your new application is ready.") - } - } -} diff --git a/test/IntegrationSpec.scala b/test/IntegrationSpec.scala deleted file mode 100644 index 652edde..0000000 --- a/test/IntegrationSpec.scala +++ /dev/null @@ -1,24 +0,0 @@ -import org.specs2.mutable._ -import org.specs2.runner._ -import org.junit.runner._ - -import play.api.test._ -import play.api.test.Helpers._ - -/** - * add your integration spec here. - * An integration test will fire up a whole play application in a real (or headless) browser - */ -@RunWith(classOf[JUnitRunner]) -class IntegrationSpec extends Specification { - - "Application" should { - - "work from within a browser" in new WithBrowser { - - browser.goTo("http://localhost:" + port) - - browser.pageSource must contain("Your new application is ready.") - } - } -}