Auth0では、2022年7月28日より、名前空間のないプライベートのカスタムクレームをアクセストークンとIDトークンに追加できるようになりました。これらのクレームは、/userinfoエンドポイントの応答にも追加されます。クレームのタイプについては、「JSON Web Tokenクレーム」をお読みください。
// an Auth0 action exports.onExecutePostLogin = async (event, api) => { // public namespaced custom claims api.accessToken.setCustomClaim('https://myDomain.com/myClaim', 'this is a public, namespaced claim'); api.idToken.setCustomClaim('https://myDomain.com/myClaim', 'this is a public, namespaced claim'); // non-namespaced custom claims api.accessToken.setCustomClaim('myClaim', 'this is a private, non namespaced claim'); api.idToken.setCustomClaim('myClaim', 'this is a private, non namespaced claim');};
// an Auth0 action exports.onExecutePostLogin = async (event, api) => { // fetching a payload that is superior to 100KB const aHeavyPayload = getHeavyPayload(); // this will fail the authentication api.idToken.setCustomClaim('myclaim', aHeavyPayload);};
Copy
Ask AI
// an Auth0 action exports.onExecutePostLogin = async (event, api) => { // fetching a payload that is 50KB const a50KBPayload = getHeavyPayload(); // fetching another payload that is 50KB const another50KBPayload = getHeavyPayload(); // this will fail the authentication api.idToken.setCustomClaim('myclaim', a50KBPayload); api.idToken.setCustomClaim('https://myDomain.com/myClaim', another50KBPayload);};
Copy
Ask AI
// an Auth0 action exports.onExecutePostLogin = async (event, api) => { // fetching a payload that is 50KB const a50KBPayload = getHeavyPayload(); // fetching another payload that is 50KB const another50KBPayload = getHeavyPayload(); // this will succeed api.accessToken.setCustomClaim('myclaim', a50KBPayload); api.idToken.setCustomClaim('https://myDomain.com/myClaim', another50KBPayload);};
// an Auth0 action exports.onExecutePostLogin = async (event, api) => { // this will be ignored api.accessToken.setCustomClaim('roles', 'this is a role, but Auth0 will ignore it'); // this will succeed, and appear in the token api.idToken.setCustomClaim('https://myDomain.com/roles', 'this is a role');};
// an Auth0 action exports.onExecutePostLogin = async (event, api) => { // these will be ignored if the audience is an Auth0 audience api.accessToken.setCustomClaim('myATclaim', 'this is a claim'); api.accessToken.setCustomClaim('https://myDomain.com/myATclaim', 'this is a claim'); // these will succeed, they are not concerned by the audience restriction api.idToken.setCustomClaim('myIdTclaim', 'this is a claim'); api.idToken.setCustomClaim('https://myDomain.com/myIdTclaim', 'this is a claim');};
-- A resource owner password flow POST https://{yourTenant}.auth0.com/oauth/tokengrant_type:passwordusername:***password:***client_id:***client_secret:***audience:https://{yourTenant}.auth0.com/api/v2/ -- This is an Auth0 audience scope:openid profile
Copy
Ask AI
// The Access token returned by Auth0{ "iss": "https://{yourTenant}.auth0.com/", "sub": ***, "aud": [ "https://{yourApi}.com", "https://{yourTenant}.auth0.com/userinfo" ], "iat": 1655283444, "exp": 1655369844, "azp":***, "scope": "openid profile", "gty": "password", // The custom claims were added, because the Audience is not an Auth0 audience "myATclaim": "this is a claim", "https://{yourDomain}.com/{myATclaim}": "this is a claim"}
-- A resource owner password flow POST https://{yourTenant}.auth0.com/oauth/tokengrant_type:passwordusername:***password:***client_id:***client_secret:***audience:https://{yourTenant}.auth0.com/api/v2/ -- This is an Auth0 audience scope:openid profile
Copy
Ask AI
// The Access token returned by Auth0{ "iss": "https://{yourTenant}.auth0.com/", "sub": ***, "aud": [ "https://{yourTenant}.auth0.com/api/v2/", "https://{yourTenant}.auth0.com/userinfo" ], "iat": 1655283444, "exp": 1655369844, "azp":***, "scope": "openid profile", "gty": "password", // The public namespaced custom claims was added, because it is not concerned by this restriction // However, the private non-namespaced custom claim {myATclaim} was ignored "https://mydomain.com/{myATclaim}": "this is a claim"}
// an Auth0 action exports.onExecutePostLogin = async (event, api) => { // none of these will be added to tokens nor to /userinfo response api.idToken.setCustomClaim('https://example.auth0.com', 'this is a claim'); api.idToken.setCustomClaim('https://example.webtask.io', 'this is a claim'); api.idToken.setCustomClaim('https://example.webtask.run', 'this is a claim');};
// an Auth0 action exports.onExecutePostLogin = async (event, api) => { // this was ignored so far. From this migration on, the claim will be added to access tokens // if the scope contains 'email' api.accessToken.setCustomClaim('email', 'myemail@domin.com'); // this was ignored so far. From this migration on, the claim will be added to access tokens // if the scope contains 'profile' api.accessToken.setCustomClaim('family_name', 'A family name');};
// an Auth0 Rulefunction (user, context, callback) { user.app_metadata.a_claim = 'This is a claim'; user.app_metadata.another_claim = 'This is a another claim'; context.samlConfiguration = context.samlConfiguration || {}; context.samlConfiguration.mappings = { "a_claim": "app_metadata.a_claim", "another_claim": "app_metadata.another_claim" }; context.idToken.app_metadata = {}; return callback(null, user, context);}
この移行前のSAML応答:
Copy
Ask AI
<samlp:Response> (...) <saml:Assertion> (...) <saml:AttributeStatement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <saml:Attribute Name="a_claim" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"> <saml:AttributeValue xsi:type="xs:string"> This is a claim </saml:AttributeValue> </saml:Attribute> <saml:Attribute Name="another_claim" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"> <saml:AttributeValue xsi:type="xs:string"> This is a another claim </saml:AttributeValue> </saml:Attribute> </saml:AttributeStatement> </saml:Assertion></samlp:Response>
// an Auth0 Rulefunction (user, context, callback) { user.app_metadata.a_claim = 'This is a claim'; user.app_metadata.another_claim = 'This is a another claim'; context.samlConfiguration = context.samlConfiguration || {}; context.samlConfiguration.mappings = { "a_claim": "app_metadata.a_claim", "another_claim": "app_metadata.another_claim", "claim_set_via_id_token": "app_metadata.claim_set_via_id_token" }; context.idToken.app_metadata = { claim_set_via_id_token: "This is a claim which was set via context.idToken" }; return callback(null, user, context);}
この移行前のSAML応答:
Copy
Ask AI
<samlp:Response> (...) <saml:Assertion> (...) <saml:AttributeStatement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <saml:Attribute Name="a_claim"> <saml:AttributeValue xsi:type="xs:anyType"> This is a claim </saml:AttributeValue> </saml:Attribute> <saml:Attribute Name="another_claim"> <saml:AttributeValue xsi:type="xs:anyType"> This is a another claim </saml:AttributeValue> </saml:Attribute> <saml:Attribute Name="claim_set_via_id_token"> <saml:AttributeValue xsi:type="xs:anyType"> This is a claim which was set via context.idToken </saml:AttributeValue> </saml:Attribute> </saml:AttributeStatement> </saml:Assertion></samlp:Response>
アップグレード後の動作のSAML応答:
Copy
Ask AI
<samlp:Response> (...) <saml:Assertion> (...) <saml:AttributeStatement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <saml:Attribute Name="claim_set_via_id_token" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"> <saml:AttributeValue xsi:type="xs:string"> This is a claim which was set via context.idToken </saml:AttributeValue> </saml:Attribute> </saml:AttributeStatement> </saml:Assertion></samlp:Response>
// an Auth0 action exports.onExecutePostLogin = async (event, api) => { // previously ignored // From this migration on, the claim will be added to access tokens api.accessToken.setCustomClaim('myATclaim', 'this is a claim'); // previously ignored // From this migration on, the claim will be added to ID tokens api.idToken.setCustomClaim('myIdTclaim', 'this is a claim');};
// an Auth0 action exports.onExecutePostLogin = async (event, api) => { // this was ignored so far. // From this migration on, this claim will be returned in /userinfo api.idToken.setCustomClaim('myIdTclaim', 'this is a claim');};
Copy
Ask AI
-- a call to /userinfo GET https://{yourTenant}.auth0.com/userinfoAuthorization: Bearer {yourAccessToken}
Copy
Ask AI
// the response from /userinfo{ "sub": ***, (...) "myIdTclaim": "this is a claim"}
// my_claim will be ignored, this line of code is not relevant anymore,// prefer setting my_claim on `context.idToken`user.app_metadata.my_claim = 'a value'; // this version of app_metadata will take precedence over any other change context.idToken.app_metadata = { another_claim: 'another value'};// Only `another_claim` will appear in SAML/WsFed responses
if (!['samlp', 'wsfed'].includes(context.protocol)) { context.idToken.app_metadata = { claim_set_via_id_token: "This is a claim which was set via context.idToken" };}