Skip to main content

#JWT Bearer Token Exhcange3 discutindo

Fale sobre isso no Slack para obter respostas mais rápidas e ajuda em tempo real.

Hi, 

I am having issues finding a sample of C# code to connect to my external app using JWT. Since uid/password for OAuth2 will be discontinued as of February, I am getting started now on replacing that with JWT authentication. The cert has been created and uploaded from the connecting server, so that should be set up properly. Now I need to convert my code to use JWT for authentication instead of the current uid/password method, but am unable to find a reliable sample of code. Can anyone out there help? 

 

#Authentication.clientId  #JWT Bearer Token Exhcange  #OAuth 2.0

5 respostas
  1. 14 de out., 16:06

    byte[] privateKeyRaw // contain the key in base 64

    RSACryptoServiceProvider provider = new RSACryptoServiceProvider();

    provider.ImportPkcs8PrivateKey(new ReadOnlySpan<byte>(privateKeyRaw), out _);

    RsaSecurityKey rsaSecurityKey = new RsaSecurityKey(provider);

    var now = DateTime.UtcNow;

    var claims = new[] {

    new Claim(JwtRegisteredClaimNames.Iss, cfg.AppSettings.Settings["ClientId"].Value),

    new Claim(JwtRegisteredClaimNames.Sub, cfg.AppSettings.Settings["Username"].Value),

    new Claim(JwtRegisteredClaimNames.Exp, ((DateTimeOffset.Now.ToUnixTimeMilliseconds() / 1000) + 300).ToString()),

    };

    var handler = new JwtSecurityTokenHandler();

    var token = new JwtSecurityToken

    (

    cfg.AppSettings.Settings["ClientId"].Value,

    cfg.AppSettings.Settings["LoginUrl"].Value,

    claims,

    now.AddMilliseconds(-30),

    now.AddMinutes(60),

    new SigningCredentials(rsaSecurityKey, SecurityAlgorithms.RsaSha256)

    );

    jwt_token = handler.WriteToken(token);

    // To do

    // Connect to the api {{url}}/services/oauth2/token

    // send the jwt in the body parameter

    var content = new FormUrlEncodedContent(

    [

    new KeyValuePair<string, string>("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer"),

    new KeyValuePair<string, string>("assertion", jwt_token)

    ]);

    HttpClient _httpClient = new();

    var request = new HttpRequestMessage

    {

    Method = HttpMethod.Post,

    RequestUri = new Uri(cfg.AppSettings.Settings["LoginUrl"].Value + "/services/oauth2/token"),

    Content = content

    };

    var responseMessage = await _httpClient.SendAsync(request).ConfigureAwait(false);

    var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

Hello,

 

I tried to implement Salesforce Auth JWT but am getting unsupported grant type error.

 

JWTBearerTokenExchange Error 400 unsupported grant type

 

Code:

Auth.JWT jwt = new Auth.JWT();

jwt.setSub('user@email.ca');

jwt.setAud('https://custom.sandbox.lightning.force.com');

jwt.setIss('0000000000000000000000000000000000000000000000000');

Auth.JWS jws = new Auth.JWS(jwt, 'certificate');

String token = jws.getCompactSerialization();

System.debug(token);

String tokenEndpoint = 'https://custom.com/services/rest/auth/oauth2/v1/token';

Auth.JWTBearerTokenExchange bearer = new Auth.JWTBearerTokenExchange(tokenEndpoint, jws);

String accessToken = String.valueOf(bearer.getAccessToken());

//String accessToken = String.valueOf(bearer.getHttpResponse());

System.debug(accessToken);

The bearer grant type was defaulted to urn:ietf:params:oauth:grant-type:jwt-bearer. I tried using Named Credentials instead of Apex implementation and am still getting the same error.

 

How do I resolve this issue?

 

Regards,

Shylesh

 

#Apex #Authentication Token #Auth #JWT Bearer Token Exhcange

being alerted by the here mentioned deferral reason "Your API clients rely on RSA key exchanges in their HTTPS connections instead of Elliptic Curve Diffie Hellman Ephemeral (ECDHE) key exchanges.", I try to understand the impact of the Edge Network on a system that integrates with the Salesforce REST API, and authenticates using OAuth JWT. Namely, I want to formulate requirements for the IT partner hosting this system, so that it would be ready for the Edge Network.

 

I share my findings so far:

  • For the OAuth JWT an RSA keypair is used as suggested here. I could observe that on both a sandbox without Edge, and another sandbox where Edge was auto-enabled already a while ago, the JWT authentication succeeds. So, probably the said deferral reason is not about OAuth JWT.
  • I then turned my attention to the connection security, simply by inspecting REST endpoints in my browser, and notice that both on Edge and not yet Edge the connection security is based on ECDHE, only differing in the type of curve I believe. However this probably does not tell much, and probably my browser negotiates the best possible connection security.What is meant exactly by the Edge Network deferral reason

now my questions:

  • Can anyone confirm that OAuth JWT authentication on the Salesforce REST API based on an RSA keypair is indeed guaranteed to continue working also on the Edge Network?
  • Does the "Your API clients rely on RSA key exchanges"  deferral reason have to do with the Edge Network offering less/other connection security methods?
  • Is there a list of what connection security methods exactly the Edge Network offers? 
  • Or alternatively, how to inspect all possible connection security methods on the Salesforce REST API resources? 

kr,

Sidney

 

@Salesforce Edge Network Auto Enablement #Security #Integration #Edge Network #JWT Bearer Token Exhcange #Saleforce Administrator

2 respostas
  1. 13 de set. de 2023, 12:07

    Thank you a lot @Steven Lawrance. With you mentioning the TLS cipher suite, I understood what is meant by that opt-out, and more precisely what is meant with key exchanges.

     

    And so I realised I could check the situation of our API client as follows:

    • I took the TLS Cipher Suite from the Login History of this API client's user ( this is on an org not yet on the Edge Network)Thank you a lot . With you mentioning the TLS cipher suite, I understood what is meant by that opt-out, and more precisely what is meant with key exchanges.
    • I looked up the OpenSSL format
    • I ascertained that the API client uses a cipher where the key exchange part is ECDHE

    OpenSSL format: ECDHE-RSA-AES256-GCM-SHA384

    corresponding general format: TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 

Hi @Architect Trailblazers ,

 

I'm unable to find answers on this so posting it here.

 

Does Salesforce support the JWKS standard? 

 

The JWKS standard in brief - it's a standard that exposes a JWKS endpoint - from which the public keys can be retrieved. This is useful especially for key rotation.

 

My question:

Is there a way for Salesforce to consume a JWKS endpoint to retrieve the public keys "on the fly" before verifying the JWT token?

 

As far as my knowledge goes, this is not possible with the standard JWT Bearer Token Exhcange OAuth flow. (https://help.salesforce.com/articleView?id=remoteaccess_oauth_jwt_flow.htm&type=0)

 

- because the Public Key has to be uploaded against the Connected App - and can't be retrieved on the fly.

 

I understand that we can use the Salesforce-published public keys to verify that the signature in the response is a valid Salesforce signature. (https://help.salesforce.com/articleView?id=remoteaccess_using_id_token.htm&type=5)

 

But can Salesforce consume the published public keys of a different provider's JWKS endpoint and verify the JWT token in the standard JWT Bearer Token flow? 

3 comentários