Skip to content

Commit 018c470

Browse files
Add endpoint to expose public key for JWT verification
The dynamic log level feature uses JWTs. These can be verified by a public key. Since the sample app can generate those JWTs this new endpoint exposes the public key required to verify the generated tokens.
1 parent ae4346d commit 018c470

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.sap.hcp.cf.logging.sample.springboot.controller;
2+
3+
import java.security.interfaces.RSAPublicKey;
4+
import java.util.Base64;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import com.sap.hcp.cf.logging.sample.springboot.keystore.TokenKeyProvider;
11+
12+
/**
13+
* This controller provides an endpoint to get the public key used for signing
14+
* the tokens generated by the {@link TokenController}.
15+
*/
16+
@RestController
17+
public class PublicKeyController {
18+
19+
private TokenKeyProvider keyProvider;
20+
21+
public PublicKeyController(@Autowired TokenKeyProvider keyProvider) {
22+
this.keyProvider = keyProvider;
23+
}
24+
25+
/**
26+
* Returns the public key in Base64 encoding used to sign the JWTs created by
27+
* the {@link TokenController}.
28+
*
29+
* @return
30+
*/
31+
@GetMapping("/publickey")
32+
public String getPublicKey() {
33+
String keyId = keyProvider.getPrivateKeyId();
34+
RSAPublicKey publicKey = keyProvider.getPublicKeyById(keyId);
35+
return Base64.getEncoder().encodeToString(publicKey.getEncoded());
36+
}
37+
38+
}

sample-spring-boot/src/main/java/com/sap/hcp/cf/logging/sample/springboot/controller/TokenController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import com.sap.hcp.cf.logging.sample.springboot.service.TokenGenerator;
1717

1818
/**
19-
* This controller provides and endpoint to create new JWT tokens. These token
19+
* This controller provides an endpoint to create new JWT tokens. These token
2020
* can be used as headers of HTTP request to dynamically switch the log level.
2121
*/
2222
@RestController

0 commit comments

Comments
 (0)