How do I add custom claims to JWT's?

At times it may be more useful to attach data directly to a JWT, to prevent an extra API call to an Account’s Custom Data resource.

Although this feature is not directly supported in our Product, you can implement it in our SDKs. The idea is to take the Stormpath generated access token from the backend and create a new JWT with the original claims (and your new ones). Then, you can sign it with your Stormpath Tenant's API secret. 


// Create a token using an OAuth grant flow (Password Grant in this example) var passwordGrantRequest = OauthRequests.NewPasswordGrantRequest() .SetLogin("") .SetPassword("Changeme123!") .Build(); var grantResult = await app.NewPasswordGrantAuthenticator() .AuthenticateAsync(passwordGrantRequest); // Use the Stormpath .NET SDK to parse the returned JWT var accessTokenJwt = client.NewJwtParser().Parse(grantResult.AccessTokenString); // Build a new JWT by copying the header and body, and signing it with the API secret var jwtBuilder = client.NewJwtBuilder() .SetHeader(accessTokenJwt.Header) .SetClaims(accessTokenJwt.Body) .SignWith(client.Configuration.Client.ApiKey.Secret, Encoding.UTF8); // Add any other fun stuff you want! jwtBuilder.SetClaim("foo", "myvalue!"); // Spit out a JWT string var newJwt = jwtBuilder.Build().ToString();

Java: Coming soon

Node: Coming soon

PHP: Coming soon

Ruby: Coming soon


Have more questions? Submit a request



Please sign in to leave a comment.