Documentation
¶
Overview ¶
Package elogin provides auth middle procedures for remote authentication including oauth2 and other protocols, and specific providers like google, facebook, etc.
Index ¶
- Variables
- type AuthAccessor
- type AuthHTTPWrapper
- func (wr *AuthHTTPWrapper) Callback(w http.ResponseWriter, r *http.Request)
- func (wr *AuthHTTPWrapper) HandleWrapper(prefix string) http.Handler
- func (wr *AuthHTTPWrapper) Login(w http.ResponseWriter, r *http.Request)
- func (wr *AuthHTTPWrapper) Protocol() string
- func (wr *AuthHTTPWrapper) Provider() string
- type ErrorHandler
- type RedirectParamsExtractor
- type SuccessHandler
- type Token
- type URLParam
- type UserData
Constants ¶
This section is empty.
Variables ¶
var (
ErrInvalidState = errors.New("invalid state")
)
Functions ¶
This section is empty.
Types ¶
type AuthAccessor ¶
type AuthAccessor interface {
// Provider returns the name of the authentication provider (e.g., "google", "github").
Provider() string
// Protocol returns the protocol used by the provider (e.g., "oauth2", "saml").
Protocol() string
// LoginURL generates the login URL with the provided URL parameters.
LoginURL(urlParams []URLParam) string
// UserData exchanges authentication values for user information and token.
// It takes a context, URL values from the callback, and URL parameters,
// returning the token, user data, or an error.
UserData(ctx context.Context, values url.Values, urlParams []URLParam) (*Token, *UserData, error)
}
AuthAccessor defines the interface for authentication providers. It handles OAuth/login flow operations like generating login URLs and exchanging codes for user data.
type AuthHTTPWrapper ¶
type AuthHTTPWrapper struct {
Auth AuthAccessor
Error ErrorHandler
Success SuccessHandler
RedirectParams RedirectParamsExtractor
}
AuthHTTPWrapper provides HTTP handler wrapping for authentication flows
func NewWrapper ¶
func NewWrapper(auth AuthAccessor, err ErrorHandler, success SuccessHandler, redirectParams RedirectParamsExtractor) *AuthHTTPWrapper
NewWrapper creates a new instance of AuthHTTPWrapper
func (*AuthHTTPWrapper) Callback ¶
func (wr *AuthHTTPWrapper) Callback(w http.ResponseWriter, r *http.Request)
Callback handles the provider callback and authenticates the user
func (*AuthHTTPWrapper) HandleWrapper ¶
func (wr *AuthHTTPWrapper) HandleWrapper(prefix string) http.Handler
HandleWrapper returns an HTTP handler for the authentication routes
func (*AuthHTTPWrapper) Login ¶
func (wr *AuthHTTPWrapper) Login(w http.ResponseWriter, r *http.Request)
Login handles the login request and redirects to the provider
func (*AuthHTTPWrapper) Protocol ¶
func (wr *AuthHTTPWrapper) Protocol() string
Protocol returns the authentication protocol name
func (*AuthHTTPWrapper) Provider ¶
func (wr *AuthHTTPWrapper) Provider() string
Provider returns the authentication provider name
type ErrorHandler ¶
type ErrorHandler interface {
Error(w http.ResponseWriter, r *http.Request, err error)
}
ErrorHandler defines the interface for handling authentication errors
type RedirectParamsExtractor ¶
type RedirectParamsExtractor interface {
RedirectParams(w http.ResponseWriter, r *http.Request, login bool) []URLParam
}
RedirectParamsExtractor defines the interface for extracting redirect parameters
type SuccessHandler ¶
type SuccessHandler interface {
Success(w http.ResponseWriter, r *http.Request, token *Token, data *UserData)
}
SuccessHandler defines the interface for handling successful authentication
type Token ¶
type Token struct {
TokenType string `json:"token_type,omitempty"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresAt time.Time `json:"expires_at,omitempty"`
Scopes []string `json:"scopes,omitempty"`
}
Token represents OAuth token data with access and refresh tokens.
type UserData ¶
type UserData struct {
// ID is the unique identifier of the user
ID string `json:"id"`
// Email is the user's email address
Email string `json:"email"`
// FirstName is the user's first name
FirstName string `json:"first_name"`
// LastName is the user's last name
LastName string `json:"last_name"`
// Username is the user's username or handle
Username string `json:"username"`
// AvatarURL is the URL to the user's avatar image
AvatarURL string `json:"avatar_url"`
// Link is the user's profile link or homepage URL
Link string `json:"link"`
// Ext contains additional provider-specific user data
Ext map[string]any `json:"ext,omitempty"`
// OAuth2conf is the OAuth2 configuration (excluded from JSON serialization)
OAuth2conf *oauth2.Config `json:"-"`
}
UserData represents the user data retrieved from an OAuth2 provider.