-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce2d005
commit 7c46474
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/main/java/com/docusign/core/security/SecurityHelpers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> getScopeList() { | ||
List<String> 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<String> 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); | ||
} | ||
} |