Skip to content

fix(auth): use base64url encoding for JWT segments in createCustomToken#167

Closed
JaredEzz wants to merge 1 commit intofirebase:nextfrom
JaredEzz:fix/custom-token-signature
Closed

fix(auth): use base64url encoding for JWT segments in createCustomToken#167
JaredEzz wants to merge 1 commit intofirebase:nextfrom
JaredEzz:fix/custom-token-signature

Conversation

@JaredEzz
Copy link
Copy Markdown

Summary

Fixes #16

createCustomToken produces JWTs that Firebase Auth rejects with invalid-custom-token because the token segments are encoded with standard base64 instead of base64url.

  • Root cause: _encodeSegment in token_generator.dart used base64Encode (standard base64, which uses + and / characters) instead of base64Url.encode (base64url, which uses - and _ characters per RFC 4648 Section 5).
  • Effect: The JWT header, payload, and signature all contained non-URL-safe characters, violating the JWS Compact Serialization format required by RFC 7515 Section 2. Firebase Auth correctly rejected these tokens.
  • Fix: Replace base64Encode(buffer).replaceFirst(RegExp(r'=+$'), '') with base64Url.encode(buffer).replaceAll('=', '').

Note: The app_check token_generator.dart already handles this correctly via its _toWebSafeBase64 helper -- this PR brings the auth token generator in line with that approach using Dart's built-in base64Url codec.

This is a minimal one-line change. No new dependencies are introduced.

Related

Test plan

  • Verify createCustomToken output passes jwt.io validation without base64url warnings
  • Verify signInWithCustomToken succeeds on a Flutter client using the generated token
  • Verify the signature segment is 342 base64url characters (256 bytes) for RS256

The _encodeSegment method was using base64Encode (standard base64 with
+ and / characters) instead of base64Url.encode (base64url with - and _
characters). Per RFC 7515, JWT segments must be base64url-encoded without
padding. This caused Firebase Auth to reject custom tokens with the error
"invalid-custom-token" because the signature was not correctly encoded.

Fixes firebase#16

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 17, 2026

CLA assistant check
All committers have signed the CLA.

@demolaf demolaf changed the base branch from main to next February 18, 2026 09:49
@demolaf demolaf deleted the branch firebase:next March 18, 2026 15:41
@demolaf demolaf closed this Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createCustomToken generates invalid Token

3 participants