Skip to content

Add Cloudflare service token authentication support - #4

Merged
josevalim merged 3 commits into
dashbitco:mainfrom
AkioCode:main
Mar 4, 2026
Merged

josevalim merged 3 commits into
dashbitco:mainfrom
AkioCode:main

Conversation

@AkioCode

@AkioCode AkioCode commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Support service token authentication strategy for Cloudflare Zero Trust.
Service token authentications are detected by their JWT structure.

Given Cloudflare ZTA is set up along with a Service Auth policy,
And a valid `cf-access-jwt-assertion` token generated after a service token auth request,
When performing JWT validation using `authenticate/3`, 
Then return `{conn, %{} = metadata}`.

Currently, it returns {conn, nil} due to 400 from get_user_identity response, which is interpreted as a failure.

Code changes

  • Fix typo
  • Detects a user identity vs service token authentication strategy
  • Rewords get_user_identity to get_metadata for more consistency
  • Includes two more fields in the metadata output (only applied for Cloudflare provider for now):
    • strategy: either "user_identity" or "service_token";
    • client_id: Cloudflare service token client_id
  • Adapt Cloudflare tests, generate tokens, and add service token tests

AkioCode added 2 commits March 3, 2026 22:41
Support both identity-based and service token authentication strategies
for Cloudflare Zero Trust. Service tokens are detected by their JWT
field structure (common_name, exp, iat, iss, sub, type) and return
client_id and strategy metadata instead of user identity.
Comment thread lib/nimble_zta/cloudflare.ex Outdated
resp = Req.request!(url: url, headers: [cookie: cookie])
if resp.status == 200, do: {:ok, resp.body}, else: :error
defp get_metadata(token, encoded_token, url) do
if MapSet.equal?(MapSet.new(Map.keys(token.fields)), @service_token_keys) do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be cleaner to do a pattern matching instead:

case token.fields do
  %{"aud" => _, "common_name" => _, ...}

@josevalim josevalim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment and we can ship it!

@AkioCode
AkioCode requested a review from josevalim March 4, 2026 12:29
@josevalim
josevalim merged commit bc004b7 into dashbitco:main Mar 4, 2026
2 checks passed
@josevalim

Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

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.

2 participants