Skip to content

Commit e250164

Browse files
committed
Return the certificates in a list instead of an array. Added documentation.
1 parent 80ca6ec commit e250164

File tree

7 files changed

+44
-12
lines changed

7 files changed

+44
-12
lines changed

docs/asciidoc/context.adoc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,33 @@ get("/{foo}") { ctx ->
659659
In case of a request like `/bar?foo=baz`, `foo is: baz` will be returned since the query parameter
660660
takes precedence over the path parameter.
661661

662+
==== Client Certificates
663+
664+
If mutual TLS is enabled, you can access the client's certificates from the context. The first
665+
certificate in the list is the peer certificate, followed by the ca certificates in the chain
666+
(the order is preserved).
667+
668+
.Java
669+
[source,java,role="primary"]
670+
----
671+
get("/{foo}", ctx -> {
672+
List<Certificate> certificates = ctx.getClientCertificates(); <1>
673+
Certificate peerCertificate = certificates.get(0); <2>
674+
});
675+
----
676+
677+
.Kotlin
678+
[source,kotlin,role="secondary"]
679+
----
680+
get("/{foo}") { ctx ->
681+
val certificates = ctx.clientCertificates <1>
682+
val peerCertificate = certificates.first() <2>
683+
}
684+
----
685+
686+
<1> Get all of the certificates presented by the client during the SSL handshake.
687+
<2> Get only the peer certificate.
688+
662689
include::value-api.adoc[]
663690

664691
include::body.adoc[]

jooby/src/main/java/io/jooby/Context.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ public interface Context extends Registry {
633633
*
634634
* @return The certificates presented by the client for mutual TLS. Empty if ssl is not enabled, or client authentication is not required.
635635
*/
636-
@Nonnull Certificate[] getClientCertificates();
636+
@Nonnull List<Certificate> getClientCertificates();
637637

638638
/**
639639
* Server port for current request.

jooby/src/main/java/io/jooby/ForwardingContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public ForwardingContext(@Nonnull Context context) {
268268
return ctx.getProtocol();
269269
}
270270

271-
@Override @Nonnull public Certificate[] getClientCertificates() {
271+
@Override @Nonnull public List<Certificate> getClientCertificates() {
272272
return ctx.getClientCertificates();
273273
}
274274

modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import java.nio.charset.Charset;
6262
import java.security.cert.Certificate;
6363
import java.util.ArrayList;
64+
import java.util.Arrays;
6465
import java.util.Collection;
6566
import java.util.Collections;
6667
import java.util.Enumeration;
@@ -282,8 +283,8 @@ public JettyContext(Request request, Router router, int bufferSize, long maxRequ
282283
return request.getProtocol();
283284
}
284285

285-
@Nonnull @Override public Certificate[] getClientCertificates() {
286-
return (Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");
286+
@Nonnull @Override public List<Certificate> getClientCertificates() {
287+
return Arrays.asList((Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate"));
287288
}
288289

289290
@Nonnull @Override public String getScheme() {

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.nio.charset.Charset;
3333
import java.security.cert.Certificate;
3434
import java.util.ArrayList;
35+
import java.util.Arrays;
3536
import java.util.Collection;
3637
import java.util.Collections;
3738
import java.util.HashMap;
@@ -288,16 +289,16 @@ boolean isHttpGet() {
288289
}
289290
}
290291

291-
@Nonnull @Override public Certificate[] getClientCertificates() {
292+
@Nonnull @Override public List<Certificate> getClientCertificates() {
292293
SslHandler sslHandler = (SslHandler) ctx.channel().pipeline().get("ssl");
293294
if (sslHandler != null) {
294295
try {
295-
return sslHandler.engine().getSession().getPeerCertificates();
296+
return Arrays.asList(sslHandler.engine().getSession().getPeerCertificates());
296297
} catch (SSLPeerUnverifiedException x) {
297298
throw SneakyThrows.propagate(x);
298299
}
299300
}
300-
return new Certificate[0];
301+
return new ArrayList<Certificate>();
301302
}
302303

303304
@Nonnull @Override public String getScheme() {

modules/jooby-test/src/main/java/io/jooby/MockContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,8 @@ public MockContext setResponseType(@Nonnull MediaType contentType, @Nullable Cha
541541
return "HTTP/1.1";
542542
}
543543

544-
@Nonnull @Override public Certificate[] getClientCertificates() {
545-
return new Certificate[0];
544+
@Nonnull @Override public List<Certificate> getClientCertificates() {
545+
return new ArrayList<Certificate>();
546546
}
547547

548548
@Nonnull @Override public String getScheme() {

modules/jooby-utow/src/main/java/io/jooby/internal/utow/UtowContext.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
import java.nio.channels.ReadableByteChannel;
2424
import java.nio.charset.Charset;
2525
import java.security.cert.Certificate;
26+
import java.util.ArrayList;
27+
import java.util.Arrays;
2628
import java.util.Collection;
2729
import java.util.Collections;
2830
import java.util.Deque;
2931
import java.util.HashMap;
3032
import java.util.Iterator;
3133
import java.util.LinkedHashMap;
34+
import java.util.List;
3235
import java.util.Map;
3336
import java.util.Optional;
3437
import java.util.concurrent.Executor;
@@ -210,16 +213,16 @@ boolean isHttpGet() {
210213
return exchange.getProtocol().toString();
211214
}
212215

213-
@Nonnull @Override public Certificate[] getClientCertificates() {
216+
@Nonnull @Override public List<Certificate> getClientCertificates() {
214217
SSLSessionInfo ssl = exchange.getConnection().getSslSessionInfo();
215218
if (ssl != null) {
216219
try {
217-
return ssl.getPeerCertificates();
220+
return Arrays.asList(ssl.getPeerCertificates());
218221
} catch (SSLPeerUnverifiedException | RenegotiationRequiredException x) {
219222
throw SneakyThrows.propagate(x);
220223
}
221224
}
222-
return new Certificate[0];
225+
return new ArrayList<Certificate>();
223226
}
224227

225228
@Nonnull @Override public String getScheme() {

0 commit comments

Comments
 (0)