goth_fiber

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 13 Imported by: 0

README

Goth-Fiber: Multi-Provider Authentication for Go GoDoc

Is wrapper for goth library to use with fiber Framework, provides a simple, clean, and idiomatic way to write authentication packages for Go web applications.

Unlike other similar packages, Goth, lets you write OAuth, OAuth2, or any other protocol providers, as long as they implement the Provider and Session interfaces.

Installation

$ go get github.com/Noooste/goth_fiber

Supported Providers

  • Amazon
  • Apple
  • Auth0
  • Azure AD
  • Battle.net
  • Bitbucket
  • Box
  • Cloud Foundry
  • Dailymotion
  • Deezer
  • Digital Ocean
  • Discord
  • Dropbox
  • Eve Online
  • Facebook
  • Fitbit
  • Gitea
  • GitHub
  • Gitlab
  • Google
  • Google+ (deprecated)
  • Heroku
  • InfluxCloud
  • Instagram
  • Intercom
  • Kakao
  • Lastfm
  • Linkedin
  • LINE
  • Mailru
  • Meetup
  • MicrosoftOnline
  • Naver
  • Nextcloud
  • OneDrive
  • OpenID Connect (auto discovery)
  • Paypal
  • SalesForce
  • Shopify
  • Slack
  • Soundcloud
  • Spotify
  • Steam
  • Strava
  • Stripe
  • Tumblr
  • Twitch
  • Twitter
  • Typetalk
  • Uber
  • VK
  • Wepay
  • Xero
  • Yahoo
  • Yammer
  • Yandex

Examples

See the examples folder for a working application that lets users authenticate through Twitter, Facebook, Google Plus etc.

To run the example either clone the source from GitHub

$ git clone git@github.com/Noooste/goth_fiber.git
$ go get github.com/Noooste/goth_fiber
$ cd goth_fiber/examples
$ go get -v
$ go build
$ ./examples

Now open up your browser and go to http://localhost:8088/login/google to see the example.

To actually use the different providers, please make sure you set environment variables. Example given in the examples/main.go file

Security Notes

By default, goth_fiber uses a Session from the gofiber/session package to store session data.

As configured, goth will generate cookies with the following session.Config:

    session.Config{
	    Expiration: 24 * time.Hour,
	    Storage:    memory.New(),
	    KeyLookup: "cookie:_gothic_session",
	    CookieDomain: "",
	    CookiePath: "",
	    CookieSecure: false,
	    CookieHTTPOnly: true,
	    CookieSameSite: "Lax",
	    KeyGenerator: utils.UUIDv4,
	}

To tailor these fields for your application, you can override the goth_fiber.SessionStore variable at startup.

The following snippet shows one way to do this:

    // optional config
    config := session.Config{
	    Expiration:     30 * time.Minutes,
	    Storage:        sqlite3.New(), // From github.com/gofiber/storage/sqlite3
	    KeyLookup:      "header:session_id",
	    CookieDomain:   "google.com",
	    CookiePath:     "/users",
	    CookieSecure:   os.Getenv("ENVIRONMENT") == "production",
	    CookieHTTPOnly: true, // Should always be enabled
	    CookieSameSite: "Lax",
	    KeyGenerator:   utils.UUIDv4,
	}

    // create session handler
    sessions := session.New(config)

    goth_fiber.SessionStore = sessions

Issues

Issues always stand a significantly better chance of getting fixed if they are accompanied by a pull request.

Contributing

Would I love to see more providers? Certainly! Would you love to contribute one? Hopefully, yes!

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Write Tests!
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

Documentation

Index

Constants

View Source
const ProviderParamKey key = iota

ProviderParamKey can be used as a key in context when passing in a provider

Variables

View Source
var (
	SessionStore  *session.Store
	ErrSessionNil = errors.New("goth/gothic: no SESSION_SECRET environment variable is set. The default cookie store is not available and any calls will fail. Ignore this warning if you are using a different store")
)

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

Functions

func BeginAuthHandler

func BeginAuthHandler(ctx fiber.Ctx) error

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 CompleteUserAuth

func CompleteUserAuth(ctx fiber.Ctx, options ...CompleteUserAuthOptions) (goth.User, error)

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".

This method automatically ends the session. You can prevent this behavior by passing in options. Please note that any options provided in addition to the first will be ignored.

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

func GetAuthURL

func GetAuthURL(ctx fiber.Ctx) (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 GetContextWithProvider

func GetContextWithProvider(ctx fiber.Ctx, provider string) fiber.Ctx

GetContextWithProvider returns a new request context containing the provider

func GetFromSession

func GetFromSession(key string, ctx fiber.Ctx) (string, error)

GetFromSession retrieves a previously-stored value from the session. If no value has previously been stored at the specified key, it will return an error.

func GetProviderName

func GetProviderName(ctx fiber.Ctx) (string, error)

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.

func GetState

func GetState(ctx fiber.Ctx) string

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

func Logout

func Logout(ctx fiber.Ctx) error

Logout invalidates a user session.

func SetState

func SetState(ctx fiber.Ctx) string

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.

func StoreInSession

func StoreInSession(key string, value string, ctx fiber.Ctx) error

StoreInSession stores a specified key/value pair in the session.

Types

type CompleteUserAuthOptions

type CompleteUserAuthOptions struct {
	// True if CompleteUserAuth should automatically end the user's session.
	//
	// Defaults to True.
	ShouldLogout bool
}

CompleteUserAuthOptions Options that affect how CompleteUserAuth works.

type Params

type Params struct {
	// contains filtered or unexported fields
}

func (*Params) Get

func (p *Params) Get(key string) string

Jump to

Keyboard shortcuts

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