Skip to content

Commit

Permalink
Fixes token requirement logic (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
StrongestNumber9 authored Apr 4, 2024
1 parent 0f5f174 commit c0ecaf2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion etc/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ relp.appName=lsh_01
relp.hostname=localhost

security.tokenRequired=true
security.token=SomeSecretToken
security.token=SomeSecretToken
1 change: 1 addition & 0 deletions src/main/java/com/teragrep/lsh_01/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static void main(String[] args) {
}
LOGGER.info("Got server config: <[{}]>", nettyConfig);
LOGGER.info("Got relp config: <[{}]>", relpConfig);
LOGGER.info("Requires token: <[{}]>", securityConfig.tokenRequired);
RelpConversion relpConversion = new RelpConversion(relpConfig, securityConfig);
try (NettyHttpServer server = new NettyHttpServer(nettyConfig, relpConversion, null, 200)) {
server.run();
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/com/teragrep/lsh_01/MessageProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,25 @@ public void onRejection() {
public void run() {
try {
final HttpResponse response;
if (messageHandler.requiresToken() && !req.headers().contains(HttpHeaderNames.AUTHORIZATION)) {
LOGGER.debug("Required authorization not provided; requesting authentication.");
response = generateAuthenticationRequestResponse();
if (!messageHandler.requiresToken()) {
response = processMessage();
}
else {
final String token = req.headers().get(HttpHeaderNames.AUTHORIZATION);
req.headers().remove(HttpHeaderNames.AUTHORIZATION);
if (messageHandler.validatesToken(token)) {
LOGGER.debug("Valid authorization; processing request.");
response = processMessage();
if (!req.headers().contains(HttpHeaderNames.AUTHORIZATION)) {
LOGGER.debug("Required authorization not provided; requesting authentication.");
response = generateAuthenticationRequestResponse();
}
else {
LOGGER.debug("Invalid authorization; rejecting request.");
response = generateFailedResponse(HttpResponseStatus.UNAUTHORIZED);
final String token = req.headers().get(HttpHeaderNames.AUTHORIZATION);
req.headers().remove(HttpHeaderNames.AUTHORIZATION);
if (messageHandler.validatesToken(token)) {
LOGGER.debug("Valid authorization; processing request.");
response = processMessage();
}
else {
LOGGER.debug("Invalid authorization; rejecting request.");
response = generateFailedResponse(HttpResponseStatus.UNAUTHORIZED);
}
}
}
ctx.writeAndFlush(response);
Expand Down

0 comments on commit c0ecaf2

Please sign in to comment.