Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.gradle | ||
.idea | ||
build | ||
.gradle/ | ||
.idea/ | ||
build/ | ||
out/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
plugins { | ||
application | ||
} | ||
|
||
dependencies { | ||
arrayOf("annotationProcessor", "implementation", "testAnnotationProcessor").forEach { | ||
add(it, platform("io.micronaut:micronaut-bom:1.2.6")) | ||
} | ||
annotationProcessor("io.micronaut:micronaut-inject-java") | ||
implementation(project(":bearerauth-core")) | ||
implementation("io.micronaut:micronaut-http-server-netty") | ||
implementation("io.micronaut:micronaut-inject") | ||
runtimeOnly("ch.qos.logback:logback-classic:1.2.3") | ||
testAnnotationProcessor("io.micronaut:micronaut-inject-java") | ||
testImplementation("io.micronaut.test:micronaut-test-junit5") | ||
testImplementation("org.junit.jupiter:junit-jupiter-api") | ||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") | ||
} | ||
|
||
application { | ||
mainClassName = "sample.SampleMicronautApplication" | ||
} |
71 changes: 71 additions & 0 deletions
71
samples/sample-micronaut/src/main/java/sample/MicronautBearerAuthenticationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package sample; | ||
|
||
import io.github.vpavic.bearerauth.AuthorizationContext; | ||
import io.github.vpavic.bearerauth.AuthorizationContextResolver; | ||
import io.github.vpavic.bearerauth.BearerAuthenticationHandler; | ||
import io.github.vpavic.bearerauth.HttpExchange; | ||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.http.MutableHttpResponse; | ||
import io.micronaut.http.annotation.Filter; | ||
import io.micronaut.http.filter.OncePerRequestHttpServerFilter; | ||
import io.micronaut.http.filter.ServerFilterChain; | ||
import io.reactivex.Completable; | ||
import org.reactivestreams.Publisher; | ||
|
||
import java.time.Instant; | ||
import java.util.Collections; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Filter("/**") | ||
public class MicronautBearerAuthenticationFilter extends OncePerRequestHttpServerFilter { | ||
|
||
private final BearerAuthenticationHandler bearerAuthenticationHandler; | ||
|
||
public MicronautBearerAuthenticationFilter() { | ||
this.bearerAuthenticationHandler = BearerAuthenticationHandler.builder( | ||
bearerToken -> authorizationContextResolver().apply(bearerToken)).build(); | ||
} | ||
|
||
private AuthorizationContextResolver authorizationContextResolver() { | ||
return bearerToken -> { | ||
switch (bearerToken.toString()) { | ||
case "valid": | ||
return CompletableFuture.completedFuture( | ||
new AuthorizationContext(Collections.emptySet(), Instant.MAX, Collections.emptyMap())); | ||
case "expired": | ||
return CompletableFuture.completedFuture( | ||
new AuthorizationContext(Collections.emptySet(), Instant.MIN, Collections.emptyMap())); | ||
default: | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Publisher<MutableHttpResponse<?>> doFilterOnce(HttpRequest<?> request, ServerFilterChain chain) { | ||
return Completable.fromFuture( | ||
this.bearerAuthenticationHandler.handle(new MicronautHttpExchange(request)).toCompletableFuture()) | ||
.andThen(chain.proceed(request)); | ||
} | ||
|
||
private static class MicronautHttpExchange implements HttpExchange { | ||
|
||
private final HttpRequest<?> httpRequest; | ||
|
||
private MicronautHttpExchange(HttpRequest<?> httpRequest) { | ||
this.httpRequest = httpRequest; | ||
} | ||
|
||
@Override | ||
public String getRequestHeader(String headerName) { | ||
return this.httpRequest.getHeaders().get(headerName); | ||
} | ||
|
||
@Override | ||
public void setAttribute(String attributeName, Object attributeValue) { | ||
this.httpRequest.setAttribute(attributeName, attributeValue); | ||
} | ||
|
||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
samples/sample-micronaut/src/main/java/sample/SampleMicronautApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package sample; | ||
|
||
import io.micronaut.runtime.Micronaut; | ||
|
||
public class SampleMicronautApplication { | ||
|
||
public static void main(String[] args) { | ||
Micronaut.run(SampleMicronautApplication.class, args); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
samples/sample-micronaut/src/main/java/sample/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package sample; | ||
|
||
import io.github.vpavic.bearerauth.AuthorizationContext; | ||
import io.github.vpavic.bearerauth.BearerAuthenticationHandler; | ||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.http.MediaType; | ||
import io.micronaut.http.annotation.Controller; | ||
import io.micronaut.http.annotation.Get; | ||
|
||
@Controller("/resource") | ||
public class TestController { | ||
|
||
@Get(produces = MediaType.TEXT_PLAIN) | ||
public String greet(HttpRequest<?> request) { | ||
request.getAttribute(BearerAuthenticationHandler.AUTHORIZATION_CONTEXT_ATTRIBUTE, AuthorizationContext.class) | ||
.ifPresent(authorizationContext -> System.out.println("authorizationContext{scope=" | ||
+ String.join(",", authorizationContext.getScope()) + ",expiry=" | ||
+ authorizationContext.getExpiry() + "}")); | ||
return "Hello World!"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
</configuration> |
13 changes: 13 additions & 0 deletions
13
samples/sample-micronaut/src/test/java/sample/SampleMicronautApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package sample; | ||
|
||
import io.micronaut.test.annotation.MicronautTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@MicronautTest | ||
class SampleMicronautApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters