Skip to Content
Signet

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/oidc

2. 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 fieldUsed for
authorization_endpointThe URL the browser is sent to.
token_endpointRedeeming the authorization code.
userinfo_endpointFetching or cross-checking the identity.
issuerChecking the ID token's iss claim.
jwks_uriFetching public keys to verify the ID token signature.
id_token_signing_alg_values_supportedThe allowed signing algorithms.
token_endpoint_auth_methods_supportedHow 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

  1. POST /oauth/start generates a PKCE verifier and a random OIDC nonce, seals both into the single-use OAuth state, and builds the authorization URL with openid, code_challenge, code_challenge_method=S256 and nonce.
  2. 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.
  3. Signet redeems the code at the discovered token_endpoint with the PKCE verifier.
  4. If discovery published jwks_uri, Signet fetches and caches the keys, then verifies the ID token: signature, iss = discovered issuer, aud = client ID, exp/iat with a 30-second tolerance, allowed algorithms, and nonce = the sealed value. none and HS256 are always rejected. A key fetch failure is a hard error.
  5. UserInfo is fetched with the access token. Its sub must match the verified ID token's sub. Email and email_verified fall back per field from the token to UserInfo.
  6. 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.
  7. Signet creates a Session exactly as it does for Google and GitHub, records acme as the Last sign-in method, and redirects the browser to the redirect you 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 only grant_type, code, redirect_uri and code_verifier.
  • Post → no Basic header; client_id and client_secret are 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

ConditionResult
Discovery unreachable, non-HTTPS or redirectingNewProvider error at startup
Issuer mismatch, no supported algorithmsNewProvider error at startup
Duplicate/reserved ProviderIDErrOAuthProviderConflict from signet.New
Invalid state, expired state, bad redirectinvalid_request (400)
JWKS fetch failure, bad signature, wrong issuer/audience/nonceCallback fails; no Session is created
Subject mismatch between token and UserInfoCallback fails
Email missing or unverifiedExplicit account-linking flow

See OAuth sign-in for the shared endpoint contract and state encryption.

On this page