Magic links and session keys

Magic links and session keys

A session key identifies a portal user (a Contact) on every request. A magic link is a separate, emailable token that the portal exchanges for a session key. There is no Salesforce User login for portal users. With Secure Session Keys enabled in 12.5, both tokens are signed by the org that issued them. The setting is off by default; the rollout steps below describe legacy compatibility.

What the token is

With Secure Session Keys enabled in 12.5, a session key or magic link token is an opaque, signed value that is safe to place in a URL as is. Treat it as a credential. Do not parse it, store it or build logic on its contents. To find out who a key belongs to, call b3p.PortalSession (see Verifying a session from another package).

The signing key is generated inside each org. It is not shipped in the package and is not visible to admins, code or the API.

Validity is Portal_Magic_Link_Validity__c days (default 14) unless the caller supplies its own. Link validity and session length are two different controls. See Capping session length.

Switching on secure session keys

The legacy token format is deprecated. Enable Secure Session Keys on every org as soon as your installed packages support it.

Everything this article says about signed tokens applies once Secure Session Keys (Secure_Session_Keys__c) is checked on the Portal custom setting. Its default is unchecked so that other 3B packages keep working during migration. Configure it on the effective Portal settings records, including any Site guest user or profile overrides. While it is unchecked:

Roll out in this order:

  1. Install 3B Portals 12.5. Legacy session behavior continues while the setting is off.
  2. Confirm that 3B Onboarding, 3B WFM and any other installed package that consumes portal sessions verify them through 3B Portals. Upgrade those packages where needed before enabling the setting.
  3. Check Secure Session Keys.

The field is marked as a temporary migration setting. When you check it:

A magic link is a portal URL with a magic link token attached as the sessionKey parameter:

https://<site>/b3p__Portal#<PortalName>?sessionKey=<token>&recordId=<optionalRecordId>

When the user opens the link, the frontend reads sessionKey from the URL at boot, removes it from the address bar and sends it to the redeemMagicLink action. The action returns a fresh session key, which the browser stores. Every subsequent Apex call sends the stored key.

A magic link token is never accepted as a session by itself. Any action other than redeemMagicLink rejects it, with or without One Time Magic Links.

Check One_Time_Magic_Links__c on the Portal custom setting to make each magic link work once.

Step What happens
Link minted The 3B Get Magic Link action and the Portal Login As component on the Contact record mint a magic link token (type m). Keys from access codes, passkeys and login codes are session keys (type s).
First open redeemMagicLink checks the link (expiry, Contact, Disabled_Portal_User__c), records a Magic Link Login Web Event, and returns a fresh session key. The browser stores the fresh key, not the link.
Second open The link has already been used, so the user sees "This link has already been used and is no longer valid. Please request a new one." (label Portal_MagicLinkUsed). A session the browser already holds is kept.
Link used as a session directly Rejected on every action. A magic link token is only accepted by redeemMagicLink, so calling loadUser or any other action with it does not bypass the check.

Notes for administrators:

Logging in as a Contact

The Portal Login As Lightning web component (loginAs) lets an internal user open a portal as a Contact, for support or testing. It runs as a Login As quick action on Contact (Contact.Login_As) or embedded on a Contact Lightning record page.

  1. Pick a Web Portal from the readable portals listed by name (up to 1,000).
  2. Pick a Site, the same list as the Portal Publisher: "Internally" (the org domain) plus every active Site.
  3. The component mints a magic link through Publisher.getContactSession and shows it with a Copy button and a Log In As {Contact Name} button that opens it in a new tab.

The link has the standard shape <site>/b3p__Portal#<Web Portal Name>?sessionKey=<token>, uses the Portal_Magic_Link_Validity__c validity and is marked as a magic link, so One Time Magic Links applies. A fresh link is minted on every selection change. With One Time Magic Links on, clicking the button consumes the link shown, so change a selection to mint a new one before copying it to the Contact.

Setup:

Step Where
Add the Login As action to the Contact page layout Setup, Object Manager, Contact, Page Layouts, Mobile and Lightning Actions
Or drag Portal Login As onto a Contact record page Lightning App Builder. The Portal Page property defaults to b3p__Portal
Grant access to the Publisher Apex class and read access to Web_Portal__c Permission set

