gauth

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

README

gauth

[!NOTE] The implemenation only has been tested with the Authentik Auth provider. More information can be found here

An auth OIDC-based implemenation for SOARCA based using the GIN framework. Library provides convient functionality and middleware for the OIDCS token validation and redirects. Gauth uses encrypted stored cookies for storing the jwt-token client-side. For more information on secure cookies we refer to Gorilla. By default the life time of a stored session cookie is set to 8 hours, see COOKIE_LIFETIME under /cookies/cookie.go.

The library can be used in two modes:

  • OIDC Redirect mode: Provides the redirect functionality for the OICS flow
  • Token validation mode: Provides a middleware for token validation

In the examples section below more information is provided.

Installation

First, install the GAuth package:

go get github.com/COSSAS/gauth

Using gauth

Required Environment Variables for Basic Validation

For OIDC authentication:

  • OIDC_ISSUER: OIDC provider URL
  • OIDC_CLIENT_ID: Application client ID
  • OIDC_CLIENT_SECRET: Application client secret (for redirect mode)
Required Additional Environment variables for OIDC flow.
  • OIDC_REDIRECT_URL: "http://localhost:8081/auth/soarca_gui/callback"
  • COOKIE_SECRET_KEY: "SOME_COOKIE_SECRET" #openssl rand -base64 32 or head -c 32 /dev/urandom | base64 # OPTIONAL
  • OIDC_SKIP_TLS_VERIFY: Set to true for development (not recommended for production)
OIDC functionality:
  • gauth.OIDCRedirectToLogin(c *gin.Context): redirect unauthenticated users to OIDC login
  • gauth.OIDCCallBack(c *gin.Context, "/dashboard"): handle OIDC provider callback after authentication
  • gauth.Logout(c *gin.Context, "/login"): logout route to clear session and redirect
Middleware functionality:

gauth.LoadAuthContext(): Attempts to authenticate the user via session cookie or bearer token gauth.Middleware([]string):

  • Ensures the user is authenticated
  • Optional group-based authorization
  • Passes if no groups are specified
  • Requires user to be in ALL specified groups

Examples

Examples are located in the /examples/ directory. Real life implementation can be found here:

OIDC Redirect Mode example:
Basic OIDC Authentication
  • examples/basic/main.go: Demonstrates OIDC authentication configuration using:
    • Default configuration
    • Login and callback routes
    • Protected routes with middleware
    • Logout functionality
Token Validation example
  • examples/validation/main.go: Demonstrates the validation proces of token obtained through a jwt-token bearer, and validated against the OIDC provider.

Security Considerations

  • Always use HTTPS in production
  • Set OIDC_SKIP_TLS_VERIFY to false
  • Manage environment variables securely
  • Currently JWT-tokens are stored encrypted on the client-side.

Documentation

Index

Constants

View Source
const (
	DEFAULT_OIDC_CALLBACK_PATH   = "/oidc-callback"
	COOKIE_ENCRYPTION_KEY_LENGTH = 32
	COOKIE_SECRET_KEY_LENGTH     = 32
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Authenticator

type Authenticator struct {
	Cookiejar   cookies.ICookieJar
	OIDCconfig  *oidc.Config
	OauthConfig *oauth2.Config
	// contains filtered or unexported fields
}

func New

func New(config *Config) (*Authenticator, error)

func (*Authenticator) GetProvider

func (auth *Authenticator) GetProvider() *oidc.Provider

func (*Authenticator) GetTokenVerifier

func (auth *Authenticator) GetTokenVerifier() *oidc.IDTokenVerifier

func (*Authenticator) LoadAuthContext

func (auth *Authenticator) LoadAuthContext() gin.HandlerFunc

func (*Authenticator) Logout

func (auth *Authenticator) Logout(ginContext *gin.Context, redirectPath string)

func (*Authenticator) Middleware

func (auth *Authenticator) Middleware(requiredGroups []string) gin.HandlerFunc

func (*Authenticator) OIDCCallBack

func (auth *Authenticator) OIDCCallBack(ginContext *gin.Context, redirectPath string)

func (*Authenticator) OIDCRedirectToLogin

func (auth *Authenticator) OIDCRedirectToLogin(ginContext *gin.Context)

func (*Authenticator) VerifyClaims

func (auth *Authenticator) VerifyClaims(ginContext *gin.Context, token string) (*models.User, error)

type Config

type Config struct {
	Mode                ConfigMode
	IssuerUri           string
	ClientID            string
	ClientSecret        string
	SkipTLSValidation   bool
	OidcCallbackPath    string
	CookieJarSecret     string
	CookieEncryptionKey string
	RedirectURL         string
	Provider            Provider
}

func DefaultConfig

func DefaultConfig() *Config

func OIDCRedirectConfig

func OIDCRedirectConfig() *Config

type ConfigMode

type ConfigMode int
const (
	ModeVerify ConfigMode = iota
	ModeOIDCRedirect
)

type IAuth

type IAuth interface {
	Middleware(groups []string)
	LoadAuthContext() gin.HandlerFunc
	OIDCCallBack(ginContext *gin.Context, redirectPath string)
	OIDCRedirectToLogin(ginContext *gin.Context)
	Logout(ginContext *gin.Context)
}

type Provider

type Provider string
const (
	Generic   Provider = "Generic"
	Authentik Provider = "Authentik"
)

type UserClaimsConfig

type UserClaimsConfig struct {
	OIDCClaimUsernameField string
	OIDCClaimEmailField    string
	OIDCClaimNameField     string
	OIDCClaimGroupsField   string
}

func GetUserClaims

func GetUserClaims(provider Provider) *UserClaimsConfig

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