Generic OIDC provider
Configure any OpenID Connect provider, from discovery to a signed-in User, step by step.
The oauth/oidc adapter implements Signet's OAuthProvider interface, so a generic OIDC Provider
is registered in Config.OAuth.Providers exactly like Google or
GitHub. It uses Provider discovery, PKCE, an OIDC nonce and verified ID tokens,
without changing Signet's core or adding a new dependency.
1. Install the adapter
go get github.com/shakibdshy/signet-auth/oauth/oidc2. Build the Provider — discovery happens here
import (
"context"
"log"
"net/http"
"os"
"github.com/shakibdshy/signet-auth"
"github.com/shakibdshy/signet-auth/oauth/oidc"
)
ctx := context.Background()
provider, err := oidc.NewProvider(ctx, oidc.Config{
ProviderID: "acme", // unique; not "google" or "github"
ClientID: os.Getenv("OIDC_CLIENT_ID"),
ClientSecret: os.Getenv("OIDC_CLIENT_SECRET"),
Issuer: "https://idp.example.com",
Scopes: []string{"profile", "email"}, // "openid" is added automatically
})
if err != nil {
log.Fatal(err) // discovery failures are startup errors
}NewProvider fetches .well-known/openid-configuration once and keeps:
| Discovery field | Used for |
|---|---|
authorization_endpoint | The URL the browser is sent to. |
token_endpoint | Redeeming the authorization code. |
userinfo_endpoint | Fetching or cross-checking the identity. |
issuer | Checking the ID token's iss claim. |
jwks_uri | Fetching public keys to verify the ID token signature. |
id_token_signing_alg_values_supported | The allowed signing algorithms. |
token_endpoint_auth_methods_supported | How to authenticate at the token endpoint. |
Explicit fields on oidc.Config override discovered values. If your provider needs a nonstandard
discovery path, set DiscoveryURL explicitly — tenant paths are preserved:
provider, err := oidc.NewProvider(ctx, oidc.Config{
ProviderID: "acme",
ClientID: os.Getenv("OIDC_CLIENT_ID"),
ClientSecret: os.Getenv("OIDC_CLIENT_SECRET"),
Issuer: "https://idp.example.com",
DiscoveryURL: "https://idp.example.com/tenant/.well-known/openid-configuration",
HTTPClient: &http.Client{}, // inject custom TLS trust or timeouts
})Every network URL — discovery, JWKS, UserInfo and overrides — must be HTTPS and may not redirect.
A failed fetch is an error, never a downgrade. ProviderID must be unique and cannot be google
or github; a duplicate makes signet.New return ErrOAuthProviderConflict.
3. Register the Provider and mount the HTTP API
cfg.OAuth = &signet.OAuthConfig{
Providers: []signet.OAuthProvider{provider},
RedirectBaseURL: "https://api.example.com",
AllowedRedirects: []string{"https://app.example.com/callback"},
StateKey: stateKey, // exactly 32 random bytes
}
cfg.PathPrefix = "/auth"
cfg.AppURL = "https://api.example.com"
auth, err := signet.New(cfg)
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
mux.Handle("/auth/", auth.Handler())Register https://api.example.com/auth/oauth/callback as an allowed redirect URI with your
identity provider. PathPrefix decides that path — change both values together.
4. Start the sign-in from the browser
POST /auth/oauth/start
Content-Type: application/json
{"provider":"acme","redirect":"https://app.example.com/callback"}The response is:
{"url":"https://idp.example.com/authorize?client_id=acme&code_challenge=...&nonce=..."}Redirect the browser to url. Signet owns GET /auth/oauth/callback — do not implement it.
For browser code, see Frontend client; its baseURL must
include the backend PathPrefix (https://api.example.com/auth).
5. What happens on the callback, in order
POST /oauth/startgenerates a PKCE verifier and a random OIDC nonce, seals both into the single-use OAuth state, and builds the authorization URL withopenid,code_challenge,code_challenge_method=S256andnonce.- The Provider redirects to
GET /auth/oauth/callback?code=...&state=.... Signet consumes the state Token, decrypts it, checks the 10-minute expiry and revalidates the callback URL. - Signet redeems the code at the discovered
token_endpointwith the PKCE verifier. - If discovery published
jwks_uri, Signet fetches and caches the keys, then verifies the ID token: signature,iss= discovered issuer,aud= client ID,exp/iatwith a 30-second tolerance, allowed algorithms, andnonce= the sealed value.noneand HS256 are always rejected. A key fetch failure is a hard error. - UserInfo is fetched with the access token. Its
submust match the verified ID token'ssub. Email andemail_verifiedfall back per field from the token to UserInfo. - A verified ID token alone can supply identity. Without a JWKS or ID token, UserInfo is required. If neither yields a subject, sign-in fails.
- Signet creates a Session exactly as it does for Google and GitHub, records
acmeas the Last sign-in method, and redirects the browser to theredirectyou supplied.
Code-level details
Nonce binding
The core generates the nonce and seals it with the PKCE verifier. Two optional interfaces carry it without breaking existing adapters:
type AuthURLWithNonce interface {
AuthURLWithNonce(clientID, redirectURI, state, pkceChallenge, nonce string) string
}
type ExchangeWithNonce interface {
ExchangeWithNonce(context.Context, string, string, string, string) (*OAuthIdentity, error)
}The OIDC adapter implements both; Google and GitHub do not, and keep working unchanged.
Token endpoint authentication
Discovery's token_endpoint_auth_methods_supported is kept as p.authMethods. At redemption
time Signet chooses a style: client_secret_basic if present, otherwise client_secret_post,
and Basic if the list is absent. An explicit config value overrides it.
- Basic →
Authorization: Basic base64(clientID:clientSecret); the form body carries onlygrant_type,code,redirect_uriandcode_verifier. - Post → no Basic header;
client_idandclient_secretare added to the form body.
provider, err := oidc.NewProvider(ctx, oidc.Config{
ProviderID: "acme",
ClientID: os.Getenv("OIDC_CLIENT_ID"),
ClientSecret: os.Getenv("OIDC_CLIENT_SECRET"),
Issuer: "https://idp.example.com",
TokenEndpointAuth: "client_secret_post", // or "client_secret_basic"
})Algorithms
The discovered list is intersected with RS256, PS256, ES256, ES384, ES512 and EdDSA.
JWKS keys may be RSA, EC or Ed25519; the key type must match the token's signing method. An empty
intersection makes NewProvider fail.
Identity mapping
The subject from a verified ID token is authoritative; UserInfo's sub is only a match check.
Email and verification flag come from the token when present and fall back per field to UserInfo.
An unverified email follows Signet's normal explicit-linking flow rather than being auto-linked.
Error cases
| Condition | Result |
|---|---|
| Discovery unreachable, non-HTTPS or redirecting | NewProvider error at startup |
| Issuer mismatch, no supported algorithms | NewProvider error at startup |
Duplicate/reserved ProviderID | ErrOAuthProviderConflict from signet.New |
| Invalid state, expired state, bad redirect | invalid_request (400) |
| JWKS fetch failure, bad signature, wrong issuer/audience/nonce | Callback fails; no Session is created |
| Subject mismatch between token and UserInfo | Callback fails |
| Email missing or unverified | Explicit account-linking flow |
See OAuth sign-in for the shared endpoint contract and state encryption.