Documentation
¶
Index ¶
- Variables
- func WithAPIURL(url string) option
- func WithClient(client *http.Client) option
- func WithDriver[T Provider](name string, credentials Credentials) option
- func WithKey(key string) option
- func WithSession(session Session) option
- type AuthorizationError
- type BitbucketProvider
- func (self BitbucketProvider) Endpoint(Credentials) oauth2.Endpoint
- func (self BitbucketProvider) GetUserByToken(ctx context.Context, client *http.Client, _ *oauth2.Token, _ Credentials) (*fastjson.Value, error)
- func (self BitbucketProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
- func (self BitbucketProvider) Scoping() Scoping
- type Credentials
- type DriverContract
- type FacebookProvider
- func (self FacebookProvider) Endpoint(Credentials) oauth2.Endpoint
- func (self FacebookProvider) GetUserByToken(ctx context.Context, client *http.Client, token *oauth2.Token, ...) (*fastjson.Value, error)
- func (self FacebookProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
- func (self FacebookProvider) Scoping() Scoping
- type GitHubProvider
- func (self GitHubProvider) Endpoint(Credentials) oauth2.Endpoint
- func (self GitHubProvider) GetUserByToken(ctx context.Context, client *http.Client, _ *oauth2.Token, _ Credentials) (*fastjson.Value, error)
- func (self GitHubProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
- func (self GitHubProvider) Scoping() Scoping
- type GitLabProvider
- func (self GitLabProvider) Endpoint(credentials Credentials) oauth2.Endpoint
- func (self GitLabProvider) GetUserByToken(ctx context.Context, client *http.Client, _ *oauth2.Token, ...) (*fastjson.Value, error)
- func (self GitLabProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
- func (self GitLabProvider) Scoping() Scoping
- type GoogleProvider
- func (self GoogleProvider) Endpoint(Credentials) oauth2.Endpoint
- func (self GoogleProvider) GetUserByToken(ctx context.Context, client *http.Client, _ *oauth2.Token, _ Credentials) (*fastjson.Value, error)
- func (self GoogleProvider) Issuer() string
- func (self GoogleProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
- func (self GoogleProvider) Scoping() Scoping
- type LinkedInProvider
- func (self LinkedInProvider) Endpoint(Credentials) oauth2.Endpoint
- func (self LinkedInProvider) GetUserByToken(ctx context.Context, client *http.Client, _ *oauth2.Token, _ Credentials) (*fastjson.Value, error)
- func (self LinkedInProvider) Issuer() string
- func (self LinkedInProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
- func (self LinkedInProvider) Scoping() Scoping
- type Manager
- type Provider
- type Scoping
- type Session
- type SlackProvider
- func (self SlackProvider) Endpoint(Credentials) oauth2.Endpoint
- func (self SlackProvider) GetUserByToken(ctx context.Context, client *http.Client, _ *oauth2.Token, _ Credentials) (*fastjson.Value, error)
- func (self SlackProvider) Issuer() string
- func (self SlackProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
- func (self SlackProvider) Scoping() Scoping
- type TwitchProvider
- func (self TwitchProvider) Endpoint(Credentials) oauth2.Endpoint
- func (self TwitchProvider) GetUserByToken(ctx context.Context, client *http.Client, _ *oauth2.Token, ...) (*fastjson.Value, error)
- func (self TwitchProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
- func (self TwitchProvider) Scoping() Scoping
- type User
- type XProvider
- func (self XProvider) Endpoint(Credentials) oauth2.Endpoint
- func (self XProvider) GetUserByToken(ctx context.Context, client *http.Client, _ *oauth2.Token, _ Credentials) (*fastjson.Value, error)
- func (self XProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
- func (self XProvider) Scoping() Scoping
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnknownDriver = errors.New("sociable: unknown driver") // ErrDriverNotConfigured means the driver has no client id, no endpoints // or no redirect URL. Refuse the route rather than redirect. ErrDriverNotConfigured = errors.New("sociable: driver not configured") ErrMissingKeyOrSession = errors.New("sociable: a Key or a Session is required") ErrSessionSealed = errors.New("sociable: the cookie cannot be opened") )
var ( // ErrInvalidState means the callback was not started here, was already // used, or came back with another state. ErrInvalidState = errors.New("sociable: invalid state") // ErrAuthorization is what every provider refusal matches. ErrAuthorization = errors.New("sociable: the provider refused") // ErrAccessDenied is the refusal that means the person said no. ErrAccessDenied = fmt.Errorf("%w: access denied", ErrAuthorization) // ErrExchange means the code could not be exchanged for a token. ErrExchange = errors.New("sociable: token exchange failed") // ErrProfile means the provider would not describe the person. ErrProfile = errors.New("sociable: could not read the profile") // ErrIDToken means the ID token failed verification: signature, issuer, // audience, expiry or nonce. ErrIDToken = errors.New("sociable: the id token could not be verified") )
Functions ¶
func WithAPIURL ¶ added in v0.4.0
func WithAPIURL(url string) option
func WithClient ¶ added in v0.5.0
WithClient sets the HTTP client every call to a provider goes through, so a traced or timed-out transport there covers all of them.
func WithDriver ¶ added in v0.4.0
func WithDriver[T Provider](name string, credentials Credentials) option
WithDriver registers a provider type under a name.
func WithSession ¶ added in v0.4.0
func WithSession(session Session) option
Types ¶
type AuthorizationError ¶
AuthorizationError is a refusal the provider sent back on the redirect, in the shape RFC 6749 section 4.1.2.1 defines.
func (*AuthorizationError) Error ¶
func (self *AuthorizationError) Error() string
func (*AuthorizationError) Is ¶
func (self *AuthorizationError) Is(target error) bool
Is makes every refusal match ErrAuthorization, and access_denied match ErrAccessDenied as well.
type BitbucketProvider ¶ added in v0.4.0
type BitbucketProvider struct{}
BitbucketProvider reads the user document and the primary confirmed address from the emails endpoint, which is the only place Bitbucket puts it.
func (BitbucketProvider) Endpoint ¶ added in v0.4.0
func (self BitbucketProvider) Endpoint(Credentials) oauth2.Endpoint
func (BitbucketProvider) GetUserByToken ¶ added in v0.4.0
func (BitbucketProvider) MapUserToStruct ¶ added in v0.4.0
func (self BitbucketProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
func (BitbucketProvider) Scoping ¶ added in v0.4.0
func (self BitbucketProvider) Scoping() Scoping
type Credentials ¶
type Credentials struct {
ClientID string
ClientSecret string
// BaseURL is the provider's, for one that is self-hosted or an OpenID
// Connect issuer. A built-in provider has its own default.
BaseURL string
RedirectURL string
}
Credentials are what a provider issued the application.
type DriverContract ¶ added in v0.4.0
type DriverContract interface {
Scopes(scopes ...string) DriverContract
SetScopes(scopes ...string) DriverContract
With(params map[string]string) DriverContract
RedirectURL(url string) DriverContract
Stateless() DriverContract
Redirect(w http.ResponseWriter, r *http.Request) error
AuthURL(w http.ResponseWriter, r *http.Request) (string, error)
User(w http.ResponseWriter, r *http.Request) (*User, error)
Callback(w http.ResponseWriter, r *http.Request) (*oauth2.Token, error)
UserFromToken(ctx context.Context, token *oauth2.Token) (*User, error)
RefreshToken(ctx context.Context, refreshToken string) (*oauth2.Token, error)
}
DriverContract is what Driver hands back: Socialite's provider surface, the chain that shapes a redirect and the calls that run the flow. A fake implements it too.
type FacebookProvider ¶ added in v0.4.0
type FacebookProvider struct{}
FacebookProvider reads the Graph API's me document, with the appsecret_proof Facebook asks for from server-side apps.
func (FacebookProvider) Endpoint ¶ added in v0.4.0
func (self FacebookProvider) Endpoint(Credentials) oauth2.Endpoint
func (FacebookProvider) GetUserByToken ¶ added in v0.4.0
func (FacebookProvider) MapUserToStruct ¶ added in v0.4.0
func (self FacebookProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
func (FacebookProvider) Scoping ¶ added in v0.4.0
func (self FacebookProvider) Scoping() Scoping
type GitHubProvider ¶ added in v0.4.0
type GitHubProvider struct{}
GitHubProvider reads the profile and, when the profile hides the address, the primary verified one from the emails endpoint.
func (GitHubProvider) Endpoint ¶ added in v0.4.0
func (self GitHubProvider) Endpoint(Credentials) oauth2.Endpoint
func (GitHubProvider) GetUserByToken ¶ added in v0.4.0
func (GitHubProvider) MapUserToStruct ¶ added in v0.4.0
func (self GitHubProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
func (GitHubProvider) Scoping ¶ added in v0.4.0
func (self GitHubProvider) Scoping() Scoping
type GitLabProvider ¶ added in v0.4.0
type GitLabProvider struct{}
GitLabProvider signs in against gitlab.com, or a self-hosted instance given as BaseURL.
func (GitLabProvider) Endpoint ¶ added in v0.4.0
func (self GitLabProvider) Endpoint(credentials Credentials) oauth2.Endpoint
func (GitLabProvider) GetUserByToken ¶ added in v0.4.0
func (GitLabProvider) MapUserToStruct ¶ added in v0.4.0
func (self GitLabProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
func (GitLabProvider) Scoping ¶ added in v0.4.0
func (self GitLabProvider) Scoping() Scoping
type GoogleProvider ¶ added in v0.4.0
type GoogleProvider struct{}
GoogleProvider signs in through OpenID Connect. For a refresh token, redirect With(map[string]string{"access_type": "offline", "prompt": "consent"}).
func (GoogleProvider) Endpoint ¶ added in v0.4.0
func (self GoogleProvider) Endpoint(Credentials) oauth2.Endpoint
func (GoogleProvider) GetUserByToken ¶ added in v0.4.0
func (GoogleProvider) Issuer ¶ added in v0.4.0
func (self GoogleProvider) Issuer() string
func (GoogleProvider) MapUserToStruct ¶ added in v0.4.0
func (self GoogleProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
func (GoogleProvider) Scoping ¶ added in v0.4.0
func (self GoogleProvider) Scoping() Scoping
type LinkedInProvider ¶ added in v0.4.0
type LinkedInProvider struct{}
LinkedInProvider signs in through OpenID Connect.
func (LinkedInProvider) Endpoint ¶ added in v0.4.0
func (self LinkedInProvider) Endpoint(Credentials) oauth2.Endpoint
func (LinkedInProvider) GetUserByToken ¶ added in v0.4.0
func (LinkedInProvider) Issuer ¶ added in v0.4.0
func (self LinkedInProvider) Issuer() string
func (LinkedInProvider) MapUserToStruct ¶ added in v0.4.0
func (self LinkedInProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
func (LinkedInProvider) Scoping ¶ added in v0.4.0
func (self LinkedInProvider) Scoping() Scoping
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager is a configured set of drivers, Socialite's SocialiteManager. An application makes one with New at boot and hands it to its handlers; a test makes its own and fakes on it, so tests share nothing and run in parallel.
func (*Manager) Configure ¶ added in v0.4.0
func (self *Manager) Configure(options ...option)
Configure sets the manager up from scratch: the environment first, then the options over it.
func (*Manager) Driver ¶
func (self *Manager) Driver(name string) DriverContract
Driver picks a configured driver by name.
type Provider ¶
type Provider interface {
Endpoint(credentials Credentials) oauth2.Endpoint
Scoping() Scoping
// GetUserByToken fetches the provider's document for the person. The
// client already presents the token, the way Socialite's getHttpClient
// does. Socialite's getUserByToken.
GetUserByToken(ctx context.Context, client *http.Client, token *oauth2.Token, credentials Credentials) (*fastjson.Value, error)
// MapUserToStruct turns that document into a User. The driver calls it,
// and fills in the token afterwards. Socialite's mapUserToObject.
MapUserToStruct(json *fastjson.Value) (*User, error)
}
Provider is what a driver is made of, Socialite's four abstract methods: where to send the browser, what to ask for, how to read the raw document behind a token, and how to map it. The provider is a zero value, so what it needs to know comes in through the credentials.
type Session ¶
type Session interface {
Put(w http.ResponseWriter, r *http.Request, key, value string) error
Pull(w http.ResponseWriter, r *http.Request, key string) (string, error)
}
Session keeps the handshake between the redirect and the callback, the way Socialite keeps it in Laravel's session. The default is an encrypted cookie; an application with a session of its own hands that over with WithSession.
The value holds the PKCE verifier, so a Session must keep it server-side or encrypted, and bound to the browser that started the flow.
type SlackProvider ¶ added in v0.4.0
type SlackProvider struct{}
SlackProvider signs in through OpenID Connect. These are Slack's Sign in with Slack endpoints, not the workspace-app ones in x/oauth2.
func (SlackProvider) Endpoint ¶ added in v0.4.0
func (self SlackProvider) Endpoint(Credentials) oauth2.Endpoint
func (SlackProvider) GetUserByToken ¶ added in v0.4.0
func (SlackProvider) Issuer ¶ added in v0.4.0
func (self SlackProvider) Issuer() string
func (SlackProvider) MapUserToStruct ¶ added in v0.4.0
func (self SlackProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
func (SlackProvider) Scoping ¶ added in v0.4.0
func (self SlackProvider) Scoping() Scoping
type TwitchProvider ¶ added in v0.4.0
type TwitchProvider struct{}
TwitchProvider reads the Helix users document, which needs the client id on the request as well as the token.
func (TwitchProvider) Endpoint ¶ added in v0.4.0
func (self TwitchProvider) Endpoint(Credentials) oauth2.Endpoint
func (TwitchProvider) GetUserByToken ¶ added in v0.4.0
func (TwitchProvider) MapUserToStruct ¶ added in v0.4.0
func (self TwitchProvider) MapUserToStruct(json *fastjson.Value) (*User, error)
func (TwitchProvider) Scoping ¶ added in v0.4.0
func (self TwitchProvider) Scoping() Scoping
type User ¶
type User struct {
// ID is the provider's stable identifier for this person. Store this,
// not the email, which can change.
ID string
Nickname string
Name string
Email string
EmailVerified bool
Avatar string
// Token carries the access token, the refresh token when the provider
// issued one, and the expiry.
Token *oauth2.Token
// ApprovedScopes are the scopes the provider granted, which need not be
// the ones that were asked for.
ApprovedScopes []string
}
User is who the provider vouches for.
type XProvider ¶ added in v0.4.0
type XProvider struct{}
XProvider reads the users/me document. X issues an email only to apps approved for it.
func (XProvider) Endpoint ¶ added in v0.4.0
func (self XProvider) Endpoint(Credentials) oauth2.Endpoint
func (XProvider) GetUserByToken ¶ added in v0.4.0
func (XProvider) MapUserToStruct ¶ added in v0.4.0
Source Files
¶
- configuration.go
- driver.chain.go
- driver.go
- driver.redirect.go
- driver.token.go
- driver.user.go
- errors.go
- fake.go
- helpers.go
- provider.bitbucket.go
- provider.facebook.go
- provider.github.go
- provider.gitlab.go
- provider.go
- provider.google.go
- provider.linkedin.go
- provider.slack.go
- provider.twitch.go
- provider.x.go
- session.go
- sociable.go
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
Sign in with Google on net/http.
|
Sign in with Google on net/http. |
|
custom-provider
command
A provider of your own: any OpenID Connect issuer, given as BaseURL.
|
A provider of your own: any OpenID Connect issuer, given as BaseURL. |
|
testing
command
A sign-in handler and its test, with Fake standing in for GitHub.
|
A sign-in handler and its test, with Fake standing in for GitHub. |