/
ScentryStrategy.scala
86 lines (67 loc) · 3.01 KB
/
ScentryStrategy.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
package org.scalatra
package auth
import org.scalatra.ServletCompat.http.{ HttpServletRequest, HttpServletResponse }
trait ScentryStrategy[UserType <: AnyRef] {
protected def app: ScalatraBase
def name: String = this.getClass.getName
def registerWith(registrar: Scentry[UserType]): Unit = {
if (name == "NameMe") throwOverrideException
else registrar.register(name, createStrategy _)
}
def createStrategy(app: ScalatraBase): this.type = {
throwOverrideException
}
private def throwOverrideException = {
throw new RuntimeException("This method is used when configuring strategies through web.xml.\n" +
"If you want to use this registration method you have to override createStrategy and name in your strategy.\n" +
"Your strategy also needs to have a parameterless constructor for it to be used through web.xml")
}
/**
* Indicates if this strategy should be run.
*
* @return a Boolean to indicate validity
*/
def isValid(implicit request: HttpServletRequest) = true
/**
* Perform the authentication for this strategy
*
* @return a UserType option where None indicates auth failure
*/
def authenticate()(implicit request: HttpServletRequest, response: HttpServletResponse): Option[UserType]
/**
* Perform stuff before authenticating, only run when the module is valid
*/
def beforeAuthenticate(implicit request: HttpServletRequest, response: HttpServletResponse): Unit = {}
/**
* Perform stuff after authentication only run when the module is valid
*/
def afterAuthenticate(winningStrategy: String, user: UserType)(implicit request: HttpServletRequest, response: HttpServletResponse): Unit = {}
/**
* Perform stuff before setting the user in the session
*/
def beforeSetUser(user: UserType)(implicit request: HttpServletRequest, response: HttpServletResponse): Unit = {}
/**
* Perform stuff after setting the user in the session
*/
def afterSetUser(user: UserType)(implicit request: HttpServletRequest, response: HttpServletResponse): Unit = {}
/**
* Perform stuff before fetching and serializing the user from session
*/
def beforeFetch[IdType](userId: IdType)(implicit request: HttpServletRequest, response: HttpServletResponse): Unit = {}
/**
* Perform stuff after fetching and serializing the user from session
*/
def afterFetch(user: UserType)(implicit request: HttpServletRequest, response: HttpServletResponse): Unit = {}
/**
* Perform stuff before logging the user out and invalidating the session
*/
def beforeLogout(user: UserType)(implicit request: HttpServletRequest, response: HttpServletResponse): Unit = {}
/**
* Perform stuff after logging the user out and invalidating the session
*/
def afterLogout(user: UserType)(implicit request: HttpServletRequest, response: HttpServletResponse): Unit = {}
/**
* Perform stuff when the request is unauthenticated and the strategy is valid
*/
def unauthenticated()(implicit request: HttpServletRequest, response: HttpServletResponse): Unit = {}
}