openauth2

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2025 License: GPL-2.0 Imports: 40 Imported by: 0

Documentation

Index

Constants

View Source
const (
	USER_ID_SESSION_KEY = "openauth2_user_id"

	ErrUnknownProvider errs.Error = "Unknown provider"
)
View Source
const (
	ErrUserNil errs.Error = "User is nil"
)

Variables

This section is empty.

Functions

func AddUserMiddleware

func AddUserMiddleware() mux.Middleware

Add a user to a request, if one exists in the session.

func Client

func Client(u *openauth2models.User) (*http.Client, error)

Create a new HTTP client with the token source for the user.

This can be used to make requests to the API of the provider.

The client will automatically refresh the access token when it expires.

It will also update the user with the new token in the database.

func Logout

func Logout(r *http.Request) error

func NewAppConfig

func NewAppConfig(cnf Config) django.AppConfig

func RefreshTokens

func RefreshTokens(u *openauth2models.User) (*oauth2.Token, error)

RefreshTokens refreshes the tokens for a user.

It will return the new token and an error if one occurred.

It will also update the user with the new token in the database.

func UnAuthenticatedUser

func UnAuthenticatedUser() *openauth2models.User

func UserFromRequest

func UserFromRequest(r *http.Request) *openauth2models.User

Get the user from a request.

func UserFromRequestPure

func UserFromRequestPure(r *http.Request) authentication.User

Types

type AuthConfig

type AuthConfig struct {
	// The base oauth2 config to use.
	//
	// Under the hood the `golang.org/x/oauth2` package is used.
	Oauth2 *oauth2.Config

	// The access type to request from the provider.
	//
	// If this is nil, it will be set to "oauth2.AccessTypeOffline".
	AccessType oauth2.AuthCodeOption

	// The state to use when generating the URL
	// with Oauth2.AuthCodeURL.
	//
	// If this is left empty, it will default to "state"
	State string

	// The name of the provider, e.g. "google", "github", etc.
	Provider string

	// A nice name for the provider, e.g. "Google", "GitHub", etc.
	//
	// This is used for display purposes only.
	ProviderNiceName string

	// The URL of the documentation for the provider.
	//
	// This can be used to link to the provider's documentation.
	DocumentationURL string

	// ExtraParams are extra parameters to be set on the URL when
	// generating the url with Oauth2.AuthCodeURL
	ExtraParams map[string]string

	// An optional URL for the provider's logo.
	//
	// This is used for display purposes only.
	//
	// It is a function so it can possibly callback to django.Static(path).
	ProviderLogoURL func() string

	// DataStructURL is the URL which will be used to retrieve the data from the provider.
	//
	// This will then be used to scan the data into the DataStruct fields.
	// The URL must be a valid URL and must return a JSON object.
	DataStructURL string

	// DataStructIdentifier retrieves the unique identifier from the data struct.
	//
	// This is used to identify the user in the database.
	//
	// It has to be a function that takes the data struct and returns a string.
	DataStructIdentifier func(token *oauth2.Token, dataStruct interface{}) (string, error)

	// DataStruct is the struct that will be used to store the data returned by the provider.
	//
	// It will be copied by means of reflection, using the reprint package.
	// This means that it DOES support unexported fields, though these will
	// NOT be used for JSON unmarshalling.
	DataStruct interface{}

	// UserToString is a function that takes a user and returns a string.
	//
	// It can act on the user's data struct to return a string.
	// It is used for display purposes only.
	UserToString func(user *openauth2models.User, dataStruct interface{}) string
}

func (*AuthConfig) DataForUser

func (c *AuthConfig) DataForUser(user *openauth2models.User) (interface{}, error)

func (*AuthConfig) ReadableName

func (c *AuthConfig) ReadableName() string

func (*AuthConfig) ScanStruct

func (c *AuthConfig) ScanStruct(r io.Reader) (interface{}, error)

type Config

type Config struct {
	// The base URL for the callback URL. This is used to generate the redirect URL for the OAuth2 provider.
	//
	// This should be the base URL of your application, e.g. "https://example.com/"
	BaseCallbackURL string

	// A list of authentication configurations for the providers.
	AuthConfigurations []AuthConfig

	// If the user's state should be inactive by default.
	UserDefaultIsDisabled bool

	// A function to generate the default URL after the user has logged in.
	//
	// Note:
	//  If this is not set, the default URL will be "/".
	// 	A redirect URL might also be stored in a HTTP-only cookie, if present the cookie's URL will be used instead.
	RedirectAfterLogin func(user *openauth2models.User, datastruct interface{}, r *http.Request) string

	// A function to generate the default URL after the user has logged out.
	RedirectAfterLogout func(r *http.Request) string
}

type OpenAuth2AppConfig

type OpenAuth2AppConfig struct {
	*apps.DBRequiredAppConfig
	Config *Config
	// contains filtered or unexported fields
}
var (
	App *OpenAuth2AppConfig
)

func (*OpenAuth2AppConfig) AdminLoginHandler

func (oa *OpenAuth2AppConfig) AdminLoginHandler(w http.ResponseWriter, r *http.Request)

func (*OpenAuth2AppConfig) AuthHandler

func (oa *OpenAuth2AppConfig) AuthHandler(w http.ResponseWriter, r *http.Request, a *AuthConfig)

func (*OpenAuth2AppConfig) CallbackHandler

func (oa *OpenAuth2AppConfig) CallbackHandler(w http.ResponseWriter, r *http.Request, a *AuthConfig)

func (*OpenAuth2AppConfig) LoginHandler

func (oa *OpenAuth2AppConfig) LoginHandler(w http.ResponseWriter, r *http.Request)

func (*OpenAuth2AppConfig) LogoutHandler

func (oa *OpenAuth2AppConfig) LogoutHandler(w http.ResponseWriter, r *http.Request)

func (*OpenAuth2AppConfig) Provider

func (a *OpenAuth2AppConfig) Provider(name string) (*AuthConfig, error)

func (*OpenAuth2AppConfig) Providers

func (a *OpenAuth2AppConfig) Providers() []AuthConfig

func (*OpenAuth2AppConfig) Querier

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL