Repository navigation
feat: setUserJwt, setUserJwtProvider and setAuthTokens (customer identity) - #61
Merged
Merged
Conversation
Customer identity (contract §2, §3, §5, §6): - Gleap.getInstance().setUserJwt(String) decodes the JWT payload (no signature check), reads userId / user_id and sends the existing identify with userHash = token and no contact fields. Not three base64url parts, no user id or over 4096 characters: log line, no request. Memory only (never written to the session store). GleapSessionProperties.equals compares an identity token, and the accepted token stays with the in-memory session properties, so a new token is sent even when nothing else changed. - setUserJwtProvider(GleapUserJwtProvider) (callback.onToken(token)) is asked on identify 401 token_expired (one retry with the new token) and before an identify is sent again (file session refresh, retry) when the token expires within 5 minutes. One call in flight, 10 s timeout, 30 s pause after a failure (GleapUserJwtProviderGate). - 401 token_expired / token_invalid keep the session and the user instead of clearRejectedIdentity; signature_required and other 4xx as before. - setAuthTokens(Map<String, String>) posts to /sessions/auth-tokens (null removes), kept in memory and resent after a signed identify on a new session; 401 warns and keeps the session. - clearIdentity drops the token and the auth tokens; the provider stays. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This was referenced Nov 5, 2026
Merged
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ionDuration) Customer identity spec §1, §2, §13: - The grant from a signed identify (identity.grant) is kept in memory and in the session store with its exp, bound to the session it was issued to, so an app restart stays signed until it expires. - Gleap-Grant rides on every request of that session; never with another session's id. - The Messenger gets it as a top-level `grant` in session-update, re-sent on every change (new identify, renewal, logout sends null). - An identify answered without a grant drops it; clearIdentity drops it. - A grant about to expire is renewed by replaying the last signed identify (through the JWT provider when the token expires too), at most once a minute, on a timer and on return to the foreground. - 400 identity_mismatch: drop the local session, start a fresh visitor session, send the identify once more; a second mismatch starts over as a guest. token_expired / token_invalid unchanged. - New optional config setSessionDuration (ms), sent with every identify. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…t they say - An identify refused with signature_required logs why (still starts over as a guest). - GleapSessionProperties.equals compares a token's kid and claims without iat/exp/nbf/jti, so a token re-minted per app start is not identified again. - The stored grant is removed only when one was stored. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…itialize, short-session renewal - A session answer with identity.mode 'signed' and no grant for an identified session queues the held identify (last identify, or the user hash kept from the last start) once per app start and session; GleapIdentifyService sends it even when unchanged (claimSignedGrantRequest marks the session, so it never loops) and a refusal of that grant-only identify keeps the session. - setUserJwt before Gleap.initialize is buffered and sent once the session controller exists; clearIdentity drops it. - Grant renewal: removeCallbacks before posting, renew at min(5 min, lifetime/2), re-arm once after the 60 s throttle. - JWT provider: an expired token or the token the server just refused (token_expired / token_invalid) counts as a failure (30 s pause). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Customer identity for the Android SDK, following
.handover/customer-identity/CONTRACT.md§2 (transport), §3 (identify results), §5 (POST /sessions/auth-tokens) and §6 (SDK methods), and the call shapes in the docs PR (documentation/android/user-identity.mdx,guides/customer-identity.mdx).Methods
Gleap.getInstance().setUserJwt(String token): decodes the payload (base64url JSON, no signature check), readsuserIdoruser_id(string or number), sendsPOST /sessions/identifywithuserId,userHash = tokenand no contact fields (pluslang,platform,deviceTypeas always). Refused with a log line and no request when the token is not three non-empty base64url segments, has no user id or is over 4096 characters. Memory only:setGleapUserSessionnever writes a JWT to the session store.Gleap.getInstance().setUserJwtProvider(GleapUserJwtProvider provider): new public interfaceGleapUserJwtProvider { void fetchToken(Callback callback); interface Callback { void onToken(String token); } }, called on the main thread. Asked on an identify 401 containingtoken_expired(retried once with the new token) and before an identify is sent again (file session refresh, retry after a network failure) when the token'sexpis within 5 minutes. One call in flight, 10 s timeout, 30 s pause after a failure (null, not a JWT with a user id, exception, timeout).Gleap.getInstance().setAuthTokens(Map<String, String> tokens):POST /sessions/auth-tokenswith{ "tokens": { name: value } }and the identify headers; anullvalue removes a token, invalid names (^[a-zA-Z][a-zA-Z0-9_-]{0,39}$) are skipped with a log line. Kept in memory and resent after a signed identify on a new session; a 401 logs a warning and keeps the session.clearIdentity()also drops the token and the auth tokens; the provider stays.Behaviour changes
GleapSessionProperties.equalsnow compares an identity token (when the new identify carries a JWT); the token the server accepted stays with the in-memory session properties, so a new token is sent even when nothing else changed and the same token is not sent twice. A legacy hash is still not compared. AsetUserJwtidentify compares only the user id and the token.token_expiredortoken_invalidno longer callsclearRejectedIdentity(warning logged;token_expiredgoes to the provider).signature_requiredand other 4xx with a JSON body keep today's behaviour.Difference from the documented shape
None: the docs' Java lambda
setUserJwtProvider(callback -> myBackend.fetchGleapToken(token -> callback.onToken(token)))and KotlinGleapUserJwtProvider { callback -> ... }both compile against the single-method interface.Grant transport (
.handover/customer-identity/INTERCOM-MODEL.md§1, §2, §13)GleapSessionController):identity.grantfrom a session answer is kept in memory and in the session store (SharedPreferences viaKeyValueStore:grant,grant_session_id), bound to the answer'sgleapId; the expiry is read from the grant'sexp. An app restart stays signed until it expires (loaded with the stored session, only when it belongs to it). An answer withidentitybut no grant (an unsigned identify) drops it; an answer withoutidentitykeeps it while the session is the same.Gleap-GrantviaGleapHttp.addGrantHeaderinopenSessionPost(sessions, identify, partial update, auth tokens) andaddReportSessionHeaders(reports, events, uploads), inGleapCaptureApi(JSON posts and the OkHttp file upload). Only sent with the session id the grant was issued to. An expired grant is still sent (the server may extend it once).GleapWidgetMessages.sessionUpdate()carries a top-levelgrant(string ornull); it is pushed on every session answer (GleapMainActivity.refreshSession), so a new identify or renewal re-sends it.onActivityStarted, at most once a minute. Classic projects (no grant) never trigger it.identity_mismatch(400):GleapIdentifyServicereturnsIDENTITY_MISMATCH;GleapSessionController.restartForIdentifydrops the local session (store, grant, file session, push group), keeps the identify, the provider and the auth tokens, starts a fresh visitor session and sends the identify once more on it. A second mismatch starts over as a guest.token_expired/token_invalidhandling is unchanged.clearIdentitydrops the grant (memory and store).Gleap.getInstance().setSessionDuration(long milliseconds)(also oniGleap), sent assessionDurationwith every identify; 0 removes it. Nothing else new is public.Not covered
GleapURLGenerator) carrygleapId/gleapHashas query parameters; neither gets the grant (it does not belong in a URL). The widget receives it throughsession-updateonly.identity.grantin a session answer.Verification
./gradlew test(JAVA_HOME = Android Studio jbr) →BUILD SUCCESSFUL; 239 tests, 0 failures, 0 errors in each oftestDebugUnitTestandtestReleaseUnitTest.New tests:
GleapUserJwtTest(payload decoding, shape/length rules, expiry window, 401 code detection,equalswith tokens),GleapUserJwtProviderGateTest(one in flight, 10 s timeout, 30 s pause, invalid result, throwing provider, new provider after a failure),CustomerIdentityTest(identify body, invalid token no request, resend on token change, token_invalid keeps the session, signature_required still clears, token_expired → provider → one retry, renewal before a file session refresh, auth tokens body/null/once per session/resend after signed identify, clearIdentity keeps the provider).Grant transport:
./gradlew assembleRelease→ success;./gradlew :gleap:testReleaseUnitTest→ 244 tests, 0 failures. NewGleapGrantTest: grant on the session's requests (and never with another session id) + insession-update, survives an app start and leaves withclearIdentity, an unsigned identify drops it,identity_mismatch→ fresh visitor → one more identify with the token,sessionDurationin the identify body.Final-review fixes (b8e1fa9)
signature_requiredis logged (logcat tagGleap) before the SDK starts over as a guest (behaviour unchanged): "Gleap: this project enforces identity verification and the identify had no valid signature (signature_required). Pass a token from your backend to setUserJwt."GleapSessionProperties.equalscomparesGleapUserJwt.identityFingerprint: the token'skidand its claims withoutiat/exp/nbf/jti, keys sorted (was: the raw token string). A backend that mints a fresh token per app start no longer causes an identify on every start; a changed claim or key is still sent once.store.remove("grant")(andgrant_session_id) only when a grant was stored (grantStored, set on load and on write, reset onstore.clear()).backup_rules/data_extraction_rules, so nothing was added. The stored grant is encrypted with an AndroidKeyStore key, which is not part of a backup, so a restored copy cannot be read on another device.Checks:
./gradlew test→ passes, 245 tests in each oftestDebugUnitTestandtestReleaseUnitTest, 0 failures (new: fingerprint/equals, signature_required detection, re-minted token not resent).Final re-check fixes (cdd9322)
Contract addition (Server in parallel): in Signed mode the
/sessionsand identify answers for an identified session without a grant carryidentity: { signed: false, grant: null, mode: 'signed' }; Classic carries noidentity.applyGrantkeepsidentity.mode. A non-identify session answer for an identified session on a Signed project without a grant queues the held identify once: the app's last identify with a hash, or the user hash kept in the store from the last app start (processSessionActionResult).GleapIdentifyServiceasksclaimSignedGrantRequest, which marks the session id (memory only), so the unchanged identify is sent once per app start and session and never in a loop; Classic projects still skip it. A refusal of that grant-only identify keeps the session (logged) instead of starting over as a guest.setUserJwtbeforeinitializewas dropped silently; it is now buffered (memory only) and sent byGleapInitializerright after the session controller exists, queued behind the stored session.clearIdentitydrops a buffered token.removeCallbacks(newGleapMainThread.removeCallbacks) before posting, so only one renewal is pending.min(5 min, lifetime/2)before expiry (lifetime =exp - iat, orexp -arrival withoutiat); a renewal held back by the 60 s throttle is re-armed once for when the minute is over.exp <= now, or the exact token the server just refused (token_expired/token_invalid), counts as a failure (30 s pause).CustomerIdentityTest.anExpiredRetryIsNotRetriedAgainnow expects the refused token not to be sent a second time.Checks:
./gradlew test→ 250 tests, 0 failures in each oftestDebugUnitTestandtestReleaseUnitTest. New:GleapGrantTest(held legacy hash sent once on a Signed session load, never repeated while the server answers without a grant, Classic skips the unchanged identify,setUserJwtbefore initialize sent once the SDK starts),GleapUserJwtProviderGateTest(expired / refused token fails).🤖 Generated with Claude Code