Skip to content

Commit

Permalink
added micronaut view instead of crude html
Browse files Browse the repository at this point in the history
  • Loading branch information
odhekar committed Jun 4, 2021
1 parent 1ec1b8d commit a812b08
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 22 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ dependencies {
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut:micronaut-runtime")
implementation("javax.annotation:javax.annotation-api")
runtimeOnly("ch.qos.logback:logback-classic")
implementation("io.micronaut.views:micronaut-views-freemarker")
implementation("io.micronaut:micronaut-validation")

runtimeOnly("ch.qos.logback:logback-classic")
}


Expand Down
3 changes: 3 additions & 0 deletions helm/ambassador-jwt/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ spec:
- name: http
containerPort: 8080
protocol: TCP
env:
- name: TENANTID
value: {{ .Values.azure.tenantID }}
38 changes: 18 additions & 20 deletions src/main/java/com/odhekar/DefaultController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,39 @@
import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.authentication.Authentication;
import io.micronaut.security.rules.SecurityRule;
import io.micronaut.views.View;

import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;


@Controller
public class DefaultController {

@Secured(SecurityRule.IS_AUTHENTICATED)
@View("home")
@Get
@Produces(MediaType.TEXT_HTML)
public String getAuthenticated(HttpRequest<?> request, @Nullable Authentication authentication) {
var response = "<html><style>body {font-family: sans-serif;}</style><body>";
public HttpResponse<Map<String, Object>> getAuthenticatedView(HttpRequest<?> request,
@Nullable Authentication authentication) {
Map<String, Object> responseMap = new HashMap<>();
if (authentication != null) {
response += "<b>User</b>: " + authentication.getName() + " (" + authentication.getAttributes().get("email") + ")<p>";
responseMap.put("name", authentication.getName());
responseMap.put("email", authentication.getAttributes().get("email"));
}
if (request.getHeaders().get(HttpHeaders.AUTHORIZATION) != null) {
response += "<b>JWT</b><p><textarea rows=\"10\" cols=\"100\">" + getToken(request) +
"</textarea><p>";
response += "<b>Decoded JWT</b><pre>" + getDecodedToken(request) + "</pre><hr>";
responseMap.put("token", getToken(request));
responseMap.put("decodedToken", getDecodedToken(request));
}
response += "<b>Request Headers</b><pre>" + getHeaders(request) + "</pre></body></html>";
return response;
responseMap.put("headers", getHeaders(request));
return HttpResponse.ok(responseMap);
}

private String getHeaders(HttpRequest<?> request) {
Expand All @@ -54,30 +56,26 @@ private String getHeaders(HttpRequest<?> request) {
}

private String getToken(HttpRequest<?> request) {
String token = request.getHeaders().get(HttpHeaders.AUTHORIZATION).split("Bearer ")[1];
String[] tokenParts = token.split("\\.");
return tokenParts[1];
return request.getHeaders().get(HttpHeaders.AUTHORIZATION).split("Bearer ")[1];
}

private String getDecodedToken(HttpRequest<?> request) {
byte[] token = Base64.getDecoder().decode(getToken(request));
String[] tokenParts = getToken(request).split("\\.");
byte[] token = Base64.getDecoder().decode(tokenParts[1]);
JsonNode jwt = null;
try {
jwt = new ObjectMapper().readTree(token);
} catch (IOException e) {
e.printStackTrace();
}
return prettyJson(jwt);
}

private String prettyJson(JsonNode node) {
try {
return new ObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT)
.writeValueAsString(node);
.writeValueAsString(jwt);
} catch (JsonProcessingException e) {
return e.getMessage();
}
}


}
68 changes: 68 additions & 0 deletions src/main/resources/views/home.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html>

<head>
<link rel="icon" type="image/png"
href="data:image/png;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAQAQAABMLAAATCw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TeJaWE3/WlhN/1pYTcdaWE02WlhNKlpYTb9aWE3/WlhN/wAAAABaWE3/WlhN61pYTWMAAAAAAAAAAAAAAABaWE0MWlhNgVpYTVFaWE0VWlhNlVpYTftaWE3/WlhN/1pYTf8AAAAAWlhNjVpYTRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABaWE0BWlhNllpYTf5aWE3/WlhN/1pYTf9aWE2pAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABaWE0/WlhNzlpYTc9aWE1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/D8SivgPAADIAwAAiAEAAIgBAACIAQAAiBEAAIgRAACIEQAAiBEAAIARCn6AEQAAgBEAAMATAADwHwAA/D8AAA==">
<title>Authentication</title>
<style>
body {
font-family: sans-serif;
font-size: 1rem;
margin: 25px auto;
max-width: 70%;
}
.panel {
margin: 25px auto;
border: 1px solid #C5C5C5;
border-radius: 2px;
}
.panel-head {
background-color: #F5F5F5;
padding: 0.7rem;
}
.panel-body {
padding: 0.7rem;
font-family: Cascadia Code, Consolas, monospace;
font-size: 0.9rem;
line-height: 150%;
overflow: auto;
white-space: pre;
scrollbar-width: none;
}
.panel-body::-webkit-scrollbar {
display: none;
}
.panel-jwt {
word-wrap: break-word;
white-space: pre-wrap;
}
</style>
</head>

<body>
<#if name??>
<div class="panel">
<div class="panel-head">User</button></div>
<div class="panel-body">${name} (${email})</div>
</div>
<div class="panel">
<div class="panel-head">JWT</div>
<div class="panel-body panel-jwt">${token} </div>
</div>
<div class="panel">
<div class="panel-head">Decoded JWT</div>
<div class="panel-body">${decodedToken}</div>
</div>
</#if>
<div class="panel">
<div class="panel-head">Headers</div>
<div class="panel-body">${headers}</div>
</div>
</body>

</html>

0 comments on commit a812b08

Please sign in to comment.