Skip to content

Commit 112ca6d

Browse files
authored
Merge pull request #6 from CodeCafeOpenShiftGame/whoareyou
Adding basic auth checking to POST to the API service
2 parents 44ed805 + ef1820c commit 112ca6d

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ Expose access to outside of cluster
8080
For more info on this way of deploying (and alternatives) [see the docs here](https://quarkus.io/guides/deploying-to-openshift-s2i).
8181

8282
## Other Things
83+
## The POST route for scores now has basic auth enabled on it
84+
* So you will need to pass username/password in to POST scores
85+
* You can disable it with an environment vairable (see the application.properties file)
86+
8387
## Running a MongoDB in OpenShift
8488
This service won't function until it can store its data into a MongoDB. We can easily deploy one on OpenShift and have OpenShift provide service discovery. And then we configure this app's deployment with the user/password details for connecting to the DB.
8589
> `oc new-app -e MONGODB_USER=thisisauser -e MONGODB_PASSWORD=thisis4password -e MONGODB_DATABASE=highscores -e MONGODB_ADMIN_PASSWORD=thisis4password mongodb:latest`
@@ -90,5 +94,15 @@ This service won't function until it can store its data into a MongoDB. We can e
9094
## Hooking in 3scale API Management
9195
TBD - [3scale ref here](https://access.redhat.com/documentation/en-us/red_hat_3scale_api_management/2.7/html/providing_apis_in_the_developer_portal/create-new-service-openapi-specification#using_openapi_specification)
9296

97+
98+
## Testing POSTS with HTTPie
99+
I like to use a nice CLI tool called HTTPie. If you have it below are some useful commands.
100+
101+
### Testing POST with basic auth turned on
102+
```
103+
http -a dudash:123456 POST http://localhost:5000/scores score=1000 name=JAS
104+
```
105+
106+
93107
## Thanks and Credit
94108
This service was built based on guidance from the [Quarkus example here](https://quarkus.io/guides/openapi-swaggerui#loading-openapi-schema-from-static-files).

src/main/java/io/nub3s/ScoresResource.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
package io.nub3s;
22

3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Base64;
35
import io.vertx.axle.core.eventbus.EventBus;
4-
import io.vertx.axle.core.eventbus.Message;
56

67
import javax.inject.Inject;
78
import javax.ws.rs.Consumes;
89
import javax.ws.rs.GET;
10+
import javax.ws.rs.HeaderParam;
911
import javax.ws.rs.POST;
1012
import javax.ws.rs.Path;
1113
import javax.ws.rs.Produces;
1214
import javax.ws.rs.core.Response;
15+
16+
import org.eclipse.microprofile.config.inject.ConfigProperty;
17+
1318
import java.util.List;
1419

1520
@Path("/scores")
1621
public class ScoresResource {
1722

23+
@ConfigProperty(name = "quickauthenforcing", defaultValue = "true")
24+
protected boolean quickAuthEnforcing;
25+
@ConfigProperty(name = "quickauthuser", defaultValue = "true")
26+
protected String quickAuthUser;
27+
@ConfigProperty(name = "quickauthpassword", defaultValue = "true")
28+
protected String quickAuthPassword;
29+
1830
@Inject EventBus bus;
1931

2032
@GET
@@ -25,7 +37,20 @@ public List<Score> list(){
2537

2638
@POST
2739
@Consumes("application/json")
28-
public Response create(Score score) {
40+
public Response create(@HeaderParam("Authorization") String authorization, Score score) {
41+
if (quickAuthEnforcing) {
42+
if (authorization == null) return Response.status(401).build();
43+
if (!authorization.toLowerCase().startsWith("basic")) return Response.status(401).build();
44+
String base64string = authorization.substring("Basic".length()).trim();
45+
byte[] bytes = Base64.getDecoder().decode(base64string);
46+
String credentials = new String(bytes, StandardCharsets.UTF_8);
47+
final String[] keyValueCredentials = credentials.split(":", 2);
48+
if (keyValueCredentials[0].compareTo(quickAuthUser)!=0) return Response.status(401).build();
49+
if (keyValueCredentials[1].compareTo(quickAuthPassword)!=0) return Response.status(401).build();
50+
}
51+
else {
52+
System.out.println("ignoring auth");
53+
}
2954
score.persist();
3055
bus.publish("newscore", score.toString()); // tell NotifcationsWebSocket to broadcast an update
3156
bus.publish("topten", topTenList().toString()); // tell NotifcationsWebSocket to broadcast an update

src/main/resources/application.properties

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
quarkus.http.port=8080
33
%dev.quarkus.http.port=5000
44

5+
# Security
6+
%dev.quickauthenforcing=false
7+
quickauthenforcing=${QUICKAUTH_ENFORCING:true}
8+
quickauthuser=${QUICKAUTH_USER:dudash}
9+
quickauthpassword=${QUICKAUTH_PASSWORD:123456}
10+
# TODO: Auth should be done with an IdentityProvider and vars below
11+
# quarkus.http.auth.basic=true
12+
# quarkus.http.auth.permission.api-permission-check1.paths=/scores
13+
# quarkus.http.auth.permission.api-permission-check1.policy=authenticated
14+
# quarkus.http.auth.permission.api-permission-check1.methods=POST
15+
516
# API Stuff
617
# if doing a demo app, turn this on - make default off for security reasons
718
quarkus.swagger-ui.always-include=false

0 commit comments

Comments
 (0)