oauth

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const SessionName = "session-name"

Variables

View Source
var CompleteUserAuth = func(ctx *gin.Context) (goth.User, error) {
	providerName, err := GetProviderName(ctx)
	if err != nil {
		return goth.User{}, err
	}

	provider, err := goth.GetProvider(providerName)
	if err != nil {
		return goth.User{}, err
	}
	session, _ := store.Get(ctx.Request, SessionName)
	value := session.Values[providerName].(string)
	if value == "" {
		return goth.User{}, errors.New("session value for " + providerName + " not found")
	}

	sess, err := provider.UnmarshalSession(value)
	if err != nil {
		return goth.User{}, err
	}

	user, err := provider.FetchUser(sess)
	if err == nil {

		return user, err
	}

	_, err = sess.Authorize(provider, ctx.Request.URL.Query())
	if err != nil {
		return goth.User{}, err
	}

	session.Values[providerName] = sess.Marshal()
	return provider.FetchUser(sess)
}

CompleteUserAuth does what it says on the tin. It completes the authentication process and fetches all of the basic information about the user from the provider. It expects to be able to get the name of the provider from the query parameters as either "provider" or ":provider". See https://github.com/markbates/goth/examples/main.go to see this in action.

View Source
var GetProviderName = func(ctx *gin.Context) (string, error) {

	if p, ok := ctx.GetQuery("provider"); ok {
		return p, nil
	}

	if p := ctx.PostForm("provider"); p != "" {
		return p, nil
	}

	if p, _ := ctx.Get("provider"); p.(string) != "" {
		return p.(string), nil
	}

	return "", errors.New("you must select a provider")
}

GetProviderName is a function used to get the name of a provider for a given request. By default, this provider is fetched from the URL query string. If you provide it in a different way, assign your own function to this variable that returns the provider name for your request.

View Source
var GetState = func(ctx *gin.Context) string {
	return ctx.Query("state")
}

GetState gets the state returned by the provider during the callback. This is used to prevent CSRF attacks, see http://tools.ietf.org/html/rfc6749#section-10.12

View Source
var SetState = func(ctx *gin.Context) string {
	if state, ok := ctx.GetQuery("state"); ok {
		return state
	}

	return "state"

}

SetState sets the state string associated with the given request. If no state string is associated with the request, one will be generated. This state is sent to the provider and can be retrieved during the callback.

Functions

func BeginAuthHandler

func BeginAuthHandler(ctx *gin.Context)

BeginAuthHandler is a convenience handler for starting the authentication process. It expects to be able to get the name of the provider from the query parameters as either "provider" or ":provider". BeginAuthHandler will redirect the user to the appropriate authentication end-point for the requested provider. See https://github.com/markbates/goth/examples/main.go to see this in action.

func CallBack

func CallBack(ctx *gin.Context)

func GetAuthURL

func GetAuthURL(ctx *gin.Context) (string, error)

GetAuthURL starts the authentication process with the requested provided. It will return a URL that should be used to send users to. It expects to be able to get the name of the provider from the query parameters as either "provider" or ":provider" or from the context's value of "provider" key. I would recommend using the BeginAuthHandler instead of doing all of these steps yourself, but that's entirely up to you.

func Index

func Index(res http.ResponseWriter, req *http.Request)

func Logout

func Logout(ctx *gin.Context)

Logout invalidates a user session.

func RegisterOauthServiceHandlerServer

func RegisterOauthServiceHandlerServer(r *gin.Engine, server OauthServiceServer)

Types

type OauthServiceServer

type OauthServiceServer interface {
	OauthAuthorize(context.Context, *oauth.OauthReq) (*response.HttpResponse, error)
	OauthToken(context.Context, *oauth.OauthReq) (*response.HttpResponse, error)
}

Jump to

Keyboard shortcuts

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