Documentation
¶
Overview ¶
Package rp logs people in with OpenID Connect: it is the relying-party half that go-authn/oidc deliberately leaves out.
p, err := rp.Discover(ctx, http.DefaultClient, "https://login.example.org")
c, err := rp.NewClient(ctx, p, rp.Config{ClientID: "console", RedirectURI: redirect})
f := rp.NewFlow()
http.Redirect(w, r, c.AuthCodeURL(f, nil), http.StatusFound)
// ... at the redirect URI, with the flow found again:
tok, id, err := c.Exchange(ctx, code, f)
// ... later, a token for one resource server (RFC 8707 §2.2):
at, err := c.Refresh(ctx, tok.RefreshToken, rp.For{Resource: "https://fs-a.example.org/"})
What it always does, because each is a defence somebody once skipped:
- PKCE with S256 (RFC 7636), for confidential clients too (RFC 9700 §2.1.1), and a provider that publishes its methods without S256 is refused at discovery: it would ignore the challenge.
- A state and a nonce per login. The ID token is verified -- signature, issuer, audience, times, by go-authn/oidc -- and must carry this login's nonce; with several audiences, azp must be this client.
- The discovery document must name the issuer it was fetched for, exactly (OIDC Discovery §4.3), and every endpoint must be https or loopback.
A native application uses Loopback, which runs the whole login through the system browser and a listener on 127.0.0.1 (RFC 8252). Such a client is public: a secret shipped inside an application is no secret (RFC 8252 §8.5).
Index ¶
- Constants
- Variables
- func Challenge(verifier string) string
- func IsInvalidGrant(err error) bool
- func Loopback(ctx context.Context, p *Provider, cfg Config, open func(string) error, ...) (*Client, *Token, *oidc.Token, error)
- func OpenBrowser(u string) error
- type Client
- func (c *Client) AuthCodeURL(f Flow, extra url.Values) string
- func (c *Client) CheckIssuer(q url.Values) error
- func (c *Client) EndSessionURL(postLogout string) string
- func (c *Client) Exchange(ctx context.Context, code string, f Flow) (*Token, *oidc.Token, error)
- func (c *Client) Provider() *Provider
- func (c *Client) Refresh(ctx context.Context, refreshToken string, f For) (*Token, error)
- func (c *Client) Revoke(ctx context.Context, token, hint string) error
- func (c *Client) VerifyID(ctx context.Context, raw, nonce string) (*oidc.Token, error)
- type Config
- type Error
- type Flow
- type For
- type Provider
- type Set
- func (s *Set) Add(name string, c *Client) error
- func (s *Set) AuthCodeURL(f Flow, extra url.Values) (string, error)
- func (s *Set) Client(name string) *Client
- func (s *Set) Exchange(ctx context.Context, name, code string, f Flow, query url.Values) (*Token, *oidc.Token, error)
- func (s *Set) Names() []string
- func (s *Set) NewFlow(name string) (Flow, error)
- type Token
Constants ¶
const ( RefreshTokenHint = "refresh_token" AccessTokenHint = "access_token" )
RefreshTokenHint and AccessTokenHint are the token_type_hint values of RFC 7009 §2.1.
Variables ¶
var ErrMixUp = errors.New("rp: the authorization response is not from the provider this login was sent to")
ErrMixUp is a redirect delivered to the callback of a provider other than the one the login was sent to, or carrying another provider's issuer.
var ErrNoRevocation = errors.New("rp: the provider has no revocation endpoint")
ErrNoRevocation: the provider publishes no revocation_endpoint (or one in the clear), so a token cannot be revoked at it.
var ErrUnknownProvider = errors.New("rp: unknown provider")
ErrUnknownProvider is a name that is not in the set.
Functions ¶
func IsInvalidGrant ¶
IsInvalidGrant says whether err is the provider ending the grant.
func Loopback ¶
func Loopback(ctx context.Context, p *Provider, cfg Config, open func(string) error, extra url.Values) (*Client, *Token, *oidc.Token, error)
Loopback logs a person in from a native application, as RFC 8252 says to:
- it listens on the loopback IP literal, 127.0.0.1, or [::1] when there is no IPv4 loopback, on a port the system picks (§7.3); never on "localhost" (§8.3), and on no other interface;
- it opens a single-use link on its own listener, which redirects to the authorization URL, in the system browser with open (so the state, nonce and challenge never appear in a process's arguments) -- never a web view inside the application (§8.12) -- and PKCE protects the code on its way back (§6);
- it waits for the redirect at http://127.0.0.1:<port>/callback until ctx ends. A request whose state is not this login's is answered 400 and ignored -- any local process can reach the port -- so only the provider's answer ends the wait.
cfg.RedirectURI is set here; leave it empty. A native app is a public client (cfg.Secret empty, §8.5); go-authn/bridge accepts any loopback port for a client registered with http://127.0.0.1/callback. extra adds authorization parameters, such as RFC 8707 resources.
It returns the client (to refresh with), the tokens, and the verified ID token.
func OpenBrowser ¶
OpenBrowser opens u in the system's default browser.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client logs people in at one provider.
func NewClient ¶
NewClient is a client of p. It reads the provider's keys now, to verify ID tokens with.
func (*Client) AuthCodeURL ¶
AuthCodeURL is where to send the browser. extra adds parameters, such as RFC 8707 resource indicators or a prompt.
func (*Client) CheckIssuer ¶ added in v0.3.0
CheckIssuer verifies the "iss" authorization-response parameter of the redirect's query against this client's provider (RFC 9207 §2.4): when it is present it must be exactly the provider's issuer, and present once. A response without one is refused when the provider's metadata says it sends it (Provider.IssParamSupported); otherwise it is accepted, as the RFC allows and the ID token's "iss" is still verified. Use it in any redirect handler, whether or not there are several providers.
func (*Client) EndSessionURL ¶
EndSessionURL is where to send a browser to end the session at the provider too (OIDC RP-Initiated Logout §2), or "" when it has no such endpoint.
func (*Client) Exchange ¶
Exchange trades the code the redirect brought for tokens, and verifies the ID token against the flow's nonce.
func (*Client) Refresh ¶
Refresh buys an access token with a refresh token. A provider that rotates refresh tokens returns a new one, and revokes the whole grant when a retired one comes back (RFC 9700 §4.14.2): keep the new one, and never refresh twice at once with the same.
func (*Client) Revoke ¶ added in v0.4.0
Revoke asks the provider to revoke a token (RFC 7009 §2): the refresh token of a login that ends, so that a copy of it is worth nothing. hint is RefreshTokenHint, AccessTokenHint or "". The provider answers 200 whether or not the token was valid (§2.2), so success says the token is not usable, not that it ever was. A 503 (§2.2.1: try later) and the provider's own errors come back as errors; with no endpoint, ErrNoRevocation. The client authenticates as it does at the token endpoint, and a redirect is not followed.
func (*Client) VerifyID ¶
VerifyID checks an ID token: signature, issuer, audience (this client), times -- go-authn/oidc -- and the nonce of the login it answers, so that an ID token from another login cannot be played into this one (OIDC Core §3.1.3.7). With several audiences, azp must be this client.
type Config ¶
type Config struct {
ClientID string
// Secret is a confidential client's, sent with client_secret_basic
// (RFC 6749 §2.3.1). Empty is a PUBLIC client: its client_id goes in the
// token request instead, as a native app's must (RFC 8252 §8.5).
Secret string
RedirectURI string
// Scopes asked for at the login. Default openid. A refresh token usually
// needs offline_access, or the provider's equivalent.
Scopes []string
// MinRefresh is how long the ID-token key set waits before it is fetched
// again for an unknown kid (go-authn/oidc Config.MinRefresh; 0 is its
// default, one minute). Lower picks up a provider's key rotation sooner;
// it is also how often a stream of forged kids can make this client ask
// the provider.
MinRefresh time.Duration
}
Config is a client of a provider.
type Error ¶
Error is the provider's refusal (RFC 6749 §5.2). invalid_grant means the grant is over and the person has to log in again; invalid_target, that a resource was refused (RFC 8707 §2).
type Flow ¶
type Flow struct {
State, Nonce, Verifier string
// Provider names the provider of a [Set] this login was sent to; [Set]
// refuses to finish it anywhere else (RFC 9700 §4.4.2). Empty for a login
// that does not go through a Set.
Provider string
}
A Flow is one login in progress: what the redirect must be checked against and the code exchanged with. Keep it where only this browser can bring it back -- a sealed cookie, or the memory of the native app that started it.
type For ¶
For names what a refreshed token is for: a resource server (RFC 8707 §2.2: the token is for that one, the refresh token keeps the whole grant), and/or a scope, for a provider that sets the audience from a scope.
type Provider ¶
type Provider struct {
Issuer string
AuthURL string
TokenURL string
EndSessionURL string // "" when the provider has none, or one in the clear
// RevocationURL is the RFC 7009 revocation endpoint, "" when the provider has
// none, or one in the clear (see [Client.Revoke]).
RevocationURL string
// IssParamSupported: the provider says it sends its issuer in every
// authorization response (RFC 9207 §3), so a response without one is refused.
IssParamSupported bool
// contains filtered or unexported fields
}
Provider is what discovery says about an OpenID provider.
type Set ¶ added in v0.3.0
type Set struct {
// contains filtered or unexported fields
}
A Set is the providers a service logs people in at, by name.
func (*Set) Add ¶ added in v0.3.0
Add registers a client under name. Two names for one issuer, or one name twice, are refused: a login would not say which provider it is for.
func (*Set) AuthCodeURL ¶ added in v0.3.0
AuthCodeURL is where to send the browser for the flow's provider.
func (*Set) Exchange ¶ added in v0.3.0
func (s *Set) Exchange(ctx context.Context, name, code string, f Flow, query url.Values) (*Token, *oidc.Token, error)
Exchange finishes a login at the callback of provider name. query is the redirect's query (for its "iss"). It refuses, before any request leaves, a flow that was started at another provider and a response whose iss is not this provider's; then it is Client.Exchange at that provider, whose ID token is verified against that provider's issuer and this login's nonce.