forked from playframework/play-silhouette
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCasProvider.scala
More file actions
380 lines (333 loc) · 11.5 KB
/
Copy pathCasProvider.scala
File metadata and controls
380 lines (333 loc) · 11.5 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
* 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 play.silhouette.impl.providers
import play.silhouette
import java.net.URI
import play.silhouette.api.exceptions.ConfigurationException
import play.silhouette.api.util.{ ExtractableRequest, HTTPLayer }
import play.silhouette.api.{ AuthInfo, Logger, LoginInfo }
import play.silhouette.impl
import play.silhouette.impl.exceptions.ProfileRetrievalException
import play.silhouette.impl.providers
import play.silhouette.impl.providers.CasProvider._
import org.apereo.cas.client.Protocol
import org.apereo.cas.client.authentication.AttributePrincipal
import org.apereo.cas.client.validation.{ AbstractUrlBasedTicketValidator, _ }
import play.api.mvc.{ Result, Results }
import scala.concurrent.duration._
import scala.concurrent.{ ExecutionContext, Future }
import scala.util.Try
import scala.jdk.CollectionConverters._
/**
* The CAS authentication information.
*
* @param ticket The ticket.
*/
final case class CasInfo(ticket: String) extends AuthInfo
/**
* Base CAS provider.
*
* @see http://jasig.github.io/cas
* @see https://github.com/Jasig/cas
*/
trait BaseCasProvider extends SocialProvider with CasProviderConstants with Logger {
/**
* The content type to parse a profile from.
*/
override type Content = AttributePrincipal
/**
* The settings type.
*/
override type Settings = CasSettings
/**
* The type of the auth info.
*/
override type A = CasInfo
/**
* The provider ID.
*/
override val id: String = ID
/**
* The CAS client instance.
*/
val client: CasClient
/**
* Defines the URLs that are needed to retrieve the profile data.
*
* Since profile retrieval is internal to the Jasig CAS implementation, we don't need any further URLs ourselves.
*/
override protected val urls: Map[String, String] = Map.empty
/**
* Starts the authentication process.
*
* @param request The current request.
* @tparam B The type of the request body.
* @return Either a Result or the auth info from the provider.
*/
def authenticate[B]()(implicit request: ExtractableRequest[B]): Future[Either[Result, CasInfo]] = {
Future.successful {
request.extractString(CasClient.ServiceTicketParameter).map { ticket =>
Right(CasInfo(ticket))
}.getOrElse {
Left(Results.Redirect(client.redirectURL))
}
}
}
/**
* Populate the profile information from the service provider.
*
* @param authInfo The auth info received from the provider.
* @return On success the build social profile, otherwise a failure.
*/
override protected def buildProfile(authInfo: CasInfo): Future[Profile] = {
client.validateServiceTicket(authInfo.ticket).flatMap { principal =>
profileParser.parse(principal, authInfo)
}.transform(identity, { error =>
new ProfileRetrievalException(SpecifiedProfileError.format(id, error.getMessage))
})
}
}
/**
* The profile parser for the common social profile.
*/
class CasProfileParser
extends SocialProfileParser[AttributePrincipal, CommonSocialProfile, CasInfo]
with Logger
with CasProviderConstants {
/**
* Parses the CAS profile.
*
* @param principal The principal returned from the provider.
* @param authInfo The auth info to query the provider again for additional data.
* @return The CAS profile from given result.
*/
def parse(principal: AttributePrincipal, authInfo: CasInfo): Future[CommonSocialProfile] = Future.successful {
val attr = principal.getAttributes
logger.debug("AttributePrincipal, attributes:")
attr.asScala.foreach { case (key, value) => logger.debug("key: [%s], value: [%s]".format(key, value)) }
CommonSocialProfile(
LoginInfo(ID, principal.getName),
firstName = Option(attr.get(FirstName).asInstanceOf[String]),
lastName = Option(attr.get(LastName).asInstanceOf[String]),
email = Option(attr.get(Email).asInstanceOf[String]),
avatarURL = Option(attr.get(PictureURL).asInstanceOf[String]))
}
}
/**
* The CAS provider.
*
* @param httpLayer The HTTP layer implementation.
* @param settings The CAS provider settings.
* @param client The CAS client implementation.
*/
class CasProvider(
protected val httpLayer: HTTPLayer,
val settings: CasSettings,
val client: CasClient)
extends BaseCasProvider with CommonSocialProfileBuilder {
/**
* The type of this class.
*/
override type Self = CasProvider
/**
* The profile parser implementation.
*/
override val profileParser = new CasProfileParser
/**
* Gets a provider initialized with a new settings object.
*
* @param f A function which gets the settings passed and returns different settings.
* @return An instance of the provider initialized with new settings.
*/
override def withSettings(f: Settings => Settings): CasProvider = {
new CasProvider(httpLayer, f(settings), client.withSettings(f))
}
}
/**
* The [[CasProvider]] companion object.
*/
object CasProvider extends CasProviderConstants {
/**
* The error messages.
*/
val SpecifiedProfileError = "[Silhouette][%s] Error retrieving profile information. Error message: %s"
}
/**
* The CAS provider constants.
*/
trait CasProviderConstants {
val ID = "cas"
val Email = "email"
val FirstName = "first_name"
val LastName = "family_name"
val UserName = "username"
val PictureURL = "picture_url"
}
/**
* The CAS settings.
*
* @param casURL The URL of the CAS server.
* @param redirectURL The URL the CAS server will redirect to.
* @param encoding Specifies the encoding charset the client should use.
* @param acceptAnyProxy Accept any proxy in a chain of proxies.
* @param samlTimeTolerance Adjust to accommodate clock drift between client/server, increasing tolerance has security consequences.
* @param protocol The protocol supported by the CAS server @see [[CasProtocol]].
*/
final case class CasSettings(
casURL: String,
redirectURL: String,
encoding: String = "UTF-8",
acceptAnyProxy: Boolean = false,
samlTimeTolerance: FiniteDuration = 1000.millis,
protocol: CasProtocol.Value = CasProtocol.Default) {
import CasSettings._
/**
* Validates the CAS settings.
*/
if (isValidUrl(casURL))
throw new ConfigurationException(CasUrlInvalid.format(ID, casURL))
if (isValidUrl(redirectURL))
throw new ConfigurationException(RedirectUrlInvalid.format(ID, redirectURL))
if (encoding.isEmpty)
throw new ConfigurationException(EncodingInvalid.format(ID))
if (samlTimeTolerance.toMillis < 0)
throw new ConfigurationException(TimeToleranceInvalid.format(ID, samlTimeTolerance))
/**
* Validates the given URL.
*
* @param url The URL to validate.
* @return True if the URL is valid, false otherwise.
*/
private def isValidUrl(url: String): Boolean = Try(new URI(url).toURL).isFailure
}
/**
* The [[CasSettings]] companion object.
*/
object CasSettings {
/**
* The CAS error messages
*/
val CasUrlInvalid = "[Silhouette][%s] casURL setting [%s] is invalid"
val RedirectUrlInvalid = "[Silhouette][%s] redirectURL setting [%s] is invalid"
val EncodingInvalid = "[Silhouette][%s] encoding setting cannot be empty"
val TimeToleranceInvalid = "[Silhouette][%s] samlTimeTolerance setting [%s] must be positive"
}
/**
* The CAS client.
*
* @param settings The CAS settings.
*/
class CasClient(settings: CasSettings) extends Logger {
/**
* The CAS protocol.
*/
lazy val protocol: CasProtocol = CasProtocol(settings.protocol)
/**
* The CAS validator.
*/
lazy val validator: TicketValidator = protocol.ticketValidatorFactory(settings)
/**
* The redirect URL.
*
* Based on org.jasig.cas.client.util.CommonUtils, which causes a compilation error when pulled in.
*/
lazy val redirectURL: String = {
val svcParamName = protocol.protocol.getServiceParameterName
val cbURL = java.net.URLEncoder.encode(settings.redirectURL, settings.encoding)
s"${settings.casURL}${if (settings.casURL.contains("?")) "&" else "?"}$svcParamName=$cbURL"
}
/**
* Validates the service ticket returned by the CAS server.
*
* @param ticket The ticket returned from the CAS server.
* @param ec The current ExecutionContext.
* @return The attribute principal.
*/
def validateServiceTicket(ticket: String)(implicit ec: ExecutionContext): Future[AttributePrincipal] = Future {
validator.validate(ticket, settings.redirectURL).getPrincipal
}
/**
* Gets a client initialized with a new settings object.
*
* @param f A function which gets the settings passed and returns different settings.
* @return An instance of the client initialized with new settings.
*/
def withSettings(f: CasSettings => CasSettings): CasClient = new CasClient(f(settings))
}
/**
* CasClient companion object.
*/
object CasClient {
/**
* Constants
*/
val ServiceTicketParameter = "ticket"
}
/**
* The CAS protocol.
*/
final case class CasProtocol(protocol: Protocol, ticketValidatorFactory: CasSettings => TicketValidator)
/**
* CasProtocol companion object.
*
* Helper to convert a protocol ID into a [[CasProtocol]] instance.
*
* Allowable values:
*
* "CAS10", "CAS20", "CAS30", "SAML"
*
* Default "CAS30"
*/
object CasProtocol extends Enumeration {
val CAS10: providers.CasProtocol.Value = Value("CAS10")
val CAS20: impl.providers.CasProtocol.Value = Value("CAS20")
val CAS30: silhouette.impl.providers.CasProtocol.Value = Value("CAS30")
val SAML: play.silhouette.impl.providers.CasProtocol.Value = Value("SAML")
/**
* The default cas protocol.
*/
val Default: play.silhouette.impl.providers.CasProtocol.Value = CAS30
/**
* Creates a protocol based on the protocol.
*
* @param protocol The protocol for which the protocol should be created.
* @return The protocol instance for the given protocol ID.
*/
def apply(protocol: CasProtocol.Value): CasProtocol = protocol match {
case CasProtocol.CAS10 => new CasProtocol(Protocol.CAS1, casValidatorWithEncoding(new Cas10TicketValidator(_)))
case CasProtocol.CAS20 => new CasProtocol(Protocol.CAS2, casValidatorWithEncoding(new Cas20ServiceTicketValidator(_)))
case CasProtocol.CAS30 => new CasProtocol(Protocol.CAS3, casValidatorWithEncoding(new Cas30ServiceTicketValidator(_)))
case CasProtocol.SAML => new CasProtocol(Protocol.SAML11, { settings =>
val result = new Saml11TicketValidator(settings.casURL)
result.setTolerance(settings.samlTimeTolerance.toMillis)
result
})
case _ => new CasProtocol(Protocol.CAS3, casValidatorWithEncoding(new Cas30ServiceTicketValidator(_)))
}
/**
* A helper method which adds the encoding to the CAS validator.
*
* @param constructor The constructor of the validator.
* @tparam T The type of the CAS validator.
* @return The CAS validator with the set encoding.
*/
private def casValidatorWithEncoding[T <: AbstractUrlBasedTicketValidator](constructor: String => T) = {
(settings: CasSettings) =>
val result = constructor(settings.casURL)
result.setEncoding(settings.encoding)
result
}
}