Skip to content

Commit b785576

Browse files
author
Adam Collins
committed
#467 initial api key removal
1 parent 66a3133 commit b785576

File tree

6 files changed

+28
-66
lines changed

6 files changed

+28
-66
lines changed

README.md

-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ The dependent services point to other production servers by default
1919

2020
### Minimum configurations in external config file:
2121

22-
api_key: xxxxxxxxx-d184-4276-afc2-6ece17176d7c
2322
google:
2423
apikey: xxxxxxxx
2524

@@ -33,8 +32,6 @@ The dependent services point to other production servers by default
3332
google:
3433
apikey: "xxxxxxxxxxxxxx"
3534

36-
api_key: xxxxxxxxxxxxxxxxx
37-
3835
layersService:
3936
url: "https://spatial-test.ala.org.au/ws"
4037

grails-app/conf/application.yml

-3
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,6 @@ cache:
319319
phylolink:
320320
url: 'https://phylolink.ala.org.au'
321321

322-
apiKeyCheckUrlTemplate: 'https://auth.ala.org.au/apikey/ws/check?apikey={0}'
323-
api_key: 'change me'
324-
325322
character:
326323
encoding: UTF-8
327324

grails-app/controllers/au/org/ala/spatial/portal/CollectionController.groovy

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
package au.org.ala.spatial.portal
22

33
import grails.converters.JSON
4-
import groovy.json.JsonSlurper
5-
import org.apache.http.client.methods.HttpGet
4+
import org.apache.http.entity.ContentType
65

