Use FauxProvider for testing
masterFauxProvider is a mock authentication provider included in Goth specifically for testing other parts of the package. It is not intended for production use.repository·master·Indexed 27 days ago
https://github.com/markbates/gothA multi-provider authentication library for Go web applications that provides a unified interface for implementing OAuth, OAuth2, and other authentication protocols. It supports a wide range of providers including Google, GitHub, Discord, and Nextcloud, and includes the User struct for normalized profile data and the gothic session store for managing authentication state.
FauxProvider is a mock authentication provider included in Goth specifically for testing other parts of the package. It is not intended for production use.By default, gothic uses a CookieStore from the gorilla/sessions package with specific default options (Path: /, MaxAge: 30 days, HttpOnly: true, Secure: false).
You can override the gothic.Store variable at application startup to customize session behavior, such as setting a session secret, adjusting MaxAge, or enabling Secure cookies for production (HTTPS).
key := "" // Replace with your SESSION_SECRET or similar
maxAge := 86400 * 30 // 30 days
isProd := false // Set to true when serving over https
store := sessions.NewCookieStore([]byte(key))
store.MaxAge(maxAge)
store.Options.Path = "/"
store.Options.HttpOnly = true // HttpOnly should always be enabled
store.Options.Secure = isProd
gothic.Store = storeTo use the Nextcloud provider, you must create an OAuth 2.0 Client within your Nextcloud instance.
Settings -> Security -> OAuth 2.0 client in your Nextcloud admin settings.goth.http://localhost:3000/auth/nextcloud/callback.Client Identifier and Secret generated by Nextcloud. These are required for your application configuration.You can run the provided example application to see Goth in action. Note that you must set the necessary environment variables for the specific providers you wish to test (refer to examples/main.go for details).
$ git clone git@github.com:markbates/goth.git
$ cd goth/examples
$ go get -v
$ go build
$ ./examplesTo use Goth in your Go project, install it using go get:
$ go get github.com/markbates/gothYou can spin up a local Nextcloud instance for testing using Docker Compose and Traefik. Ensure you have an external network named traefik-web created.
Use the following docker-compose.yml configuration:
version: '2'
networks:
traefik-web:
external: true
services:
app:
image: nextcloud
restart: always
networks:
- traefik-web
labels:
- traefik.enable=true
- traefik.frontend.rule=Host:${NEXTCLOUD_DNS}
- traefik.docker.network=traefik-web
environment:
SQLITE_DATABASE: "database.sqlite3"
NEXTCLOUD_ADMIN_USER: admin
NEXTCLOUD_ADMIN_PASSWORD: admin
NEXTCLOUD_TRUSTED_DOMAINS: ${NEXTCLOUD_DNS}Start the environment by running:
NEXTCLOUD_DNS=goth.my.server.name docker-compose up -dDefault credentials for the instance will be admin / admin.
To run the default login example provided in the <goth>/examples directory, you must provide the Nextcloud instance URL, the Client Identifier, and the Client Secret via environment variables. You also need to provide a SESSION_SECRET.
NEXTCLOUD_URL=https://goth.my.server.name \
NEXTCLOUD_KEY=<your-key> \
NEXTCLOUD_SECRET=<your-secret> \
SESSION_SECRET=1 \
./examplesTo run the provider-specific tests, use the same environment variables required for the login example, but execute the Go test command.
NEXTCLOUD_URL=https://goth.my.server.name \
NEXTCLOUD_KEY=<your-key> \
NEXTCLOUD_SECRET=<your-secret> \
SESSION_SECRET=1 \
go test -vGoth supports a wide range of authentication providers including:
When building a new provider for Goth, you must implement the Session interface. This interface manages the authentication state by marshaling and persisting data between the start and end of the OAuth/OIDC authorization process.
Required methods:
GetAuthURL(): Returns the provider's authentication endpoint URL.Marshal(): Generates a string representation of the session state to be stored (e.g., in a cookie or database) between requests.Authorize(Provider, Params): Validates the data returned from the provider and returns an access token.type Session interface {
GetAuthURL() (string, error)
Marshal() string
Authorize(Provider, Params) (string, error)
}UseProviders to add one or more Provider implementations to the global Goth registry. This function can be called multiple times. If you register a provider with a name that already exists, the new implementation will overwrite the previous one.ContextForClient to wrap an *http.Client into a context.Context compatible with golang.org/x/oauth2. If the provided client is nil, it returns oauth2.NoContext.