keycloak_middleware

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: Unlicense Imports: 8 Imported by: 0

README

Acknowledgements

Installation

Install using go module in your project

  go get github.com/wirnat/go-keycloak-middleware

Overview

  • Simple middleware for validating role from keycloak claims

  • Support for Echo handler

  • Support for Gin handler

  • Validate user using keycloak realm access

  • Validate user using keycloak resource access

Usage/Examples

//1.INIT Middleware instance
middleware := keycloak_middleware.NewKeyCloakMiddleware(keycloak_middleware.KeyCloakConfig{
	KeyCloakIP:   "localhost:8080",
	Realm:        "my-erp",
	ClientID:     "your client id",
	ClientSecret: "your client secret",
})

//2. Build a permission role
adminAccess := middleware.RealmAccess("admin_access").ResourceAccess("login").EchoGuard()

e := echo.New()

//3. implement it to your route
e.POST("login", func(c echo.Context) error {
	panic("something")
}, adminAccess)

e.Start(":8181")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewKeyCloakMiddleware

func NewKeyCloakMiddleware(config KeyCloakConfig, options ...func(cloak *gocloak.GoCloak)) *keyCloakMiddleware

Types

type EchoHook added in v1.0.3

type EchoHook func(ctx echo.Context, claims *jwt.MapClaims) error

type GinHook added in v1.0.3

type GinHook func(ctx *gin.Context, claims *jwt.MapClaims) error

type KeyCloakConfig

type KeyCloakConfig struct {
	KeyCloakIP         string `mapstructure:"key_cloak_ip" json:"key_cloak_ip" yaml:"key_cloak_ip"`
	Realm              string `mapstructure:"realm" json:"realm" yaml:"realm"`
	ClientID           string `mapstructure:"client_id" json:"client_id" yaml:"client_id"`
	ClientSecret       string `mapstructure:"client_secret" json:"client_secret" yaml:"client_secret"`
	RetrospectingToken bool   `json:"retrospecting_token"`
}

type Middleware

type Middleware interface {
	GetClaim(ctx context.Context, accessToken string) (claim *jwt.MapClaims, err error)
	ResourceAccess(args ...string) (r keyCloakMiddleware)
	RealmAccess(args ...string) (r keyCloakMiddleware)
	GinGuard(hook ...GinHook) gin.HandlerFunc
	EchoGuard(hook ...EchoHook) echo.MiddlewareFunc
	ValidateRealmAccess(claims jwt.MapClaims) (err error)
	ValidateResourceAccess(claims jwt.MapClaims) error
	ReturnGoCloak() *gocloak.GoCloak
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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