Skip to content

Commit

Permalink
Minlopro: added PKCE helper class to manage PKCE state
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomeandrey committed Jan 15, 2025
1 parent bd2341e commit 297b869
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public without sharing class PKCE {
private final static String INSTANCE_URL = '@SF_INSTANCE_URL';
private final static String PKCE_ENDPOINT = '/services/oauth2/pkce/generator'; // Public PKCE endpoint;
private final static Cache.OrgPartition PARTITION = Cache.Org.getPartition('local.PKCE'); // Org Cache;

static {
Assert.isNotNull(PARTITION, 'PKCE Org Cache Partition is not defined!');
}

public State generate(String cacheKey) {
// Step 1 - Invoke public Salesforce endpoint to get PKCE state;
HttpRequest pkceRequest = new HttpRequest();
pkceRequest.setMethod('GET');
pkceRequest.setEndpoint(String.format('{0}/{1}', Lists.of(INSTANCE_URL, PKCE_ENDPOINT)));
HttpResponse pkceResponse = new Http().send(pkceRequest);
State pkceState = (State) JSON.deserialize(pkceResponse.getBody(), State.class);
// Step 2 - Save state in Platform Cache;
PARTITION.put(cacheKey, pkceState, 300);
return pkceState;
}

public State get(String cacheKey) {
return (State) PARTITION.get(cacheKey);
}

public class State {
public String code_challenge_method = 'S256';
public String code_challenge = null; // e.g. JB7nT*************
public String code_verifier = null; // e.g. GkLvw**************
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit 297b869

Please sign in to comment.