76
class CollectionController {
8-
def hubWebService
7+
def webService
98

9+
// used to find the data resources for deprecated sandbox uploads
1010
def list() {
1111
String url = "${grailsApplication.config.collections.url}/ws/tempDataResource?alaId=" + params.alaId
12-
def headers = [:]
13-
headers.put ("apiKey",grailsApplication.config.api_key)
14-
request.headerNames.each { name -> headers.put(name, request.getHeader(name)) }
15-
def r = hubWebService.urlResponse(HttpGet.METHOD_NAME, url, null, headers,
16-
null, true)
12+
def r = webService.get(url, [:], ContentType.APPLICATION_JSON, false, true, [:])
1713
if (r.statusCode == 200) {
18-
def parser = new JsonSlurper()
19-
def json = parser.parseText(r.text)
20-
render json as JSON
14+
render r.resp as JSON
2115
} else {
2216
def result = [error: 'Cannot fetch list from: ' + url]
2317
render result as JSON

grails-app/controllers/au/org/ala/spatial/portal/LogController.groovy

+11-12
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import grails.converters.JSON
44
import org.apache.commons.httpclient.methods.StringRequestEntity
55
import org.apache.http.client.methods.HttpPost
66
import org.apache.http.client.methods.HttpGet
7+
import org.apache.http.entity.ContentType
78

89
class LogController {
9-
def hubWebService
10-
def authService
10+
def webService
1111
static allowedMethods = [index:'POST', search:'GET']
1212

1313
/**
@@ -16,21 +16,20 @@ class LogController {
1616
*/
1717
def index() {
1818
String url = "${grailsApplication.config.layersService.url}/log/"
19-
def headers = [:]
20-
headers.put ("apiKey",grailsApplication.config.api_key)
21-
request.headerNames.each { name -> headers.put(name, request.getHeader(name)) }
22-
def r = hubWebService.urlResponse(HttpPost.METHOD_NAME, url, null, headers,
23-
new StringRequestEntity(request.JSON.toString()), true)
19+
20+
Map map = [data: (request.JSON as Map)]
21+
def r = webService.post(url, map, null, ContentType.APPLICATION_JSON, false, true)
22+
2423
render status: r.statusCode
2524
}
2625

2726
def search() {
2827
String url = "${grailsApplication.config.layersService.url}/log/search"
29-
def headers = [:]
30-
headers.put("Accept", "application/json")
31-
headers.put ("apiKey",grailsApplication.config.api_key)
32-
def r = hubWebService.urlResponse(HttpGet.METHOD_NAME, url, params, headers, null, true)
28+
29+
Map inputs = params as Map
30+
def r = webService.get(url, inputs, ContentType.APPLICATION_JSON, false, true)
31+
3332
response.status = r.statusCode
34-
render JSON.parse(new String(r?.text ?: "")) as JSON
33+
render r.resp as JSON
3534
}
3635
}

grails-app/controllers/au/org/ala/spatial/portal/PortalController.groovy

+12-17
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,10 @@ class PortalController {
198198
* null when not logged in
199199
*/
200200
private def getValidUserId(params) {
201-
//apiKey + userId (non-numeric) OR authenticated user
201+
// find the authenticated user, or the default user
202202
def userId
203203
if (!Holders.config.security.oidc.enabled) {
204204
userId = portalService.DEFAULT_USER_ID
205-
} else if (portalService.isValidApiKey(params.apiKey) && !StringUtils.isNumeric(params.userId)) {
206-
userId = params.userId
207205
} else {
208206
userId = authService.userId
209207
}
@@ -290,26 +288,29 @@ class PortalController {
290288
notAuthorised()
291289
} else {
292290
def type = id
291+
292+
// write the file to disk
293293
MultipartFile mFile = ((MultipartHttpServletRequest) request).getFile('shapeFile')
294-
def settings = [apiKey: grailsApplication.config.api_key]
295294

296295
String ce = grailsApplication.config.character.encoding
297296

298-
def r = hubWebService.postUrl("${grailsApplication.config.layersService.url}/shape/upload/${type}?" +
297+
String url = "${grailsApplication.config.layersService.url}/shape/upload/${type}?" +
299298
"name=${URLEncoder.encode((String) params.name, ce)}&" +
300-
"description=${URLEncoder.encode((String) params.description, ce)}&" +
301-
"api_key=${grailsApplication.config.api_key}", null, settings, mFile);
299+
"description=${URLEncoder.encode((String) params.description, ce)}"
300+
301+
List files = [mFile]
302+
def r = webService.post(url, null, null, files, ContentType.MULTIPART_FORM_DATA, false, true)
302303

303304
if (!r) {
304305
render [:] as JSON
305306
} else if (r.error || r.statusCode > 299) {
306307
log.error("failed ${type} upload: ${r}")
307-
def msg = JSON.parse(new String(r?.text ?: "{}"))
308+
def msg = r.resp
308309
Map error = [error: msg.error]
309310
response.status = r.statusCode
310311
render error as JSON
311312
} else {
312-
def json = JSON.parse(new String(r?.text ?: "{}"))
313+
def json = r.resp
313314
def shapeFileId = json.id
314315
def area = json.collect { key, value ->
315316
if (key == 'shp_id') {
@@ -352,14 +353,8 @@ class PortalController {
352353
} else {
353354
def json = request.JSON as Map
354355

355-
Map params = [sessionId: params.sessionId]
356-
for (def key : json.keySet()) {
357-
if (key != 'sessionId') {
358-
params.put(key, String.valueOf(json[key]))
359-
}
360-
}
361-
362-
def r = webService.post("${grailsApplication.config.layersService.url}/tasks/create", null, params, ContentType.APPLICATION_JSON, false, true)
356+
String url = "${grailsApplication.config.layersService.url}/tasks/create?userId=${userId}&sessionId=${params.sessionId}"
357+
def r = webService.post(url, json, null, ContentType.APPLICATION_JSON, false, true)
363358

364359
if (r == null) {
365360
render [:] as JSON

grails-app/services/au/org/ala/spatial/portal/PortalService.groovy

-20
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,6 @@ class PortalService {
118118

119119
def validKeys = [] as Set
120120

121-
def isValidApiKey(key) {
122-
if (key == null) {
123-
return false
124-
}
125-
126-
Boolean result = validKeys.contains(key)
127-
128-
if (result == null) {
129-
String url = MessageFormat.format(grailsApplication.config.apiKeyCheckUrlTemplate.toString(), key)
130-
131-
result = key == grailsApplication.config.serviceKey || hubWebService.getUrl(url).contains('"valid":true')
132-
133-
if (result) {
134-
validKeys.add(key)
135-
}
136-
}
137-
138-
return result
139-
}
140-
141121
def canProxy(url) {
142122
def predefined = url.toString().startsWith(Holders.config.layersService.url) ||
143123
url.toString().startsWith(Holders.config.phylolink.url) ||

0 commit comments

Comments
 (0)