Skip to content

Commit d7fa2df

Browse files
committed
feat(API-1791): Add App java snippets
1 parent 764a3b8 commit d7fa2df

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

content/apps/create-app.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ immediately in this Activation URL.
2929
!!!include(content/apps/create-app/activate-php.md)!!!
3030
!!!include(content/apps/create-app/activate-nodejs.md)!!!
3131
!!!include(content/apps/create-app/activate-python.md)!!!
32+
!!!include(content/apps/create-app/activate-java.md)!!!
3233

3334
## Callback URL
3435

@@ -38,6 +39,7 @@ Then, your application must expose a callback URL.
3839
!!!include(content/apps/create-app/callback-php.md)!!!
3940
!!!include(content/apps/create-app/callback-nodejs.md)!!!
4041
!!!include(content/apps/create-app/callback-python.md)!!!
42+
!!!include(content/apps/create-app/callback-java.md)!!!
4143

4244

4345

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
```java [activate:Java Spring]
2+
import java.security.SecureRandom;
3+
4+
import javax.servlet.http.HttpServletRequest;
5+
import javax.servlet.http.HttpServletResponse;
6+
import javax.servlet.http.HttpSession;
7+
8+
import org.apache.tomcat.util.buf.HexUtils;
9+
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
public class App {
14+
static final String OAUTH_CLIENT_ID = "CLIENT_ID";
15+
static final String OAUTH_SCOPES = "read_products write_products";
16+
17+
@GetMapping("/activate")
18+
public void activate(HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception {
19+
// Create a random state for preventing cross-site request forgery
20+
byte[] randomBytes = new byte[10];
21+
new SecureRandom().nextBytes(randomBytes);
22+
String state = HexUtils.toHexString(randomBytes);
23+
24+
Object pimUrl = request.getParameter("pim_url");
25+
if (pimUrl == null) {
26+
throw new Exception("Missing PIM URL in the query");
27+
}
28+
29+
// Store in the user session the state and the PIM URL
30+
session.setAttribute("oauth2_state", state);
31+
session.setAttribute("pim_url", pimUrl.toString());
32+
33+
// Build url for the Authorization Request
34+
// https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.1
35+
String authorizeUrl = pimUrl + "/connect/apps/v1/authorize" + "?response_type=code" + "&client_id=" + OAUTH_CLIENT_ID
36+
+ "&scope=" + OAUTH_SCOPES + "&state=" + state;
37+
38+
// Redirect the user to the Authorization URL
39+
response.sendRedirect(authorizeUrl);
40+
}
41+
}
42+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
```java [callback:Java Spring]
2+
import java.net.URI;
3+
import java.net.http.HttpClient;
4+
import java.net.http.HttpRequest;
5+
import java.net.http.HttpRequest.BodyPublishers;
6+
import java.net.http.HttpResponse;
7+
import java.net.http.HttpResponse.BodyHandlers;
8+
import java.nio.charset.StandardCharsets;
9+
import java.security.MessageDigest;
10+
import java.security.SecureRandom;
11+
12+
import javax.json.Json;
13+
import javax.json.JsonObject;
14+
import javax.servlet.http.HttpServletRequest;
15+
import javax.servlet.http.HttpSession;
16+
17+
import org.apache.tomcat.util.buf.HexUtils;
18+
import org.springframework.web.bind.annotation.GetMapping;
19+
20+
public class App {
21+
static final String OAUTH_CLIENT_SECRET = "CLIENT_SECRET";
22+
23+
@GetMapping("/callback")
24+
public String callback(HttpServletRequest request, HttpSession session) throws Exception {
25+
Object sessionState = session.getAttribute("oauth2_state");
26+
String stateParam = request.getParameter("state");
27+
28+
// We check if the received state is the same as in the session, for security.
29+
if (sessionState == null || !sessionState.equals(stateParam)) {
30+
throw new Exception("Invalid state");
31+
}
32+
33+
Object code = request.getParameter("code");
34+
if (code == null) {
35+
throw new Exception("Missing authorization code");
36+
}
37+
38+
Object pimUrl = session.getAttribute("pim_url");
39+
if (pimUrl == null) {
40+
throw new Exception("No PIM url in session");
41+
}
42+
43+
// Generate code challenge
44+
byte[] randomBytes = new byte[30];
45+
new SecureRandom().nextBytes(randomBytes);
46+
String codeIdentifier = HexUtils.toHexString(randomBytes);
47+
48+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
49+
byte[] codeChallengeBytes = digest.digest((codeIdentifier + OAUTH_CLIENT_SECRET).getBytes(StandardCharsets.UTF_8));
50+
String codeChallenge = HexUtils.toHexString(codeChallengeBytes);
51+
52+
String accessTokenUrl = pimUrl + "/connect/apps/v1/oauth2/token";
53+
54+
JsonObject json = Json.createObjectBuilder()
55+
.add("client_id", OAUTH_CLIENT_ID)
56+
.add("code_identifier", codeIdentifier)
57+
.add("code_challenge", codeChallenge)
58+
.add("code", code.toString())
59+
.add("grant_type", "authorization_code")
60+
.build();
61+
62+
// Do a POST request on the token endpoint
63+
HttpClient client = HttpClient.newHttpClient();
64+
HttpRequest authorizeRequest = HttpRequest.newBuilder()
65+
.uri(URI.create(accessTokenUrl))
66+
.header("Content-Type", "application/json")
67+
.POST(BodyPublishers.ofString(json.toString()))
68+
.build();
69+
70+
HttpResponse<String> response = client.send(authorizeRequest, BodyHandlers.ofString());
71+
72+
return response.body();
73+
}
74+
}
75+
```

0 commit comments

Comments
 (0)