Skip to content

Commit dd1e214

Browse files
authored
chore: add guidance for external_id in tokenizer (#2414)
1 parent bf58db2 commit dd1e214

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

docs/identities/session-to-jwt-cors.mdx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ session:
114114
whoami:
115115
tokenizer:
116116
templates:
117-
jwt_template_1:
118-
jwks_url: base64://... # A JSON Web Key Set (required)
119-
claims_mapper_url: base64://... # A JsonNet template for modifying the claims
120-
ttl: 1m # 1 minute (defaults to 10 minutes)
121-
subject_source: id # (optional, defaults to id, other option: external_id if you're using `external_id` on identities)
122-
another_jwt_template:
123-
jwks_url: base64://... # A JSON Web Key Set
117+
jwt_template_1:
118+
jwks_url: base64://... # A JSON Web Key Set (required)
119+
claims_mapper_url: base64://... # A JsonNet template for modifying the claims
120+
ttl: 1m # 1 minute (defaults to 10 minutes)
121+
subject_source: id # (optional, defaults to id, other option: external_id if you're using `external_id` on identities)
122+
another_jwt_template:
123+
jwks_url: base64://... # A JSON Web Key Set
124124
```
125125
126126
### JSON Web Token claim mapper
@@ -220,3 +220,17 @@ If the key set contains more than one key, the first key in the list will be use
220220
],
221221
}
222222
```
223+
224+
### Handling `external_id` in the JWT sub claim
225+
226+
If your identities use `external_id`, set `subject_source` to `external_id` in the tokenizer template to populate the JWT sub
227+
claim with that value.
228+
229+
Tokenization will fail if `subject_source` is set to `external_id` but an identity is missing `external_id`. This is a security
230+
measure that prevents issuing tokens with the same sub claim for different identities, since identity IDs and `external_id` values
231+
are only guaranteed to be unique within their own namespaces, not across both.
232+
233+
To avoid this, configure a webhook to automatically set `external_id` for new identities (for example using the same ID generation
234+
logic as your previous system).
235+
236+
After migration, you can switch to using Ory identity IDs and set `subject_source` back to `id`.

docs/kratos/manage-identities/60_external-id.mdx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ create or update identities.
3434

3535
Do not add `external_id` to your identity schema definition. It is handled separately by Ory Kratos internally.
3636

37-
### Use `external_id` in JWT `sub` claim
37+
### Use `external_id` in tokenized session JWTs `sub` claim
3838

3939
Set the `subject_source` to `external_id` in the tokenization config:
4040

@@ -43,15 +43,17 @@ session:
4343
whoami:
4444
tokenizer:
4545
templates:
46-
jwt_template_1:
47-
jwks_url: base64://... # A JSON Web Key Set (required)
48-
claims_mapper_url: base64://... # A JsonNet template for modifying the claims
49-
ttl: 1m # 1 minute (defaults to 10 minutes)
50-
subject_source: external_id
51-
another_jwt_template:
52-
jwks_url: base64://... # A JSON Web Key Set
46+
jwt_template_1:
47+
jwks_url: base64://... # A JSON Web Key Set (required)
48+
claims_mapper_url: base64://... # A JsonNet template for modifying the claims
49+
ttl: 1m # 1 minute (defaults to 10 minutes)
50+
subject_source: external_id
51+
another_jwt_template:
52+
jwks_url: base64://... # A JSON Web Key Set
5353
```
5454
55+
Read more about [session tokenization here](../../identities/session-to-jwt-cors.mdx).
56+
5557
This will populate the `sub` claim in JWTs with the value of `external_id`.
5658

5759
If `external_id` is not set for a user when `subject_source` is `external_id`, tokenization will fail.

0 commit comments

Comments
 (0)