core

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ClientCallbackURL is where the frontend will receive the OAuth result.
	ClientCallbackURL string

	// ProviderMap maps provider names to their OAuthProvider implementation.
	ProviderMap = map[string]OAuthProvider{}

	// UserDB is required to get user data from the database.
	UserDB UserDatabase
)

Dependencies of the core.

View Source
var (
	// ErrProviderNotFound is returned when the required provider is not supported.
	ErrProviderNotFound = errors.New("provider not found")
	// ErrUserNotFound is returned when the required user document is not present in the database.
	ErrUserNotFound = errors.New("user not found")
)

Functions

func OAuthCallback

func OAuthCallback(ctx context.Context, provider string, code string, writer http.ResponseWriter) error

OAuthCallback handles the callback received from the provider.

func OAuthRedirect

func OAuthRedirect(ctx context.Context, provider string, writer http.ResponseWriter) error

OAuthRedirect redirects the caller to the specified provider's auth page.

Types

type OAuthProvider

type OAuthProvider interface {
	// Name provides the name of the provider.
	Name() string

	// GetRedirectURL returns the URL to the auth page of the provider.
	GetRedirectURL(ctx context.Context) string

	// Code2Token converts the auth code to identity token.
	Code2Token(ctx context.Context, code string) (string, error)

	// Token2UserInfo converts the identity token into the user's info.
	Token2UserInfo(ctx context.Context, token string) (*UserDoc, error)
}

OAuthProvider represents a generic OAuth provider.

type UserDatabase

type UserDatabase interface {
	// SetUser upserts the user's info in the database.
	SetUser(ctx context.Context, doc *UserDoc) error

	// GetUserInfo fetches the user info for the provided userID.
	GetUser(ctx context.Context, userID string) (*UserDoc, error)
}

UserDatabase represents the database layer for user information.

type UserDoc

type UserDoc struct {
	ID          string `json:"_id" bson:"_id"`
	Email       string `json:"email" bson:"email"`
	FirstName   string `json:"first_name" bson:"first_name"`
	LastName    string `json:"last_name" bson:"last_name"`
	PictureLink string `json:"picture_link" bson:"picture_link"`
}

UserDoc is the schema of the user document as saved in the database.

func GetUser

func GetUser(ctx context.Context, userID string) (*UserDoc, error)

GetUser fetches the provided user's document from the database.

Jump to

Keyboard shortcuts

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