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

Move persistence based API into a separate project #419

Merged
merged 1 commit into from
Oct 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ object Build extends Build {
base = file("silhouette")
)

val silhouettePersistence = Project(
id = "play-silhouette-persistence",
base = file("silhouette-persistence"),
dependencies = Seq(silhouette)
)

val silhouettePersistenceMemory = Project(
id = "play-silhouette-persistence-memory",
base = file("silhouette-persistence-memory"),
dependencies = Seq(silhouettePersistence)
)

val silhouetteTestkit = Project(
id = "play-silhouette-testkit",
base = file("silhouette-testkit"),
Expand All @@ -33,7 +45,12 @@ object Build extends Build {
val root = Project(
id = "root",
base = file("."),
aggregate = Seq(silhouette, silhouetteTestkit),
aggregate = Seq(
silhouette,
silhouettePersistence,
silhouettePersistenceMemory,
silhouetteTestkit
),
settings = Defaults.coreDefaultSettings ++
APIDoc.settings ++
Seq(
Expand Down
9 changes: 7 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@ object Dependencies {
val cache = "com.typesafe.play" %% "play-cache" % version
val test = "com.typesafe.play" %% "play-test" % version
val specs2 = "com.typesafe.play" %% "play-specs2" % version
object Specs2 {
private val version = "3.4"
val matcherExtra = "org.specs2" %% "specs2-matcher-extra" % version
val mock = "org.specs2" %% "specs2-mock" % version
}
}

object Specs2 {
private val version = "3.4"
private val version = "3.6.5"
val core = "org.specs2" %% "specs2-core" % version
val matcherExtra = "org.specs2" %% "specs2-matcher-extra" % version
val mock = "org.specs2" %% "specs2-mock" % version
}

val jbcrypt = "org.mindrot" % "jbcrypt" % "0.3m"
val jwtCore = "com.atlassian.jwt" % "jwt-core" % "1.2.4"
val jwtApi = "com.atlassian.jwt" % "jwt-api" % "1.2.4"
val mockito = "org.mockito" % "mockito-core" % "1.10.19"
val scalaGuice = "net.codingwell" %% "scala-guice" % "4.0.0"
val akkaTestkit = "com.typesafe.akka" %% "akka-testkit" % "2.3.10"
}
Expand Down
8 changes: 8 additions & 0 deletions silhouette-persistence-memory/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Dependencies._

libraryDependencies ++= Seq(
Library.scalaGuice,
Library.Specs2.core % Test
)

enablePlugins(Doc)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2015 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.persistence.daos

import com.mohiva.play.silhouette.impl.providers.OAuth1Info

/**
* The DAO to persist the OAuth1 information.
*
* Note: Not thread safe, demo only.
*/
class OAuth1InfoDAO extends InMemoryAuthInfoDAO[OAuth1Info]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2015 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.persistence.daos

import com.mohiva.play.silhouette.impl.providers.OAuth2Info

/**
* The DAO to persist the OAuth2 information.
*
* Note: Not thread safe, demo only.
*/
class OAuth2InfoDAO extends InMemoryAuthInfoDAO[OAuth2Info]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2015 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.persistence.daos

import com.mohiva.play.silhouette.impl.providers.OpenIDInfo

/**
* The DAO to persist the OpenID information.
*
* Note: Not thread safe, demo only.
*/
class OpenIDInfoDAO extends InMemoryAuthInfoDAO[OpenIDInfo]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2015 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.persistence.daos

import com.mohiva.play.silhouette.api.util.PasswordInfo

/**
* The DAO to persist the password information.
*
* Note: Not thread safe, demo only.
*/
class PasswordInfoDAO extends InMemoryAuthInfoDAO[PasswordInfo]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright 2015 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.persistence

/**
* Provides drop-in DAO replacements for the common `AuthInfo` implementations.
*/
package object daos
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2015 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.persistence.modules

import com.google.inject.{ AbstractModule, Provides }
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.util.PasswordInfo
import com.mohiva.play.silhouette.impl.providers.{ OAuth1Info, OAuth2Info, OpenIDInfo }
import com.mohiva.play.silhouette.persistence.daos._
import com.mohiva.play.silhouette.persistence.repositories.DelegableAuthInfoRepository
import net.codingwell.scalaguice.ScalaModule

import scala.concurrent.ExecutionContext.Implicits.global

/**
* Provides Guice bindings for the persistence module.
*/
class PersistenceModule extends AbstractModule with ScalaModule {

/**
* Configures the module.
*/
def configure(): Unit = {
bind[DelegableAuthInfoDAO[PasswordInfo]].to[PasswordInfoDAO]
bind[DelegableAuthInfoDAO[OAuth1Info]].to[OAuth1InfoDAO]
bind[DelegableAuthInfoDAO[OAuth2Info]].to[OAuth2InfoDAO]
bind[DelegableAuthInfoDAO[OpenIDInfo]].to[OpenIDInfoDAO]
}

/**
* Provides the auth info repository.
*
* @param passwordInfoDAO The implementation of the delegable password auth info DAO.
* @param oauth1InfoDAO The implementation of the delegable OAuth1 auth info DAO.
* @param oauth2InfoDAO The implementation of the delegable OAuth2 auth info DAO.
* @param openIDInfoDAO The implementation of the delegable OpenID auth info DAO.
* @return The auth info repository instance.
*/
@Provides
def provideAuthInfoRepository(
passwordInfoDAO: DelegableAuthInfoDAO[PasswordInfo],
oauth1InfoDAO: DelegableAuthInfoDAO[OAuth1Info],
oauth2InfoDAO: DelegableAuthInfoDAO[OAuth2Info],
openIDInfoDAO: DelegableAuthInfoDAO[OpenIDInfo]): AuthInfoRepository = {

new DelegableAuthInfoRepository(passwordInfoDAO, oauth1InfoDAO, oauth2InfoDAO, openIDInfoDAO)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Copyright 2015 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.persistence.daos

import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.OAuth1Info
import org.specs2.concurrent.ExecutionEnv
import org.specs2.control.NoLanguageFeatures
import org.specs2.mutable.Specification
import org.specs2.specification.Scope

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.postfixOps

/**
* Test case for the [[OAuth1InfoDAO]] class.
*/
class OAuth1InfoDAOSpec(implicit ev: ExecutionEnv) extends Specification with NoLanguageFeatures {

"The `find` method" should {
"find an OAuth1 info for the given login info" in new Context {
Await.result(dao.save(loginInfo, authInfo), 10 seconds)

dao.find(loginInfo) must beSome(authInfo).await
}

"return None if no OAuth1 info for the given login info exists" in new Context {
dao.find(loginInfo.copy(providerKey = "new.key")) should beNone.await
}
}

"The `add` method" should {
"add a new OAuth1 info" in new Context {
dao.add(loginInfo.copy(providerKey = "new.key"), authInfo) must beEqualTo(authInfo).await
dao.find(loginInfo.copy(providerKey = "new.key")) must beSome(authInfo).await
}
}

"The `update` method" should {
"update an existing OAuth1 info" in new Context {
val updatedInfo = authInfo.copy(secret = "updated")

dao.update(loginInfo, updatedInfo) must beEqualTo(updatedInfo).await
dao.find(loginInfo) must beSome(updatedInfo).await
}
}

"The `save` method" should {
"insert a new OAuth1 info" in new Context {
dao.save(loginInfo.copy(providerKey = "new.key"), authInfo) must beEqualTo(authInfo).await
dao.find(loginInfo.copy(providerKey = "new.key")) must beSome(authInfo).await
}

"update an existing OAuth1 info" in new Context {
val updatedInfo = authInfo.copy(secret = "updated")

dao.update(loginInfo, updatedInfo) must beEqualTo(updatedInfo).await
dao.find(loginInfo) must beSome(updatedInfo).await
}
}

"The `remove` method" should {
"remove an OAuth1 info" in new Context {
Await.result(dao.remove(loginInfo), 10 seconds)
dao.find(loginInfo) must beNone.await
}
}

/**
* The context.
*/
trait Context extends Scope {

/**
* The OAuth1 info DAO implementation.
*/
lazy val dao = new OAuth1InfoDAO

/**
* A login info.
*/
lazy val loginInfo = LoginInfo("provider", "6253282")

/**
* A OAuth1 info.
*/
lazy val authInfo = OAuth1Info("my.token", "my.consumer.secret")
}
}
Loading