Security provider helpers
Security provider helpers add the credentials that a generated client needs before it sends an HTTP request. The package also provides a small bridge between ECDSA keys and PEM files, so applications can use trusted key files with their own JWT or protected-API code. These helpers support the client side of authentication; server-side request checks remain the responsibility of validation middleware. See supporting libraries, operation and client generation, server interface generation, and integration examples for the surrounding systems.
Security provider role
Client request authentication
A security provider is a request editor: a function that receives a request and changes it before the client sends it. Generated clients can keep a provider for all operations or pass one for a single operation. This lets the generated API code stay focused on request and response data while an application supplies its authentication policy.
The request path is a short handoff from the API contract to the credentialed HTTP request:
flowchart LR
A[OpenAPI security requirement] --> B[Generated client request]
B --> C[Request editor]
C --> D[Credentialed HTTP request]
D --> E[Protected API]
References: pkg/…/securityprovider.go• L1-4 internal/…/clients.gen.go• L34-35 internal/…/clients.gen.go• L56-59 internal/…/clients.gen.go• L96-102 internal/…/clients.gen.go• L556-567
Built-in credential forms
Authorization credentials
The package supplies providers for the two common authorization-header forms. Basic authentication encodes a username and password in the Authorization header. Bearer authentication places a token in the same header with the Bearer scheme.
These providers are useful when the protected API defines one stable credential for the client. If the application must refresh a token or select credentials per request, it can provide its own request editor with the same request-editor contract.
References: pkg/…/securityprovider.go• L26-68 README.md• L1528-1562
API key locations
The API key provider supports the request locations that an OpenAPI API-key scheme can use:
| Location | Request change | Typical contract |
|---|---|---|
header | Adds a named HTTP header | The API reads the key from a header |
query | Adds a named URL query value | The API reads the key from the URL |
cookie | Adds a named cookie | The API reads the key from a cookie |
The location, name, and value must match the protected API contract. A location outside header, query, or cookie is rejected when the provider is created, rather than producing an ambiguous request.
References: pkg/…/securityprovider.go• L70-110 internal/…/client_test.go• L51-74
Request editor use
Client-wide and operation-level credentials
Generated clients accept request editors in two scopes. A client option applies an editor to the client's requests. An operation can also receive extra editors when the call needs credentials or headers that differ from the client default. The generated client applies its stored editors first and the operation-specific editors after them; an editor error stops the request before it reaches the network.
This model supports both shared authentication and narrow exceptions. It also means that multiple editors can affect the same request, so their order and header behavior form part of the request contract.
References: internal/…/clients.gen.go• L56-59 internal/…/clients.gen.go• L96-102 internal/…/clients.gen.go• L105-117 internal/…/clients.gen.go• L556-567 internal/…/client_test.go• L51-74
ECDSA key file support
Key file conversion
The ECDSA file helpers convert between Go ECDSA key values and PEM-encoded X.509 key data. They load public keys from PKIX public-key data and private keys from EC private-key data. They return clear errors when the input has no PEM block, cannot be parsed, or contains a different public-key type.
The helpers cover file format handling only. A JWT library or another security library must use the resulting key to sign, verify, or build a key set.
References: pkg/…/ecdsafile.go• L11-20 pkg/…/ecdsafile.go• L21-59 pkg/…/ecdsafile.go• L61-85
Signing and verification boundaries
ECDSA uses a private key to sign data and a public key to verify it. A protected API integration normally keeps the private key in the trusted signer and gives verifiers only the public key. The authenticated API example uses the file helper to load a private key for a local test signer, then derives a public key for JWT validation; its hard-coded key is example-only and is not a safe production pattern.
This separation matters when a generated client uses a bearer token. The security provider sends the token, while the identity or JWT system creates and validates it. Neither responsibility is replaced by the request editor or by the ECDSA file conversion helper.
References: pkg/…/ecdsafile.go• L21-85 examples/…/fake_jws.go• L14-21 examples/…/fake_jws.go• L40-76 examples/…/fake_jws.go• L79-92 examples/…/README.md• L50-89
Client and server authentication boundaries
Client credential attachment
On the client, a provider changes an outgoing request so that a protected API can identify the caller. It does not decide whether the caller is trusted, check token claims, or enforce scopes.
Server request validation
On the server, generated handlers do not provide security validation by themselves. Validation middleware reads the API's security requirements and calls application authentication code. This keeps credential attachment, token validation, and permission checks as separate responsibilities that can use the right policy for each deployment.
References: README.md• L1500-1507 README.md• L1528-1562 examples/…/README.md• L58-89
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the request-editor function contract and editor order compatible with every generated client that accepts
WithRequestEditorFnor per-operation editors. - Keep API-key location names aligned with the OpenAPI security scheme and with the server's expected header, query value, or cookie.
- Keep PEM and X.509 key formats aligned with the external key-generation and JWT libraries that consume them.
- Preserve the private-key and public-key boundary when updating examples or protected-API integrations.
- Keep client credential attachment separate from server authentication and scope validation.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
An API-key location is not header, query, or cookie | Provider creation fails | Use the location required by the API contract |
| A key file has no PEM block or uses an unsupported key encoding | ECDSA key loading fails before signing or verification | Supply the expected PEM-encoded X.509 public or EC private key |
| A public-key loader receives an RSA or other non-ECDSA key | Public-key loading fails | Use an ECDSA public key for this helper |
| A credential editor returns an error | The generated client does not send the request | Treat editor errors as an authentication or request-build failure |
| A private signing key is embedded in application code or shared with verifiers | Anyone with the key can create trusted signatures | Keep the private key in a trusted signer and distribute only the public key |
Key files
| File | Why to read it |
|---|---|
| pkg/…/securityprovider.go | Defines the reusable Basic, Bearer, and API-key request providers. |
| pkg/…/ecdsafile.go | Defines the PEM and X.509 conversion boundary for ECDSA public and private keys. |
| internal/…/clients.gen.go | Shows the generated-client request-editor contract and when editors run. |
| internal/…/client_test.go | Shows the built-in providers attached to a generated client. |
| examples/…/fake_jws.go | Shows how ECDSA key material can support a JWT example while keeping verification and signing roles distinct. |
| README.md | Places client request editors beside the separate server-side validation model. |