4

I have a custom class that implements Auth.RegistrationHandler This class is used a a registration handler for my Salesforce Auth. Provider (basically "Login with Salesforce" in another Salesforce org).

I would like to receive an access token for the user that logs in through that auth. provider. The documentation for Auth.AuthToken Class tells me that I can only receive an Access Token for the running user, which is an internal administrator for the registration handler.

How can I receive a user's access token without needing to switch to the user's context?

1 Answer 1

0

The only way to get the Access Token from what I have experimented is by creating an apex class referencing the AuthProviderPluginClass

Using the method below from the example code:

global Auth.AuthProviderTokenResponse handleCallback(Map<string,string> 
            authProviderConfiguration, Auth.AuthProviderCallbackState state ) 
            { 
                // Here, the developer will get the callback with actual protocol. 
                // Their responsibility is to return a new object called 
                // AuthProviderTokenResponse. 
                // This will contain an optional accessToken and refreshToken 
                key = authProviderConfiguration.get('Key__c'); 
                secret = authProviderConfiguration.get('Secret__c'); 
                accessTokenUrl = authProviderConfiguration.get('Access_Token_Url__c'); 
            
                Map<String,String> queryParams = state.queryParameters; 
                String code = queryParams.get('code'); 
                String sfdcState = queryParams.get('state'); 
            
                HttpRequest req = new HttpRequest(); 
                String url = accessTokenUrl+'?code=' + code + '&client_id=' + key + 
                '&client_secret=' + secret; 
                req.setEndpoint(url); 
                req.setHeader('Content-Type','application/xml'); 
                req.setMethod('GET'); 
            
                Http http = new Http(); 
                HTTPResponse res = http.send(req); 
                String responseBody = res.getBody(); 
                String token = getTokenValueFromResponse(responseBody, 'Token', null); 

            
                return new Auth.AuthProviderTokenResponse('Concur', token, 
                'refreshToken', sfdcState); 
            } 

Not sure why you want access token accessed in Auth.RegistrationHandler as that class is meant to only handle the creation/updating of user

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .