This repository has been archived by the owner on Sep 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
SilhouetteModule.scala
340 lines (304 loc) · 12 KB
/
SilhouetteModule.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package modules
import com.google.inject.name.Named
import com.google.inject.{ AbstractModule, Provides }
import com.mohiva.play.silhouette.api.crypto.{ CookieSigner, Crypter, CrypterAuthenticatorEncoder }
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.services._
import com.mohiva.play.silhouette.api.util._
import com.mohiva.play.silhouette.api.{ Environment, EventBus, Silhouette, SilhouetteProvider }
import com.mohiva.play.silhouette.crypto.{ JcaCookieSigner, JcaCookieSignerSettings, JcaCrypter, JcaCrypterSettings }
import com.mohiva.play.silhouette.impl.authenticators._
import com.mohiva.play.silhouette.impl.providers._
import com.mohiva.play.silhouette.impl.providers.oauth1._
import com.mohiva.play.silhouette.impl.providers.oauth1.secrets.{ CookieSecretProvider, CookieSecretSettings }
import com.mohiva.play.silhouette.impl.providers.oauth1.services.PlayOAuth1Service
import com.mohiva.play.silhouette.impl.providers.oauth2._
import com.mohiva.play.silhouette.impl.providers.oauth2.state.DummyStateProvider
import com.mohiva.play.silhouette.impl.services._
import com.mohiva.play.silhouette.impl.util._
import com.mohiva.play.silhouette.password.BCryptPasswordHasher
import com.mohiva.play.silhouette.persistence.daos.{ DelegableAuthInfoDAO, InMemoryAuthInfoDAO }
import com.mohiva.play.silhouette.persistence.repositories.DelegableAuthInfoRepository
import models.daos._
import models.services.{ UserService, UserServiceImpl }
import net.ceedubs.ficus.Ficus._
import net.ceedubs.ficus.readers.ArbitraryTypeReader._
import net.ceedubs.ficus.readers.EnumerationReader._
import net.codingwell.scalaguice.ScalaModule
import play.api.Configuration
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.ws.WSClient
import utils.auth.DefaultEnv
/**
* The Guice module which wires all Silhouette dependencies.
*/
class SilhouetteModule extends AbstractModule with ScalaModule {
/**
* Configures the module.
*/
def configure() {
bind[UserService].to[UserServiceImpl]
bind[UserDAO].to[UserDAOImpl]
bind[Silhouette[DefaultEnv]].to[SilhouetteProvider[DefaultEnv]]
bind[CacheLayer].to[PlayCacheLayer]
bind[OAuth2StateProvider].to[DummyStateProvider]
bind[IDGenerator].toInstance(new SecureRandomIDGenerator())
bind[PasswordHasher].toInstance(new BCryptPasswordHasher)
bind[FingerprintGenerator].toInstance(new DefaultFingerprintGenerator(false))
bind[EventBus].toInstance(EventBus())
bind[Clock].toInstance(Clock())
// Replace this with the bindings to your concrete DAOs
bind[DelegableAuthInfoDAO[PasswordInfo]].toInstance(new InMemoryAuthInfoDAO[PasswordInfo])
bind[DelegableAuthInfoDAO[OAuth1Info]].toInstance(new InMemoryAuthInfoDAO[OAuth1Info])
bind[DelegableAuthInfoDAO[OAuth2Info]].toInstance(new InMemoryAuthInfoDAO[OAuth2Info])
bind[DelegableAuthInfoDAO[OpenIDInfo]].toInstance(new InMemoryAuthInfoDAO[OpenIDInfo])
}
/**
* Provides the HTTP layer implementation.
*
* @param client Play's WS client.
* @return The HTTP layer implementation.
*/
@Provides
def provideHTTPLayer(client: WSClient): HTTPLayer = new PlayHTTPLayer(client)
/**
* 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[JWTAuthenticator],
eventBus: EventBus): Environment[DefaultEnv] = {
Environment[DefaultEnv](
userService,
authenticatorService,
Seq(),
eventBus
)
}
/**
* Provides the social provider registry.
*
* @param facebookProvider The Facebook provider implementation.
* @param googleProvider The Google provider implementation.
* @param vkProvider The VK provider implementation.
* @param twitterProvider The Twitter provider implementation.
* @param xingProvider The Xing provider implementation.
* @return The Silhouette environment.
*/
@Provides
def provideSocialProviderRegistry(
facebookProvider: FacebookProvider,
googleProvider: GoogleProvider,
vkProvider: VKProvider,
twitterProvider: TwitterProvider,
xingProvider: XingProvider): SocialProviderRegistry = {
SocialProviderRegistry(Seq(
googleProvider,
facebookProvider,
twitterProvider,
vkProvider,
xingProvider
))
}
/**
* Provides the cookie signer for the OAuth1 token secret provider.
*
* @param configuration The Play configuration.
* @return The cookie signer for the OAuth1 token secret provider.
*/
@Provides @Named("oauth1-token-secret-cookie-signer")
def provideOAuth1TokenSecretCookieSigner(configuration: Configuration): CookieSigner = {
val config = configuration.underlying.as[JcaCookieSignerSettings]("silhouette.oauth1TokenSecretProvider.cookie.signer")
new JcaCookieSigner(config)
}
/**
* Provides the crypter for the OAuth1 token secret provider.
*
* @param configuration The Play configuration.
* @return The crypter for the OAuth1 token secret provider.
*/
@Provides @Named("oauth1-token-secret-crypter")
def provideOAuth1TokenSecretCrypter(configuration: Configuration): Crypter = {
val config = configuration.underlying.as[JcaCrypterSettings]("silhouette.oauth1TokenSecretProvider.crypter")
new JcaCrypter(config)
}
/**
* Provides the crypter for the authenticator.
*
* @param configuration The Play configuration.
* @return The crypter for the authenticator.
*/
@Provides @Named("authenticator-crypter")
def provideAuthenticatorCrypter(configuration: Configuration): Crypter = {
val config = configuration.underlying.as[JcaCrypterSettings]("silhouette.authenticator.crypter")
new JcaCrypter(config)
}
/**
* 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)
}
/**
* Provides the authenticator service.
*
* @param crypter The crypter implementation.
* @param idGenerator The ID generator implementation.
* @param configuration The Play configuration.
* @param clock The clock instance.
* @return The authenticator service.
*/
@Provides
def provideAuthenticatorService(
@Named("authenticator-crypter") crypter: Crypter,
idGenerator: IDGenerator,
configuration: Configuration,
clock: Clock): AuthenticatorService[JWTAuthenticator] = {
val config = configuration.underlying.as[JWTAuthenticatorSettings]("silhouette.authenticator")
val encoder = new CrypterAuthenticatorEncoder(crypter)
new JWTAuthenticatorService(config, None, encoder, idGenerator, clock)
}
/**
* 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 OAuth1 token secret provider.
*
* @param cookieSigner The cookie signer implementation.
* @param crypter The crypter implementation.
* @param configuration The Play configuration.
* @param clock The clock instance.
* @return The OAuth1 token secret provider implementation.
*/
@Provides
def provideOAuth1TokenSecretProvider(
@Named("oauth1-token-secret-cookie-signer") cookieSigner: CookieSigner,
@Named("oauth1-token-secret-crypter") crypter: Crypter,
configuration: Configuration,
clock: Clock): OAuth1TokenSecretProvider = {
val settings = configuration.underlying.as[CookieSecretSettings]("silhouette.oauth1TokenSecretProvider")
new CookieSecretProvider(settings, cookieSigner, crypter, clock)
}
/**
* Provides the password hasher registry.
*
* @param passwordHasher The default password hasher implementation.
* @return The password hasher registry.
*/
@Provides
def providePasswordHasherRegistry(passwordHasher: PasswordHasher): PasswordHasherRegistry = {
new PasswordHasherRegistry(passwordHasher)
}
/**
* Provides the credentials provider.
*
* @param authInfoRepository The auth info repository implementation.
* @param passwordHasherRegistry The password hasher registry.
* @return The credentials provider.
*/
@Provides
def provideCredentialsProvider(
authInfoRepository: AuthInfoRepository,
passwordHasherRegistry: PasswordHasherRegistry): CredentialsProvider = {
new CredentialsProvider(authInfoRepository, passwordHasherRegistry)
}
/**
* Provides the Facebook provider.
*
* @param httpLayer The HTTP layer implementation.
* @param stateProvider The OAuth2 state provider implementation.
* @param configuration The Play configuration.
* @return The Facebook provider.
*/
@Provides
def provideFacebookProvider(
httpLayer: HTTPLayer,
stateProvider: OAuth2StateProvider,
configuration: Configuration): FacebookProvider = {
new FacebookProvider(httpLayer, stateProvider, configuration.underlying.as[OAuth2Settings]("silhouette.facebook"))
}
/**
* Provides the Google provider.
*
* @param httpLayer The HTTP layer implementation.
* @param stateProvider The OAuth2 state provider implementation.
* @param configuration The Play configuration.
* @return The Google provider.
*/
@Provides
def provideGoogleProvider(
httpLayer: HTTPLayer,
stateProvider: OAuth2StateProvider,
configuration: Configuration): GoogleProvider = {
new GoogleProvider(httpLayer, stateProvider, configuration.underlying.as[OAuth2Settings]("silhouette.google"))
}
/**
* Provides the VK provider.
*
* @param httpLayer The HTTP layer implementation.
* @param stateProvider The OAuth2 state provider implementation.
* @param configuration The Play configuration.
* @return The VK provider.
*/
@Provides
def provideVKProvider(
httpLayer: HTTPLayer,
stateProvider: OAuth2StateProvider,
configuration: Configuration): VKProvider = {
new VKProvider(httpLayer, stateProvider, configuration.underlying.as[OAuth2Settings]("silhouette.vk"))
}
/**
* Provides the Twitter provider.
*
* @param httpLayer The HTTP layer implementation.
* @param tokenSecretProvider The token secret provider implementation.
* @param configuration The Play configuration.
* @return The Twitter provider.
*/
@Provides
def provideTwitterProvider(
httpLayer: HTTPLayer,
tokenSecretProvider: OAuth1TokenSecretProvider,
configuration: Configuration): TwitterProvider = {
val settings = configuration.underlying.as[OAuth1Settings]("silhouette.twitter")
new TwitterProvider(httpLayer, new PlayOAuth1Service(settings), tokenSecretProvider, settings)
}
/**
* Provides the Xing provider.
*
* @param httpLayer The HTTP layer implementation.
* @param tokenSecretProvider The token secret provider implementation.
* @param configuration The Play configuration.
* @return The Xing provider.
*/
@Provides
def provideXingProvider(
httpLayer: HTTPLayer,
tokenSecretProvider: OAuth1TokenSecretProvider,
configuration: Configuration): XingProvider = {
val settings = configuration.underlying.as[OAuth1Settings]("silhouette.xing")
new XingProvider(httpLayer, new PlayOAuth1Service(settings), tokenSecretProvider, settings)
}
}