Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package utils.di
- import play.api.Play
- import play.api.Play.current
- import play.api.Configuration
- import com.google.inject.{Provides, AbstractModule}
- import net.codingwell.scalaguice.ScalaModule
- import com.mohiva.play.silhouette.api.{EventBus, Environment}
- import com.mohiva.play.silhouette.api.util._
- import com.mohiva.play.silhouette.api.services._
- import com.mohiva.play.silhouette.impl.daos.AuthenticatorDAO
- import com.mohiva.play.silhouette.impl.authenticators._
- import com.mohiva.play.silhouette.impl.providers._
- import com.mohiva.play.silhouette.impl.util._
- import com.mohiva.play.silhouette.impl.services._
- import com.mohiva.play.silhouette.impl.daos.DelegableAuthInfoDAO
- import models.services.{UserService, UserServiceImpl}
- import models.dao._
- import models.User
- class SilhouetteModule extends AbstractModule with ScalaModule {
- def configure() {
- bind[UserService].to[UserServiceImpl]
- bind[UserDao].to[UserDaoImpl]
- bind[DelegableAuthInfoDAO[PasswordInfo]].to[PasswordInfoDAO]
- bind[CacheLayer].to[PlayCacheLayer]
- bind[HTTPLayer].to[PlayHTTPLayer]
- bind[AuthenticatorDAO[CookieAuthenticator]].to[CookieAuthenticatorDao]
- bind[IDGenerator].toInstance(new SecureRandomIDGenerator())
- bind[PasswordHasher].toInstance(new BCryptPasswordHasher)
- bind[EventBus].toInstance(EventBus())
- }
- /**
- * Provides the Silhouette environment.
- *
- * @param userService The user service implementation.
- * @param authenticatorService The authentication service implementation.
- * @param eventBus The event bus instance.
- * @return The Silhouette environment.
- */
- @Provides
- def provideEnvironment(
- userService: UserService,
- authenticatorService: AuthenticatorService[CookieAuthenticator],
- eventBus: EventBus,
- credentialsProvider: CredentialsProvider): Environment[User, CookieAuthenticator] = {
- Environment[User, CookieAuthenticator](
- userService,
- authenticatorService,
- Map(
- credentialsProvider.id -> credentialsProvider
- ),
- eventBus
- )
- }
- @Provides
- def provideAuthenticatorService(
- fingerprintGenerator: FingerprintGenerator,
- idGenerator: IDGenerator,
- configuration: Configuration,
- cookieAuthenticatorDao: CookieAuthenticatorDao,
- clock: Clock): AuthenticatorService[CookieAuthenticator] = {
- val settings = CookieAuthenticatorSettings(
- cookieName = Play.configuration.getString("silhouette.authenticator.cookieName").get,
- cookiePath = Play.configuration.getString("silhouette.authenticator.cookiePath").get,
- cookieDomain = Play.configuration.getString("silhouette.authenticator.cookieDomain"),
- secureCookie = Play.configuration.getBoolean("silhouette.authenticator.secureCookie").get,
- httpOnlyCookie = Play.configuration.getBoolean("silhouette.authenticator.httpOnlyCookie").get,
- useFingerprinting = Play.configuration.getBoolean("silhouette.authenticator.useFingerprinting").get,
- cookieMaxAge = Play.configuration.getInt("silhouette.authenticator.cookieMaxAge"),
- authenticatorIdleTimeout = Play.configuration.getInt("silhouette.authenticator.authenticatorIdleTimeout"),
- authenticatorExpiry = Play.configuration.getInt("silhouette.authenticator.authenticatorExpiry").get
- )
- new CookieAuthenticatorService(settings, cookieAuthenticatorDao, fingerprintGenerator, idGenerator, clock)
- }
- /**
- * Provides the auth info service.
- *
- * @param passwordInfoDAO The implementation of the delegable password auth info DAO.
- *
- * @return The auth info service instance.
- */
- @Provides
- def provideAuthInfoService(
- passwordInfoDAO: DelegableAuthInfoDAO[PasswordInfo]): AuthInfoService = {
- new DelegableAuthInfoService(passwordInfoDAO)
- }
- /**
- * Provides the avatar service.
- *
- * @param httpLayer The HTTP layer implementation.
- * @return The avatar service implementation.
- */
- @Provides
- def provideAvatarService(httpLayer: HTTPLayer): AvatarService = new GravatarService(httpLayer)
- /**
- * Provides the credentials provider.
- *
- * @param authInfoService The auth info service implemenetation.
- * @param passwordHasher The default password hasher implementation.
- * @return The credentials provider.
- */
- @Provides
- def provideCredentialsProvider(
- authInfoService: AuthInfoService,
- passwordHasher: PasswordHasher): CredentialsProvider = {
- new CredentialsProvider(authInfoService, passwordHasher, Seq(passwordHasher))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement