Authorization server
Issue OAuth tokens to your own clients, with PKCE, refresh rotation and device authorization.
Signet is a relying party by default. This guide turns it into an Authorization server in four steps, then adds device authorization.
1. Configure JWT and an OAuth client store
cfg := signet.DefaultConfig()
cfg.Store = myStore
cfg.Mailer = myMailer
cfg.RateLimiter = myLimiter
cfg.JWT = &signet.JWTConfig{
Algorithm: "RS256", Key: signingKey,
AccessTTL: 15 * time.Minute, RefreshTTL: 30 * 24 * time.Hour,
}
cfg.AuthorizationServer = &signet.AuthorizationServerConfig{
Clients: myOAuthClientStore, // or StaticClients below
Devices: myDeviceStore, // required at startup
Audiences: []string{"api.example.com"},
}
auth, err := signet.New(cfg)
if err != nil {
panic(err) // ErrInvalidAuthorizationServerConfig without JWT or Devices
}For fixed clients, use StaticClients instead of a store:
cfg.AuthorizationServer.StaticClients = []signet.OAuthClient{{
ID: "cli", Secret: "secret", Trusted: true,
RedirectURIs: []string{"http://localhost:8080/callback"},
}}2. Authorize a User and issue a code
authz, err := auth.OAuthAuthorize(ctx, signet.OAuthAuthorizeInput{
ClientID: "cli",
RedirectURI: "http://localhost:8080/callback",
ResponseType: "code",
Scope: "openid email",
State: opaqueStateFromRequest,
CodeChallenge: challenge, // base64url(SHA256(verifier))
Audience: "api.example.com",
SessionToken: sessionToken,
})
if err != nil {
return err
}
// Redirect the browser with authz.Code and authz.State.The code is bound to the client, redirect URI, PKCE challenge, Scopes and User. An untrusted client needs a consent callback on AuthorizationServerConfig; without one, authorization is denied.
3. Exchange the code for tokens
tokens, err := auth.OAuthExchange(ctx, signet.OAuthTokenInput{
GrantType: "authorization_code",
Code: codeFromCallback,
CodeVerifier: verifier,
ClientID: "cli",
ClientSecret: "secret",
RedirectURI: "http://localhost:8080/callback",
})
if err != nil {
return err
}
fmt.Println(tokens.AccessToken, tokens.RefreshToken, tokens.Scope)POST /oauth/token accepts form-encoded or JSON bodies and returns OAuth-standard errors.
4. Refresh the token
refreshed, err := auth.OAuthRefresh(ctx, signet.OAuthTokenInput{
GrantType: "refresh_token",
RefreshToken: tokens.RefreshToken,
})
if err != nil {
return err
}Refresh rotation is reuse-protected: replaying the old Refresh token revokes the token family.
5. Add device authorization
device, err := auth.BeginDeviceAuthorization(ctx, signet.DeviceAuthorizeInput{
ClientID: "tv", Scope: "openid", Audience: "api.example.com",
})
// Show device.UserCode and device.VerificationURL to the User.
err = auth.ApproveDeviceAuthorization(ctx, signet.DeviceApproveInput{
UserCode: device.UserCode, SessionToken: sessionToken,
})
deviceTokens, err := auth.ExchangeDeviceAuthorization(ctx, device.DeviceCode)The device polls POST /oauth/device/token and receives authorization_pending or slow_down until the User approves in a browser. An approved device code is single-use.
Verify
Inspect the generated OpenAPI reference for /oauth/authorize, /oauth/token, /oauth/device/authorize, /oauth/device/token and the typed client wrappers. Audit events are emitted for issuance, refresh reuse, consent denial and device approval.