Skip to content

Castle

If your organization uses Castle for account security, EdgeKit can ask Castle about your users’ sign-ins and hand its verdict to your canvas rules. EdgeKit calls Castle under your own Castle account. Castle’s policies, written in your Castle dashboard, decide what it flags.

You need your own Castle account. EdgeKit holds none and resells nothing.

  1. Copy your API secret. In Castle’s dashboard, copy the API secret, not the publishable key. One secret covers all your sites.
  2. Save it in Delivery › Configuration › Castle. EdgeKit sends Castle one test request with the secret. If Castle rejects the secret, EdgeKit saves nothing and says so. Any other answer saves it, with a warning when Castle did not confirm it.

EdgeKit keeps the secret with your organization’s other secrets and never shows it back: the screen only says whether one is saved. Saving redeploys your organization, which takes a few minutes. Until then EdgeKit does not call Castle.

Where you develop or test against EdgeKit, save a secret from Castle’s sandbox instead, so test sign-ins never reach your production data at Castle.

Castle judges a request by a request token that castle.js mints in the browser. A token is good for 120 seconds and for one request only. Install castle.js with your Castle publishable key and pass the instance configure() returns to the browser client:

import * as Castle from '@castleio/castle-js';
import { createClient } from '@edgekitcloud/client-browser';
const castle = Castle.configure({ pk: '<YOUR_PUBLISHABLE_KEY>' });
const ek = createClient({ siteId: 'site_example', castle });

The client then waits for a fresh token before each decide, each sign-in send, each code sign-in, each session read and each email change request, and sends it with the request. You never handle a token yourself. If castle.js fails to mint one, the request goes without it. Without castle, no request changes.

Call configure() early in page load. A token requested before castle.js has finished loading can take 1 to 2 seconds.

A decide from your backend has no browser to mint a token. Have your page mint one with castle.createRequestToken(), send it to your server with the page’s own request, and forward it:

import { createServerClient } from '@edgekitcloud/client-server';
const ek = createServerClient({
baseUrl: 'https://<tenant>.edgekit.ai',
apiKey: process.env.EDGEKIT_API_KEY,
siteId: 'site_example',
});
const decisions = await ek.decide('paywall', {
asUser: userId,
castleRequestToken: tokenFromYourPage,
});

On the wire, the token is the decide body’s castleRequestToken field. A server decide without one gets no Castle verdict.

castle_required is the one switch for the Castle checks at sign-in. It is off by default.

  • The default for every site is set in Delivery › Configuration › Castle.
  • One site can override it on its own page in Delivery › Delivery › Sites: inherit the default, on, or off. A site override wins over the default either way, so a site where castle.js is not installed yet can stay off while the others are protected.

When it is on for a site, EdgeKit asks Castle before it sends a sign-in link or code and before it requests an email change, and refuses the request when Castle denies it. A send or email change that carries no Castle request token, or one Castle rejects as invalid, is refused too. On each session read that carries a token, EdgeKit asks Castle and signs out that one session when Castle denies or challenges it. A session read without a token is not checked. A wrong sign-in code is reported to Castle and nothing else happens. If Castle times out (after 1 second), fails or rate-limits EdgeKit, the request goes ahead.

When it is off, none of these checks run. castle.* facts in your canvas rules still work either way.

Both settings take effect on the next deploy. A deploy that turns castle_required on, as the default or for any site, is refused while no Castle secret is saved.

When castle_required is on for a site, EdgeKit asks Castle before it mails a sign-in link (POST /v1/id/sign-in/magic-link) or a sign-in code (POST /v1/id/email-otp/send-verification-otp). It tells Castle the address, as a login attempt by that user when the address has an account, and as a registration attempt when it does not. Castle gets the same address and headers as for a decide, described below.

  • Castle answers deny: EdgeKit refuses the send and mails nothing.
  • Castle answers challenge: the mail goes. The emailed link or code is the challenge.
  • The request carries no Castle request token, or Castle rejects the token as invalid: EdgeKit refuses the send.
  • Castle times out after 1 second, fails or rate-limits EdgeKit: the mail goes.

A refused send answers 409 with the error code sign_in_refused:

{
"error": { "code": "sign_in_refused", "message": "This sign-in was refused." }
}

The answer is the same whether or not the address has an account, so nobody can use it to find out who has one, and it does not name Castle. The browser client returns it as { status: 'failed', code: 'sign_in_refused' }. Show the user a general message, such as “We can’t sign you in right now”.

EdgeKit asks Castle before it counts the send against the address’s limit of sign-in emails, so a refused attempt does not use up that address’s sends.

A wrong code at POST /v1/id/sign-in/email-otp is reported to Castle as a failed login, with the address. Castle’s answer changes nothing: the user gets the same answer they would get without Castle.

EdgeKit logs each of these calls as ek.castle.check, with what it did: refused, allowed, or failed_open when Castle could not answer and the mail went anyway. A wrong-code report records no action.

