Skip to content

Add client helper methods, errors and auth types for future foji client generator - #57

Merged
bir merged 16 commits into
bir:masterfrom
davidn:ENG-3247-noclient
Jul 15, 2026
Merged

Add client helper methods, errors and auth types for future foji client generator#57
bir merged 16 commits into
bir:masterfrom
davidn:ENG-3247-noclient

Conversation

@davidn

@davidn davidn commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Motivation: I am adding a client generator to gofoji/foji. To maintain consistency with the Foji handler generator, this client should handle HTTP details and provide a simple go interface. Akin to handler generation, users will supply a auth class and helpers to convert the auth class to the required token/username/password etc. To match the handler generation, this will
use common types and helper funcs in iken. Simplicity also necessitates an error type for non-2xx HTTP responses, which must be in a library Foji clients import so that it is uniform across clients.

Changes:

  1. Add UnexpectedResponseError. This error allows wrappers of http.Client to signal non-successful HTTP responses as an error. It preserves the initial request URL (resp.Req.URL only shows the last URL in a series of redirects) for clear error messages. It preserves the body both for error messages and also to allow inspection for error handling.

  2. Add ClientAuthenticateFunc, the basic type for a function that adds authorization to an outbound request. Unlike AuthenticateFunc, this also allows the function to update the http.Client. This allows using x/oauth2, which requires you to use a client they generate. This client does additional oauth2 refreshes when required, so cannot be replicated as a mere http.Request change.

  3. Add *ClientAuth functions for Bearer auth, header auth, query auth, basic auth, cookie auth and "http client wrapping" auth. These do the actual work of setting the request/client for a specific request.

  4. Add Client*AuthenticatorFunc for tokens, basic auth, cookie auth and http.Client wrapping. These will be what the application is expected to supply to the foji generated client, and represent how to go from the user object to the credentials the *ClientAuth functions need.

  5. Add ClientSecurityGroup and ClientSecurityGroups to provide the logic for the client to send the authentication for the first security group that has all its authenticators succeed.

davidn added 5 commits July 8, 2026 13:47
Motiation: I am adding a client generator to gofoji/foji. To maintain
consistency with the foji handler generator, this client should handle
HTTP details and provide a simple go interface. This necessitates an
error type for non-2xx HTTP responses, which must be in a library foji
imports.

Change: Add UnexpectedResponseError. This error allows wrappers of
http.Client to signal non-successful HTTP responses as an error. It
preserves the initial request URL (resp.Req.URL only shows the last URL
in a series of redirects) for clear error messages. It preserves the
body both for error messages and also to allow inspection for error
handling.
Motivation: I am creating a foji client generation template. To retain
the same basic structure as the handler generation, users will supply a
auth class and helpers to convert the auth class to the required
token/username/password etc. To match the handler generation, this will
use common types and helper funcs in iken.

Change:

1. Add ClientAuthenticateFunc, the basic type for a function that adds
   authorization to an outbound request. Unlikle AuthenticateFunc, this
   also allows the function to update the http.Client. This allows using
   x/oauth2, which requires you to use a client they generate. This
   client does additional oauth2 refreshs when required, so cannot be
   replicated as a mere http.Request change.

2. Add *ClientAuth functions for Bearer auth, header auth, query auth,
   basic auth, cookie auth and "http client wrapping" auth. These do
   the actual work of setting the request/client for a specific request.

3. Add Client*AuthenticatorFunc for tokens, basic auth, cookie auth and
   http.Client wrapping. These will be what the application is expected
   to supply to the foji generated client, and represent how to go from
   the user object to the credentials the *ClientAuth functions need.
Mirroring SecurityGroup and SecurityGroups, this commit adds
ClientSecurityGroup and ClientSecurityGroups to provide the logic for a
foji client to combine authenticators to represent the security
requirements for an openapi operation.
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.76%. Comparing base (b943371) to head (7a62181).
⚠️ Report is 36 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #57      +/-   ##
==========================================
+ Coverage   97.02%   97.76%   +0.73%     
==========================================
  Files          30       32       +2     
  Lines        1311     1653     +342     
==========================================
+ Hits         1272     1616     +344     
+ Misses         31       24       -7     
- Partials        8       13       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@davidn
davidn marked this pull request as ready for review July 10, 2026 20:24
Comment thread httputil/auth.go Outdated
Comment thread httputil/client.go Outdated
Comment thread httputil/auth.go Outdated
Comment thread httputil/auth.go Outdated
Comment thread httputil/auth.go Outdated
Comment thread httputil/auth.go Outdated

@leclark leclark left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good overall after Marc's comments are resolved
added a few nits

Comment thread httputil/auth.go Outdated
Comment thread httputil/auth.go Outdated
Comment thread httputil/auth.go Outdated
Comment thread httputil/auth.go Outdated
Comment thread httputil/auth.go Outdated
Comment thread httputil/auth.go Outdated
Comment thread httputil/auth.go Outdated
davidn added 8 commits July 10, 2026 16:07
We only want the http.Client used in a request to be modified by
authorizers that are actually getting used in the request.
ClientSecurityGroup does avoid returning a modified http.Client if it
fails, but for defensive programming
bir#57 (comment) requests that
ClientSecurityGroups doesn't rely on this behaviour.

We therefore make two changes:

1. ClientSecurityGroups stores the http.Client returned by a
   ClientSecurityGroup separately from the inbound client, so it is
   discarded when the next ClientSecurityGroup is tried.

2. If ClientSecurityGroups returns because of an error, it returns the
   unmodified original http.Client.
@bir
bir merged commit 7d87ee4 into bir:master Jul 15, 2026
5 checks passed
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.

3 participants