Auth0 Actions を使用すると、カスタムコードで認証をカスタマイズできます。複数のカスタムドメイン を使用している場合は、Actions からカスタムドメイン情報にアクセスして、ドメイン固有のロジックを実装したり、ユーザーを組織に振り分けたり、アクセス ポリシーを適用したり、通知をカスタマイズしたりできます。
Actions では、event.custom_domain オブジェクトを通じてカスタムドメインの情報にアクセスできます。このオブジェクトには、ドメイン名と、そのドメインに設定されたすべてのメタデータが含まれます。
event.custom_domain オブジェクトは、以下の Action トリガーで使用できます。
| Trigger | Event object reference |
|---|
| Credentials Exchange | イベントオブジェクト |
| Custom Email Provider | イベントオブジェクト |
| Custom Phone Provider | イベントオブジェクト |
| Post Login | イベントオブジェクト |
| Post User Registration | イベントオブジェクト |
| Post Password Change | イベントオブジェクト |
| Pre User Registration | イベントオブジェクト |
| Reset Password Post Challenge | イベントオブジェクト |
event.custom_domain オブジェクトは、リクエストがカスタムドメイン経由で発生したトリガーでのみ使用できます。カスタムドメインが使用されていない場合、このプロパティは undefined になります。
ドメインメタデータで設定された login.example.com ドメインを使用する場合:
| キー | 値 |
|---|
| allow_list | example1.com,example2.com |
exports.onExecutePostLogin = async (event, api) => {
console.log(event.custom_domain);
};
コンソールには次のように出力されます:
{
"domain": "login.example.com",
"domain_metadata": {
"allow_list": "example1.com,example2.com"
}
}
カスタムドメインと正規ドメインのどちらを使用しているかに応じて、異なるルールを適用します。
exports.onExecutePostLogin = async (event, api) => {
const domain = event.custom_domain?.domain;
if (
domain === undefined ||
domain.includes('.auth0.com') || // パブリッククラウド
domain.includes('.auth0app.com') // プライベートクラウド
) {
return api.access.deny('Please use the custom domain to log in.');
}
};
カスタムドメインメタデータを使用して、メールアドレスのドメインごとのアクセスポリシーを適用します。
const getEmailDomain = (email) => {
if (!email || typeof email !== 'string') {
return null;
}
const parts = email.split('@');
// '@' がちょうど1つ存在すること(または少なくともドメイン部分が存在すること)を確認する
// "invalid-email" や "user@" のようなケースに対応する
if (parts.length < 2 || !parts[1]) {
return null;
}
return parts[1].toLowerCase().trim();
}
exports.onExecutePreUserRegistration = async (event, api) => {
const domain = event.custom_domain?.domain;
if (domain === undefined) {
return api.access.deny(
'access_denied',
`Access denied - Users cannot access without custom domain.`
);
}
const email = event.user.email;
if (email === undefined) {
return api.access.deny(
'access_denied',
`Access denied - Users cannot access without email.`
);
}
const domainAllowList = event.custom_domain?.domain_metadata?.allow_list?.split(',') || [];
const emailDomain = getEmailDomain(email);
if (domainAllowList.includes(emailDomain) === false) {
return api.access.deny(
'access_denied',
`Access denied - Users from ${emailDomain} cannot access ${domain}.`
);
}
};
アプリケーションメタデータとドメインメタデータを使用して、アプリケーション グループとドメイン グループのアクセス ポリシーを適用します。
exports.onExecuteCredentialsExchange = async (event, api) => {
const domain = event.custom_domain?.domain;
if (
domain === undefined ||
domain.includes(event.tenant.id)
) {
return; // 正規ドメインの場合はスキップ
}
const applicationGroup = new Set(event.client.metadata?.domain_group?.split(',') || []);
const domainGroup = event.custom_domain?.domain_metadata?.domain_group?.split(',') || [];
const intersection = domainGroup.filter(x => applicationGroup.has(x));
if (intersection.length === 0) {
return api.access.deny(
'invalid_request',
`Access denied - Cannot get access from application ${event.client.name} and ${domain}.`
);
}
};
接続メタデータとドメインメタデータを使用して、接続グループおよびドメイングループに対するアクセス ポリシーを適用します:
exports.onExecutePostLogin = async (event, api) => {
const domain = event.custom_domain?.domain;
if (domain === undefined) {
return api.access.deny(
`Access denied - Users cannot access without custom domain.`
);
}
const connectionGroup = new Set(event.connection.metadata?.domain_group?.split(',') || []);
const domainGroup = event.custom_domain?.domain_metadata?.domain_group?.split(',') || [];
const intersection = domainGroup.filter(x => connectionGroup.has(x));
if (intersection.length === 0) {
return api.access.deny(
`Access denied - Users cannot access connection ${event.connection.name} from ${domain}.`
);
}
};
組織とドメインのメタデータを使用して、組織グループおよびドメイングループのアクセスポリシーを適用します。
exports.onExecutePostLogin = async (event, api) => {
const organization = event.organization;
if (organization === undefined) {
return; // 組織認証以外はスキップ
}
const domain = event.custom_domain?.domain;
if (domain === undefined) {
return api.access.deny(
`Access denied - Users cannot access without custom domain.`
);
}
const organizationGroup = new Set(organization.metadata?.domain_group?.split(',') || []);
const domainGroup = event.custom_domain?.domain_metadata?.domain_group?.split(',') || [];
const intersection = domainGroup.filter(x => organizationGroup.has(x));
if (intersection.length === 0) {
return api.access.deny(
`Access denied - Users cannot access organization ${organization.name} from ${domain}.`
);
}
};
カスタムドメインメタデータに基づいて、リージョン固有の外部サービスにリクエストを送信します。
exports.onExecuteCustomEmailProvider = async (event, api) => {
const regionServiceEndpoint = event.custom_domain?.domain_metadata?.region_service_endpoint;
if (regionServiceEndpoint === undefined) {
return api.notification.drop(`Missing regional service endpoint configuration at custom domain.`);
}
const notification = event.notification;
const messageBody = {
body: notification.html
};
try {
await fetch(regionServiceEndpoint, {
method: 'POST',
headers: {
'X-API-Key': event.secrets.API_KEY,
},
body: JSON.stringify(messageBody),
});
} catch (err) {
api.notification.drop('External service failure');
}
};