|
| 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