JWTBearerTokenExchange Class

Contains methods that POST the signed JWT bearer token to a token endpoint to request an access token, in the OAuth 2.0 JWT bearer token flow.

Auth

Use the methods in this class to post a signed JWT bearer token to the OAuth token endpoint, in exchange for an access token.

To test HTTP callouts to the token endpoint, use the Auth.HttpCalloutMockUtil class.

In the following example application, the Apex controller:

  1. Creates the JSON Claims Set.
  2. Specifies the scope of the request with additional claims.
  3. Creates the signed JWT.
  4. Specifies the token endpoint and POSTs to it.
  5. Gets the access token from the HTTP response.

The following are constructors for JWTBearerTokenExchange.

Creates an instance of the JWTBearerTokenExchange class using the specified token endpoint and the signed JWT bearer token.

public JWTBearerTokenExchange(String tokenEndpoint, Auth.JWS jws)

  • tokenEndpoint:

    Type: String

    The token endpoint that the signed JWT bearer token is POSTed to.

  • jws:

    Type: Auth.JWS

    The signed JWT bearer token.

Creates an instance of the Auth.JWTBearerTokenExchange class.

public JWTBearerTokenExchange()

The following are methods for JWTBearerTokenExchange. All are instance methods.

  • clone()

    Makes a duplicate copy of the JWTBearerTokenExchange object.

  • getAccessToken()

    Returns the access_token in the token response to the JWT bearer token request.

  • getGrantType()

    Returns the grant type specified in the JWT bearer token request. The grant type value defaults to urn:ietf:params:oauth:grant-type:jwt-bearer.

  • getHttpResponse()

    Returns the full System.HttpResponse token response to the JWT bearer token request.

  • getJWS()

    Returns the JWS specified in the JWT bearer token request.

  • getTokenEndpoint()

    Returns the token endpoint that the JWT bearer token request is POSTed to.

  • setGrantType(grantType)

    Sets the grant type in the JWT bearer token request. Returned by the getGrantType() method.

  • setJWS(jws)

    Sets the JWS in the JWT bearer token request. Returned by the getJWS() method.

  • setTokenEndpoint(tokenEndpoint)

    Sets the token endpoint that the JWT bearer token request is POSTed to. Returned by the getTokenEndpoint() method.

Makes a duplicate copy of the JWTBearerTokenExchange object.

public Object clone()

Type: JWTBearerTokenExchange

Returns the access_token in the token response to the JWT bearer token request.

public String getAccessToken()

Type: String

This method extracts the access_token from the token response. If the token response issues the access token in a different parameter, the request fails.

If you want the full HTTP token response returned, use getHttpResponse instead.

Returns the grant type specified in the JWT bearer token request. The grant type value defaults to urn:ietf:params:oauth:grant-type:jwt-bearer.

public String getGrantType()

Type: String

Returns the full System.HttpResponse token response to the JWT bearer token request.

public System.HttpResponse getHttpResponse()

Type: System.HttpResponse

You can get the access token from the full System.HttpResponse. If you want only the access_token from the token response, you can use getAccessToken instead.

Returns the JWS specified in the JWT bearer token request.

public Auth.JWS getJWS()

Type: Auth.JWS

Returns the token endpoint that the JWT bearer token request is POSTed to.

public String getTokenEndpoint()

Type: String

Sets the grant type in the JWT bearer token request. Returned by the getGrantType() method.

public void setGrantType(String grantType)

Type: void

Sets the JWS in the JWT bearer token request. Returned by the getJWS() method.

public void setJWS(Auth.JWS jws)

Type: void

Sets the token endpoint that the JWT bearer token request is POSTed to. Returned by the getTokenEndpoint() method.

public void setTokenEndpoint(String tokenEndpoint)

Type: void