From 7c46474ebe59256c246f60b58deebfb4d987de2a Mon Sep 17 00:00:00 2001 From: inbargazit Date: Thu, 31 Oct 2024 12:08:36 -0700 Subject: [PATCH] Adding missing file --- .../core/security/SecurityHelpers.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/main/java/com/docusign/core/security/SecurityHelpers.java diff --git a/src/main/java/com/docusign/core/security/SecurityHelpers.java b/src/main/java/com/docusign/core/security/SecurityHelpers.java new file mode 100644 index 0000000..e7a4394 --- /dev/null +++ b/src/main/java/com/docusign/core/security/SecurityHelpers.java @@ -0,0 +1,72 @@ +package com.docusign.core.security; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Base64; +import java.util.List; +import java.util.Random; + +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; +import com.docusign.core.model.ApiType; +import com.docusign.core.model.Session; +import com.docusign.esign.client.auth.OAuth; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class SecurityHelpers { + public static List getScopeList() { + List scopes = new ArrayList<>(); + for (ApiType scope : ApiType.values()) { + scopes.addAll(Arrays.asList(scope.getScopes())); + } + return scopes; + } + + public static String generateCodeVerifier() { + byte[] randomBytes = new byte[32]; + new Random().nextBytes(randomBytes); + return Base64.getUrlEncoder().withoutPadding().encodeToString(randomBytes); + } + + public static String generateCodeChallenge(String codeVerifier) throws NoSuchAlgorithmException { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] hash = digest.digest(codeVerifier.getBytes(StandardCharsets.UTF_8)); + return Base64.getUrlEncoder().withoutPadding().encodeToString(hash); + } + + public static String parseJsonField(String jsonResponse, String field) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonNode = mapper.readTree(jsonResponse); + return jsonNode.get(field).asText(); + } + + public static void setSpringSecurityAuthentication( + List scopes, + String oAuthToken, + OAuth.UserInfo userInfo, + String accountId, + Session session, + String expiresIn) { + JWTOAuth2User principal = new JWTOAuth2User(); + principal.setAuthorities(scopes); + principal.setCreated(userInfo.getCreated()); + principal.setName(userInfo.getName()); + principal.setGivenName(userInfo.getGivenName()); + principal.setFamilyName(userInfo.getFamilyName()); + principal.setSub(userInfo.getSub()); + principal.setEmail(userInfo.getEmail()); + principal.setAccounts(userInfo.getAccounts()); + principal.setAccessToken(new OAuth.OAuthToken().accessToken(oAuthToken)); + + session.setTokenExpirationTime(System.currentTimeMillis() + Integer.parseInt(expiresIn) * 1000L); + + OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(principal, principal.getAuthorities(), + accountId); + SecurityContextHolder.getContext().setAuthentication(token); + } +}