Apex: Publisher.getData(recordId) returns { record: {Id, Name}, siteOptions: [{value,label}], portalOptions: [{value,label}] }. Publisher.getContactSession(contactId) returns the token:

{ "success": true, "responseObject": "<magic-link-token>" }

Use the invocable Apex action "3B Get Magic Link" (CreateSessionService.getSessionKey), category 3B Portals. It takes a Contact id and returns the token.

A typical automation is a record triggered Flow that, for example on invitation, calls this action for the target Contact and emails them a link built as <siteUrl>/b3p__Portal#<Portal>?sessionKey=<sessionkeyUrlSafe>.

Set Validity Minutes per link when a single default does not fit. Common values:

Use case Validity Minutes
One time password reset style link 15
Same day action, for example confirm a shift 1440 (one day)
Standard invitation leave blank, uses the setting

Validity Minutes must be greater than zero. Zero or a negative value fails the Flow interview with "Validity Minutes must be greater than zero" rather than quietly minting a dead link. Put the returned Expires At in the email so the recipient knows the deadline.

When a session key is accepted

The frontend sends the key as sessionKey on any session gated action (for example loadUser). With Secure Session Keys on, a key is accepted only when all of these are true:

  1. It is a valid signed key issued by this org.
  2. It has not expired. Expiry is always enforced.
  3. It is a session key. A magic link token never authenticates a request.
  4. The Contact exists and Disabled_Portal_User__c is not checked.
  5. It was issued after the Contact's Sessions_Revoked_At__c.

On success the Contact is returned and a Login Web Event is logged at most once per day.

Verifying a session from another package

Other packages and custom Apex must not decode or check a session key themselves. They ask 3B Portals who the key belongs to. No secret is shared and no compile time dependency is needed:

Callable portal = (Callable) Type.forName('b3p', 'PortalSession').newInstance();
Map<String, Object> result = (Map<String, Object>) portal.call(
    'verify', new Map<String, Object>{ 'sessionKey' => sessionKey });
Id contactId = (Boolean) result.get('valid') ? (Id) (String) result.get('contactId') : null;

Response:

{ "valid": true, "contactId": "003XXXXXXXXXXXXXXX", "expiresAt": 1790000000000 }
{ "valid": false, "contactId": null, "expiresAt": null }

For signed sessions, valid is true only when all five conditions above are met. While Secure Session Keys is off, legacy keys are checked under the compatibility rules described above. Always act as the returned contactId. Never trust a Contact id sent by the browser next to the key.

To give a Contact a way in from Apex, call the 3B Get Magic Link action (b3p.CreateSessionService.getSessionKey) for a link, or b3p.PortalLoginCode.issue(contactId, ttlSeconds) for a short lived one time code. See Terminal login codes. No other package can mint a session key.

Capping session length

Session_Length_Minutes__c on the Portal custom setting caps how long a single portal session may run. Leave it blank and a session lasts as long as Portal_Magic_Link_Validity__c (14 days by default). With Secure Session Keys off, a blank cap leaves expiry controlled by Expire_Session_Keys__c.

When it is set:

  1. On the first authenticated page load, loadUser checks the incoming key. If it would outlive the cap, the server mints a replacement key that expires Session_Length_Minutes__c from now and returns it alongside the Contact.
  2. The browser stores the replacement key and remembers its expiry.
  3. When that moment arrives the portal shows "The session has expired", calls logout and clears the stored key. The user lands back on the login prompt.
  4. The server rejects the expired key on every subsequent call.

The cap only ever shortens a session. Reloading the page does not slide the window forward, so a 30 minute cap means 30 minutes from the first page load, not 30 minutes of inactivity.

With One Time Magic Links off, a magic link stays valid for its own validity window even after a capped session ends. Someone who opens a 14 day link, works for 30 minutes and is logged out can open the same link again the next day and get another 30 minute session. Shorten Validity Minutes on the link itself when the link should be single sitting.

Worked example

Setting Value
Portal_Magic_Link_Validity__c 14
Session_Length_Minutes__c 30

A Contact receives an invitation link on 1 March. They open it on 5 March at 09:00. The server issues a session key expiring 09:30. At 09:30 the browser logs them out. If One Time Magic Links is off, the invitation link still works until 15 March and each use grants a fresh 30 minutes. With it on, the first use consumes the link.

Expiry and revocation

b3p.PortalSession.revokeAllSessions();

Security summary