goth: github.com/markbates/goth/gothic Index | Files

package gothic

import "github.com/markbates/goth/gothic"

Package gothic wraps common behaviour when using Goth. This makes it quick, and easy, to get up and running with Goth. Of course, if you want complete control over how things flow, in regards to the authentication process, feel free and use Goth directly.

See https://github.com/markbates/goth/examples/main.go to see this in action.

Index

Package Files

gothic.go

Constants

const SessionName = "_gothic_session"

SessionName is the key used to access the session store.

Variables

var CompleteUserAuth = func(res http.ResponseWriter, req *http.Request) (goth.User, error) {
    if !keySet && defaultStore == Store {
        fmt.Println("" /* 184 byte string literal not displayed */)
    }

    providerName, err := GetProviderName(req)
    if err != nil {
        return goth.User{}, err
    }

    provider, err := goth.GetProvider(providerName)
    if err != nil {
        return goth.User{}, err
    }

    value, err := getFromSession(providerName, req)
    if err != nil {
        return goth.User{}, err
    }

    defer Logout(res, req)

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

    err = validateState(req, sess)
    if err != nil {
        return goth.User{}, err
    }

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

        return user, err
    }

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

    err = storeInSession(providerName, sess.Marshal(), req, res)

    if err != nil {
        return goth.User{}, err
    }

    gu, err := provider.FetchUser(sess)
    return gu, err
}

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.

var GetProviderName = getProviderName

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.

var GetState = func(req *http.Request) string {
    return req.URL.Query().Get("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

var SetState = func(req *http.Request) string {
    state := req.URL.Query().Get("state")
    if len(state) > 0 {
        return state
    }

    nonceBytes := make([]byte, 64)
    for i := 0; i < 64; i++ {
        nonceBytes[i] = byte(gothicRand.Int63() % 256)
    }
    return base64.URLEncoding.EncodeToString(nonceBytes)
}

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.

var Store sessions.Store

Store can/should be set by applications using gothic. The default is a cookie store.

func BeginAuthHandler Uses

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

BeginAuthHandler is a convienence 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 GetAuthURL Uses

func GetAuthURL(res http.ResponseWriter, req *http.Request) (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".

I would recommend using the BeginAuthHandler instead of doing all of these steps yourself, but that's entirely up to you.

func Logout Uses

func Logout(res http.ResponseWriter, req *http.Request) error

Logout invalidates a user session.

Package gothic imports 11 packages (graph) and is imported by 58 packages. Updated 2017-10-17. Refresh now. Tools for package owners.