Documentation
¶
Index ¶
- func ExampleCustomMiddleware(next http.Handler) http.Handler
- func ExampleMiddlewareUsage()
- func TokenFromContext(ctx context.Context) (*jwt.Token, bool)
- type AuthorizationCodeFlow
- func (flow *AuthorizationCodeFlow) AuthorizationCodeReceivedHandler(w http.ResponseWriter, r *http.Request)
- func (flow *AuthorizationCodeFlow) ExchangeCode(ctx context.Context, authorizationCode string, receivedState string) error
- func (flow *AuthorizationCodeFlow) ExchangeDeviceAccessToken(ctx context.Context, da *oauth2.DeviceAuthResponse, ...) error
- func (flow *AuthorizationCodeFlow) GetAuthURL() string
- func (flow *AuthorizationCodeFlow) GetClient(ctx context.Context) (*http.Client, error)
- func (flow *AuthorizationCodeFlow) GetToken(ctx context.Context) (*jwt.Token, error)
- func (flow *AuthorizationCodeFlow) InjectTokenMiddleware(next http.Handler) http.Handler
- func (flow *AuthorizationCodeFlow) IsAuthenticated(ctx context.Context) (bool, error)
- func (flow *AuthorizationCodeFlow) Logout() error
- func (flow *AuthorizationCodeFlow) StartDeviceAuth(ctx context.Context) (*oauth2.DeviceAuthResponse, error)
- type IAuthorizationCodeFlow
- type IDeviceAuthorizationFlow
- type ISessionHooks
- type Option
- func WithAdditionalScope(scope string) Option
- func WithAudience(audience string) Option
- func WithAuthParameter(name, value string) Option
- func WithClientID(clientID string) Option
- func WithClientSecret(clientSecret string) Option
- func WithCustomStateGenerator(stateFunc func(*AuthorizationCodeFlow) string) Option
- func WithOffline() Option
- func WithPKCE() Option
- func WithPKCEChallengeMethod(method string) Option
- func WithPrompt(prompt string) Option
- func WithScopes(scopes ...string) Option
- func WithSessionHooks(sessionHooks ISessionHooks) Option
- func WithTokenValidation(isValidateJWKS bool, tokenOptions ...func(*jwt.Token)) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExampleCustomMiddleware ¶ added in v0.1.3
ExampleCustomMiddleware shows how to create custom middleware that uses the token
func ExampleMiddlewareUsage ¶ added in v0.1.3
func ExampleMiddlewareUsage()
ExampleMiddlewareUsage demonstrates how to use the new middleware
Types ¶
type AuthorizationCodeFlow ¶
type AuthorizationCodeFlow struct {
JWKS_URL string
// contains filtered or unexported fields
}
AuthorizationCodeFlow represents the authorization code flow.
func (*AuthorizationCodeFlow) AuthorizationCodeReceivedHandler ¶ added in v0.0.5
func (flow *AuthorizationCodeFlow) AuthorizationCodeReceivedHandler(w http.ResponseWriter, r *http.Request)
AuthorizationCodeReceivedHandler handles the callback from the authorization server.
func (*AuthorizationCodeFlow) ExchangeCode ¶
func (flow *AuthorizationCodeFlow) ExchangeCode(ctx context.Context, authorizationCode string, receivedState string) error
Exchanges the authorization code for a token and established KindeContext
func (*AuthorizationCodeFlow) ExchangeDeviceAccessToken ¶ added in v0.0.3
func (flow *AuthorizationCodeFlow) ExchangeDeviceAccessToken(ctx context.Context, da *oauth2.DeviceAuthResponse, opts ...oauth2.AuthCodeOption) error
ExchangeDeviceAccessToken retrieves the access token for the device authorization flow.
func (*AuthorizationCodeFlow) GetAuthURL ¶
func (flow *AuthorizationCodeFlow) GetAuthURL() string
Returns the URL to redirect the user to start authentication pipeline.
func (*AuthorizationCodeFlow) GetClient ¶
Returns the client to make requests to the backend, will refresh token if offline is requested.
func (*AuthorizationCodeFlow) InjectTokenMiddleware ¶ added in v0.1.3
func (flow *AuthorizationCodeFlow) InjectTokenMiddleware(next http.Handler) http.Handler
InjectTokenMiddleware injects the token into the request context for downstream handlers
func (*AuthorizationCodeFlow) IsAuthenticated ¶ added in v0.0.3
func (flow *AuthorizationCodeFlow) IsAuthenticated(ctx context.Context) (bool, error)
func (*AuthorizationCodeFlow) Logout ¶ added in v0.0.3
func (flow *AuthorizationCodeFlow) Logout() error
func (*AuthorizationCodeFlow) StartDeviceAuth ¶ added in v0.0.3
func (flow *AuthorizationCodeFlow) StartDeviceAuth(ctx context.Context) (*oauth2.DeviceAuthResponse, error)
StartDeviceAuth retrieves the device authorization response. It returns the device authorization response or an error if the request fails. This is used for the device authorization flow.
type IAuthorizationCodeFlow ¶ added in v0.0.4
type IAuthorizationCodeFlow interface {
// Logout clears the session and token.
GetAuthURL() string
// Exchanges the authorization code for a token and establishes KindeContext.
ExchangeCode(ctx context.Context, authorizationCode string, receivedState string) error
// Returns http client to call external services, will refresh token behind the scenes if offline is requested.
GetClient(ctx context.Context) (*http.Client, error)
// Check if user is authenticated.
IsAuthenticated(context.Context) (bool, error)
// Clears local tokens and logs user out.
Logout() error
// A helper handler middleware for the code exchanger
AuthorizationCodeReceivedHandler(w http.ResponseWriter, r *http.Request)
// InjectTokenMiddleware that injects the token into the request context
InjectTokenMiddleware(next http.Handler) http.Handler
}
IAuthorizationCodeFlow represents the interface for the authorization code flow.
func NewAuthorizationCodeFlow ¶
func NewAuthorizationCodeFlow(baseURL string, clientID string, clientSecret string, callbackURL string, options ...Option) (IAuthorizationCodeFlow, error)
Creates a new AuthorizationCodeFlow with the given baseURL, clientID, clientSecret and options to authenticate backend applications.
type IDeviceAuthorizationFlow ¶ added in v0.0.4
type IDeviceAuthorizationFlow interface {
// StartDeviceAuth starts the device authorization flow.
StartDeviceAuth(ctx context.Context) (*oauth2.DeviceAuthResponse, error)
// Exchanges the device code to access token.
ExchangeDeviceAccessToken(ctx context.Context, da *oauth2.DeviceAuthResponse, opts ...oauth2.AuthCodeOption) error
// Returns http client to call external services, will refresh token behind the scenes if offline is requested.
GetClient(ctx context.Context) (*http.Client, error)
// Checks if the user is authenticated.
IsAuthenticated(context.Context) (bool, error)
// Clears local tokens and logs user out.
Logout() error
// Returns the token for the current session.
GetToken(context.Context) (*jwt.Token, error)
}
IDeviceAuthorizationFlow represents the interface for the device authorization flow.
func NewDeviceAuthorizationFlow ¶ added in v0.0.3
func NewDeviceAuthorizationFlow(baseURL string, options ...Option) (IDeviceAuthorizationFlow, error)
Creates a new AuthorizationCodeFlow with the given baseURL, clientID, clientSecret and options to authenticate backend applications.
type ISessionHooks ¶ added in v0.0.4
type ISessionHooks interface {
// SetRawToken stores the raw token in the session.
SetRawToken(token *oauth2.Token) error
// GetRawToken retrieves the raw token from the session.
GetRawToken() (*oauth2.Token, error)
// GetState retrieves the state from the session.
GetState() (string, error)
// SetState sets the state in the session.
SetState(state string) error
// SetPostAuthRedirect sets the post-authentication redirect URL in the session.
SetPostAuthRedirect(redirect string) error
// GetPostAuthRedirect retrieves the post-authentication redirect URL from the session.
GetPostAuthRedirect() (string, error)
// SetCodeVerifier stores the PKCE code verifier in the session.
SetCodeVerifier(codeVerifier string) error
// GetCodeVerifier retrieves the PKCE code verifier from the session.
GetCodeVerifier() (string, error)
}
ISessionHooks defines the interface for session management in the authorization code flow.
type Option ¶ added in v0.0.4
type Option func(*AuthorizationCodeFlow)
func WithAdditionalScope ¶ added in v0.0.3
Adds a scopes to the list of scopes to request, adds scope to existing list.
func WithAudience ¶
Adds an audience to the list of audiences to request.
func WithAuthParameter ¶
Adds an arbitrary parameter to the list of parameters to request.
func WithClientID ¶ added in v0.0.3
Integrates with the session management
func WithClientSecret ¶ added in v0.0.3
Integrates with the session management
func WithCustomStateGenerator ¶
func WithCustomStateGenerator(stateFunc func(*AuthorizationCodeFlow) string) Option
Adds the offline scope to the list of scopes to request.
func WithOffline ¶
func WithOffline() Option
Adds the offline scope to the list of scopes to request.
func WithPKCE ¶ added in v0.1.2
func WithPKCE() Option
Enables PKCE (Proof Key for Code Exchange) for enhanced security in public clients. This is recommended for applications that cannot securely store a client secret.
func WithPKCEChallengeMethod ¶ added in v0.1.2
func WithPrompt ¶ added in v0.0.3
Adds an audience to the list of audiences to request.
func WithScopes ¶ added in v0.0.3
Adds a scopes to the list of scopes to request, replaces value with the provided.
func WithSessionHooks ¶
func WithSessionHooks(sessionHooks ISessionHooks) Option
Integrates with the session management