When castle_required is on for a site, EdgeKit asks Castle before it starts an email change (POST /v1/id/change-identifier/request). It tells Castle who the signed-in user is (their id, current address and when they registered), a hashed form of their session id, and the address change itself, from the current address to the new one.

  • Castle answers deny: EdgeKit refuses the change. No code goes to the new address, and the per-address limit is not spent.
  • Castle answers challenge: the change goes ahead. The code sent to the new address is the challenge.
  • The request carries no Castle request token, or Castle rejects the token as invalid: EdgeKit refuses the change.
  • Castle times out after 1 second, fails or rate-limits EdgeKit: the change goes ahead.

A refused change answers 409 with the error code change_refused:

{
"error": { "code": "change_refused", "message": "This change was refused." }
}

The answer does not name Castle. The browser client returns it as { status: 'failed', code: 'change_refused' } from id.changeIdentifier.request. Show the user a general message, such as “We can’t change your email right now”.

EdgeKit logs each check as ek.castle.check, the same as a send.

A sign-in by email link or by Google is a page navigation, which carries no Castle request token. So EdgeKit tells Castle about a sign-in on the session’s first read instead, and judges the session again on each read after it.

When castle_required is on for a site and a session read (POST /v1/id/get-session) carries a Castle request token, EdgeKit asks Castle about the session. It tells Castle who the signed-in user is (their id, address and when they registered) and a hashed form of the session id. The first checked read of a session reports a successful login, or a successful registration when that sign-in created the user. Every later read is reported as an event named session read.

  • Castle answers deny or challenge: EdgeKit signs out this one session. The read answers 200 with { "user": null, "session": null }, the same as for a signed-out user, and clears the session’s cookies and the access cookie decide reads. The page then sees the user as signed out, and they can sign in again.
  • Castle answers allow: the session is returned as usual.
  • The read carries no token: EdgeKit does not ask Castle, and the session is returned. A page that reads the session before castle.js is ready keeps the user signed in.
  • Castle times out after 1 second, fails, rate-limits EdgeKit or rejects the token as invalid: the session is returned. The login is reported again on the next checked read.

Only the session Castle judged is signed out. The user’s sessions on their other devices stay signed in, because Castle judged this device, not the account.

A session from your own identity provider is never checked: not at an external_only site, and not on the read at a dual site that exchanges your identity provider’s sign-in for an EdgeKit session. Later reads of that EdgeKit session are checked.

EdgeKit logs each check as ek.castle.check with revoked, allowed or failed_open. A revoked session is also logged as ek.identity.session with verb: "ended" and cause: "castle".

Castle bills each checked read. With the session store, the browser client reads the session on page load and after each sign-in or sign-out.

Fact Type Value
castle.action enum allow, challenge or deny: the action of the Castle policy that matched
castle.scores.account_abuse number 0 to 1: how likely the device or user is abusing accounts, for example by creating many
castle.scores.account_takeover number 0 to 1: how likely the person signed in is not the account’s owner
castle.scores.bot number 0 to 1: how likely the request is automated
castle.signals string[] The names of the signals Castle raised, such as proxy_ip. Castle adds new ones over time

The facts answer for anonymous devices too. Castle recognizes a device after it clears its cookies, so a device that creates many accounts can show a high account_abuse before it signs in. Their trust tier is EdgeKit, and no outcome can show their value to the browser, so an abuser cannot read their own score and adjust until it drops.

A rule on castle.action follows the policies you write in Castle’s dashboard. Rename a policy there and your rules keep working, because no fact carries the policy’s name.

Without a saved secret, the canvas builder lists the facts but you cannot pick one, and stage refuses a feature that reads one, directly or through a segment, with castle.fact_without_key.

EdgeKit asks Castle only when a decide request reaches a canvas that reads a castle.* fact, once per request, and waits at most 1 second. For a signed-in user it sends Castle the user’s id. For an anonymous device it sends none. A decide that reads no castle.* fact never calls Castle.

With no verdict, castle.* has no value, and that is an error: the rule reading it does not hold, and if no other rule holds, the canvas answers its error outcome. That happens with no secret saved, no request token, an answer slower than 1 second, or a request Castle refuses, rate-limits or fails. To let requests through whenever Castle cannot answer, make the error outcome your open one.

  • The request token, and an event named decide.
  • The user’s address: your site proxy’s signed address when the site proxy holds EDGEKIT_CLIENT_IP_KEY, otherwise the address that connected to EdgeKit. A forwarded address such as X-Forwarded-For is never used as the address.
  • The request’s headers, except Cookie, Authorization, X-Api-Key and EdgeKit’s own x-edgekit-* headers.
  • The signed-in user’s id, and nothing else about the user.

EdgeKit logs each call in your organization’s logs as ek.castle.check: what it sent, Castle’s whole answer, how long it took and why it failed, if it did. The request token and your secret never appear in it.

Castle bills each call: one per decide request that reaches a rule reading castle.*. Such a decide can take up to 1 second longer while Castle is slow. Castle sees anonymous browsing on those requests, on pages where you loaded castle.js.

When you erase a user, EdgeKit also asks Castle to delete the data it holds under that user’s id, because EdgeKit sent Castle that data. If Castle fails or cannot be reached, EdgeKit retries the request. With no Castle secret saved, nothing is sent.

edgekit.ai