1
1
package au.org.ala.spatial
2
2
3
- import au.org.ala.spatial.RequireAdmin
4
- import au.org.ala.spatial.RequireLogin
5
- import au.org.ala.spatial.RequirePermission
6
- import au.org.ala.spatial.SkipSecurityCheck
7
- import au.org.ala.spatial.SpatialConfig
8
3
import au.org.ala.web.AuthService
9
4
import com.google.common.base.Strings
10
5
import grails.converters.JSON
11
6
12
- /**
13
- * Copy and simplify from plugin "ala-ws-security-plugin:2.0"
14
- * Remove IP/method/controller whitelist support
15
- * collecting api_key from POST body (backward compatiblity support)
16
- *
17
- * TODO use ALA standard apiKey method: store apiKey in hearder
18
- */
19
- import org.grails.web.util.WebUtils
20
-
21
- import java.text.MessageFormat
22
-
23
7
class LoginInterceptor {
24
8
static final int STATUS_UNAUTHORISED = 401
25
9
static final int STATUS_FORBIDDEN = 403
26
- static final String [] USERID_HEADER_NAME = [" X-ALA-userId" , " userId" , " user_id" ]
27
- static final String [] API_KEY_HEADER_NAME = [" apiKey" , " api_key" , " api-key" ]
28
10
29
11
int order = LOWEST_PRECEDENCE
30
12
@@ -42,8 +24,6 @@ class LoginInterceptor {
42
24
return true
43
25
}
44
26
45
- def isAdmin = spatialAuthService. userInRole(spatialConfig. auth. admin_role)
46
-
47
27
def controller = grailsApplication. getArtefactByLogicalPropertyName(" Controller" , controllerName)
48
28
Class controllerClass = controller?. clazz
49
29
def method = controllerClass?. getMethod(actionName ?: " index" , [] as Class [])
@@ -55,18 +35,14 @@ class LoginInterceptor {
55
35
// Calculating the required permission.
56
36
def permissionLevel = null
57
37
// Permission on method has the top priority
58
- if (method?. isAnnotationPresent(RequirePermission . class)) {
59
- permissionLevel = RequirePermission
60
- } else if (method?. isAnnotationPresent(RequireLogin . class)) {
38
+ if (method?. isAnnotationPresent(RequireLogin . class)) {
61
39
permissionLevel = RequireLogin
62
40
} else if (method?. isAnnotationPresent(RequireAdmin . class)) {
63
41
permissionLevel = RequireAdmin
64
42
}
65
43
66
44
if (Objects . isNull(permissionLevel)) {
67
- if (controllerClass?. isAnnotationPresent(RequirePermission . class)) {
68
- permissionLevel = RequirePermission
69
- } else if (controllerClass?. isAnnotationPresent(RequireLogin . class)) {
45
+ if (controllerClass?. isAnnotationPresent(RequireLogin . class)) {
70
46
permissionLevel = RequireLogin
71
47
} else if (controllerClass?. isAnnotationPresent(RequireAdmin . class)) {
72
48
permissionLevel = RequireAdmin
@@ -75,15 +51,7 @@ class LoginInterceptor {
75
51
76
52
// Permission check
77
53
def role // if require a certain level of ROLE
78
- if (hasValidApiKey()) {
79
- return true
80
- } else if (permissionLevel == RequirePermission ) {
81
- if (authService. getUserId()) {
82
- return true
83
- } else {
84
- return accessDenied(STATUS_UNAUTHORISED , ' Forbidden, ApiKey or user login required!' )
85
- }
86
- } else if (permissionLevel == RequireAdmin ) {
54
+ if (permissionLevel == RequireAdmin ) {
87
55
role = spatialConfig. auth. admin_role
88
56
} else if (permissionLevel == RequireLogin ) {
89
57
RequireLogin requireAuthentication = method. getAnnotation(RequireLogin . class)
@@ -126,44 +94,4 @@ class LoginInterceptor {
126
94
return false
127
95
}
128
96
}
129
-
130
- def hasValidApiKey () {
131
- isValid(getApiKey())
132
- }
133
-
134
- def isValid (key ) {
135
- if (key == null ) {
136
- return false
137
- }
138
-
139
- Boolean result = testedKeys. get(key)
140
-
141
- if (result == null ) {
142
- String url = MessageFormat . format(grailsApplication. config. apiKeyCheckUrlTemplate. toString(), key)
143
-
144
- result = key == grailsApplication. config. serviceKey || Util . getUrl(url). contains(' "valid":true' )
145
- testedKeys. put(key, result)
146
- }
147
-
148
- return result
149
- }
150
-
151
- private getApiKey () {
152
- String apikey
153
- def request = WebUtils . retrieveGrailsWebRequest(). getCurrentRequest()
154
-
155
- for (name in API_KEY_HEADER_NAME ) {
156
- if (request. getHeader(name)) {
157
- return request. getHeader(name). split(" ,|;" )[0 ]
158
- }
159
- if (request. getParameter(name)) {
160
- return request. getParameter(name). split(" ,|;" )[0 ]
161
- }
162
- }
163
-
164
- // Last try
165
- apikey = request. JSON ?. api_key
166
-
167
- apikey
168
- }
169
97
}
0 commit comments