<@U04UQ68083H> How can I Convert Ory Sessions to J...
# ory-copilot
w
@chilly-king-10285 How can I Convert Ory Sessions to JSON Web Tokens using the Ory project .well_known keys
c
A message from kapa.ai
To convert Ory Sessions to JSON Web Tokens (JWTs), you need to follow several steps: 1. Create a JSON Web Key set and store it locally: You can use the Ory CLI to create a JSON Web Key set. Here is an example command:
Copy code
bash ory create jwk some-example-set \     --alg ES256 --project $PROJECT_ID --format json-pretty \     > es256.jwks.json
[Source](https://www.ory.sh/docs/identities/session-to-jwt-cors#end-to-end-example) 2. Create a JsonNet template: This template will be used to modify the claims of the JWT. Here is an example of how to create a JsonNet template:
Copy code
bash local claims = std.extVar('claims');   local session = std.extVar('session');      {     claims: {       iss: claims.iss + "/additional-component",       schema_id: session.identity.schema_id,       session: session,     }   }
[Source](https://www.ory.sh/docs/identities/session-to-jwt-cors#end-to-end-example) 3. Base64-encode the files: The easiest way to supply these files to Ory Network is to base64-encode them:
Copy code
bash JWKS_B64_ENCODED=$(cat es256.jwks.json | base64)   JSONNET_B64_ENCODED=$(cat claims.jsonnet | base64)
[Source](https://www.ory.sh/docs/identities/session-to-jwt-cors#end-to-end-example) 4. Configure your Ory Network project's tokenizer templates: You can use the Ory CLI to configure your project's tokenizer templates:
Copy code
bash ory patch identity-config $PROJECT_ID \     --add '/session/whoami/tokenizer/templates/jwt_example_template1={"jwks_url":"base64://'$JWKS_B64_ENCODED'","claims_mapper_url":"base64://'$JSONNET_B64_ENCODED'","ttl":"10m"}' \     --format yaml
[Source](https://www.ory.sh/docs/identities/session-to-jwt-cors#end-to-end-example) 5. Convert an Ory Session to a JWT: You can use the Ory JS SDK or REST API to convert an Ory Session to a JWT. Here is an example using the Ory JS SDK:
Copy code
javascript import { Configuration, FrontendApi } from "@ory/client"      const frontend = new FrontendApi(     new Configuration({       basePath: `https://${process.env.ORY_PROJECT_SLUG}.<http://projects.oryapis.com|projects.oryapis.com>`,     }),   )      export async function toSessionWithJwt(sessionId: string) {     const session = await frontend.toSession({       tokenize_as: "jwt_example_template1",     })     const jwt = session.tokenized     return jwt   }
[Source](https://www.ory.sh/docs/identities/session-to-jwt-cors#end-to-end-example) 6. Verify the resulting JSON Web Token: You can export the public key from the JSON Web Key Set and use it to verify the token:
Copy code
bash ory get jwk some-example-set \     --public \     --project $PROJECT_ID --format json-pretty \     > es256-public.jwks.json
[Source](https://www.ory.sh/docs/identities/session-to-jwt-cors#end-to-end-example-part-2) 7. Validate JSON Web Tokens: You can validate JSON Web Tokens issued by Ory