oauth

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2020 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidOAuthKeys   = errors.New("Invalid OAuth keys file")
	ErrInvalidOAuthDomain = errors.New("Invalid OAuth Domain")
)
View Source
var AuthCallbackHandler = func(res http.ResponseWriter, req *http.Request) {
	ctx := req.Context()
	log := logger.Logger(ctx).WithField("Method", "oauth.AuthCallbackHandler")

	webAppUrl := appcontext.WebAppURL(ctx)

	user, err := gothic.CompleteUserAuth(res, req)
	if err != nil {
		log.WithError(err).Errorf("CompleteUserAuth failed")
		http.Error(res, err.Error(), http.StatusBadRequest)
		return
	}

	err = UpdateUserSession(ctx, req, res, user)
	if err != nil {
		log.WithError(err).Errorf("UpdateUserSession failed")
		http.Error(res, err.Error(), http.StatusBadRequest)
		return
	}
	http.Redirect(res, req, webAppUrl, http.StatusFound)
}

AuthCallbackHandler finalize oauth authentification

View Source
var AuthHandler = func(res http.ResponseWriter, req *http.Request) {
	ctx := req.Context()
	log := logger.Logger(ctx).WithField("Method", "oauth.AuthHandler")

	if user, err := gothic.CompleteUserAuth(res, req); err == nil {
		err = UpdateUserSession(ctx, req, res, user)
		if err != nil {
			log.WithError(err).Errorf("UpdateUserSession failed")
			http.Error(res, err.Error(), http.StatusBadRequest)
			return
		}
	} else {
		gothic.BeginAuthHandler(res, req)
	}
}

AuthHandler reuse oauth session or open a new one

View Source
var AuthLogoutHandler = func(res http.ResponseWriter, req *http.Request) {
	ctx := req.Context()
	log := logger.Logger(ctx).WithField("Method", "oauth.AuthLogoutHandler")
	webAppUrl := appcontext.WebAppURL(ctx)

	err := RemoveSession(ctx, req)
	if err != nil {
		log.WithError(err).
			Warning("RemoveSession failed")
		http.Error(res, err.Error(), http.StatusBadRequest)
		return
	}

	err = gothic.Logout(res, req)
	if err != nil {
		log.WithError(err).
			Warning("OAuth Logout failed")
		http.Error(res, err.Error(), http.StatusBadRequest)
		return
	}

	http.Redirect(res, req, webAppUrl, http.StatusFound)
}

AuthLogoutHandler close oauth session

Functions

func Init

func Init(options Options) error

func RegisterHandlers

func RegisterHandlers(ctx context.Context, server *mux.Router)

Register handlers for OAuth providers

func RemoveSession

func RemoveSession(ctx context.Context, req *http.Request) error

func UpdateUserSession

func UpdateUserSession(ctx context.Context, req *http.Request, w http.ResponseWriter, user goth.User) error

Types

type Options

type Options struct {
	Keys   string
	Domain string
}

Jump to

Keyboard shortcuts

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