-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minlopro: added PKCE helper class to manage PKCE state
- Loading branch information
1 parent
bd2341e
commit 297b869
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/minlopro/main/classes/auth-provider-plugin-web-server-with-PKCE/PKCE.cls
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,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************** | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/minlopro/main/classes/auth-provider-plugin-web-server-with-PKCE/PKCE.cls-meta.xml
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,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> |