PluginsOAuth2 Plugin
Guide on how to set up Google authentication using Authula.
Obtain Google credentials
- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- Search for "APIs & Services", then go to "Credentials" in the left menu, click "Create Credentials" > "OAuth client ID".
- Choose "Web application", give it a name.
- Add authorized redirect URIs:
For production, replace localhost with the base URL of your Authula server (e.g.# Assuming base path is /api/auth http://localhost:8080/api/auth/oauth2/callback/googlehttps://api.yourdomain.com). - Note the Client ID and Client Secret.
Configure the provider
Standalone Mode
Add the following to your config.toml:
# SECURITY NOTE: It is recommended to set the 'client_secret' for each of these via their
# respective environment variables as shown in the .env.example file rather than hardcoding it here.
[plugins.oauth2.providers.google]
enabled = true
client_id = "your-client-id"
client_secret = "your-client-secret"
redirect_url = "http://localhost:8080/auth/oauth2/callback/google"
scopes = []Library Mode
import (
"os"
"fmt"
authula "github.com/Authula/authula"
authulaconfig "github.com/Authula/authula/config"
authulamodels "github.com/Authula/authula/models"
authulaenv "github.com/Authula/authula/env"
oauth2plugin "github.com/Authula/authula/plugins/oauth2"
oauth2plugintypes "github.com/Authula/authula/plugins/oauth2/types"
)
func main() {
config := authulaconfig.NewConfig(
authulaconfig.WithSecurity(authulamodels.SecurityConfig{
// Make sure to configure CORS and Trusted Origins appropriately
TrustedOrigins: []string{"your frontend origin"},
CORS: authulamodels.CORSConfig{
AllowCredentials: true,
AllowedOrigins: []string{"your frontend origin"},
},
}),
)
auth := authula.New(&authula.AuthConfig{
Config: config,
Plugins: []authulamodels.Plugin{
oauth2plugin.New(oauth2plugintypes.OAuth2PluginConfig{
Enabled: true,
Providers: map[string]oauth2plugintypes.ProviderConfig{
"google": {
Enabled: true,
ClientID: os.Getenv(authulaenv.EnvGoogleClientID),
ClientSecret: os.Getenv(authulaenv.EnvGoogleClientSecret),
RedirectURL: fmt.Sprintf("%s%s/oauth2/callback/google", config.BaseURL, config.BasePath),
},
},
}),
},
})
}Test the integration
- Start your Authula server.
- In your webapp, create a button when clicked on, it navigates the user to:
# (replace localhost with your Authula server URL in production). http://localhost:8080/auth/oauth2/authorize/google?redirect_to=<YOUR_REDIRECT_URL> - You should be redirected to Google's authorization page. After authorizing, you will be redirected back to your application.
