Advertisement
wolfjb

SilhouetteModule

Oct 11th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.49 KB | None | 0 0
  1. package utils.di
  2.  
  3. import play.api.Play
  4. import play.api.Play.current
  5. import play.api.Configuration
  6. import com.google.inject.{Provides, AbstractModule}
  7. import net.codingwell.scalaguice.ScalaModule
  8. import com.mohiva.play.silhouette.api.{EventBus, Environment}
  9. import com.mohiva.play.silhouette.api.util._
  10. import com.mohiva.play.silhouette.api.services._
  11. import com.mohiva.play.silhouette.impl.daos.AuthenticatorDAO
  12. import com.mohiva.play.silhouette.impl.authenticators._
  13. import com.mohiva.play.silhouette.impl.providers._
  14. import com.mohiva.play.silhouette.impl.util._
  15. import com.mohiva.play.silhouette.impl.services._
  16. import com.mohiva.play.silhouette.impl.daos.DelegableAuthInfoDAO
  17. import models.services.{UserService, UserServiceImpl}
  18. import models.dao._
  19. import models.User
  20.  
  21. class SilhouetteModule extends AbstractModule with ScalaModule {
  22.   def configure() {
  23.     bind[UserService].to[UserServiceImpl]
  24.     bind[UserDao].to[UserDaoImpl]
  25.     bind[DelegableAuthInfoDAO[PasswordInfo]].to[PasswordInfoDAO]
  26.     bind[CacheLayer].to[PlayCacheLayer]
  27.     bind[HTTPLayer].to[PlayHTTPLayer]
  28.     bind[AuthenticatorDAO[CookieAuthenticator]].to[CookieAuthenticatorDao]
  29.     bind[IDGenerator].toInstance(new SecureRandomIDGenerator())
  30.     bind[PasswordHasher].toInstance(new BCryptPasswordHasher)
  31.     bind[EventBus].toInstance(EventBus())
  32.   }
  33.   /**
  34.    * Provides the Silhouette environment.
  35.    *
  36.    * @param userService The user service implementation.
  37.    * @param authenticatorService The authentication service implementation.
  38.    * @param eventBus The event bus instance.
  39.    * @return The Silhouette environment.
  40.    */
  41.   @Provides
  42.   def provideEnvironment(
  43.     userService: UserService,
  44.     authenticatorService: AuthenticatorService[CookieAuthenticator],
  45.     eventBus: EventBus,
  46.     credentialsProvider: CredentialsProvider): Environment[User, CookieAuthenticator] = {
  47.  
  48.     Environment[User, CookieAuthenticator](
  49.       userService,
  50.       authenticatorService,
  51.       Map(
  52.         credentialsProvider.id -> credentialsProvider
  53.       ),
  54.       eventBus
  55.     )
  56.   }
  57.  
  58.   @Provides
  59.   def provideAuthenticatorService(
  60.     fingerprintGenerator: FingerprintGenerator,
  61.     idGenerator: IDGenerator,
  62.     configuration: Configuration,
  63.     cookieAuthenticatorDao: CookieAuthenticatorDao,
  64.     clock: Clock): AuthenticatorService[CookieAuthenticator] = {
  65.     val settings = CookieAuthenticatorSettings(
  66.       cookieName = Play.configuration.getString("silhouette.authenticator.cookieName").get,
  67.       cookiePath = Play.configuration.getString("silhouette.authenticator.cookiePath").get,
  68.       cookieDomain = Play.configuration.getString("silhouette.authenticator.cookieDomain"),
  69.       secureCookie = Play.configuration.getBoolean("silhouette.authenticator.secureCookie").get,
  70.       httpOnlyCookie = Play.configuration.getBoolean("silhouette.authenticator.httpOnlyCookie").get,
  71.       useFingerprinting = Play.configuration.getBoolean("silhouette.authenticator.useFingerprinting").get,
  72.       cookieMaxAge = Play.configuration.getInt("silhouette.authenticator.cookieMaxAge"),
  73.       authenticatorIdleTimeout = Play.configuration.getInt("silhouette.authenticator.authenticatorIdleTimeout"),
  74.       authenticatorExpiry = Play.configuration.getInt("silhouette.authenticator.authenticatorExpiry").get
  75.     )
  76.     new CookieAuthenticatorService(settings, cookieAuthenticatorDao, fingerprintGenerator, idGenerator, clock)
  77.   }
  78.  
  79.   /**
  80.    * Provides the auth info service.
  81.    *
  82.    * @param passwordInfoDAO The implementation of the delegable password auth info DAO.
  83.    *
  84.    * @return The auth info service instance.
  85.    */
  86.   @Provides
  87.   def provideAuthInfoService(
  88.     passwordInfoDAO: DelegableAuthInfoDAO[PasswordInfo]): AuthInfoService = {
  89.  
  90.     new DelegableAuthInfoService(passwordInfoDAO)
  91.   }
  92.  
  93.   /**
  94.    * Provides the avatar service.
  95.    *
  96.    * @param httpLayer The HTTP layer implementation.
  97.    * @return The avatar service implementation.
  98.    */
  99.   @Provides
  100.   def provideAvatarService(httpLayer: HTTPLayer): AvatarService = new GravatarService(httpLayer)
  101.  
  102.   /**
  103.    * Provides the credentials provider.
  104.    *
  105.    * @param authInfoService The auth info service implemenetation.
  106.    * @param passwordHasher The default password hasher implementation.
  107.    * @return The credentials provider.
  108.    */
  109.   @Provides
  110.   def provideCredentialsProvider(
  111.     authInfoService: AuthInfoService,
  112.     passwordHasher: PasswordHasher): CredentialsProvider = {
  113.  
  114.     new CredentialsProvider(authInfoService, passwordHasher, Seq(passwordHasher))
  115.   }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement