recloak

package module
v0.0.0-...-f8ea5f6 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2024 License: MIT Imports: 10 Imported by: 0

README

recloak

KeyCloak Fine-grained authorization library in Go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// TokenContextKey is a context key for the token
	TokenContextKey = tokenContextKey{}

	// ErrUnauthenticated is returned when the token is not found for a
	// context.
	ErrUnauthenticated = errors.New("unauthenticated")

	// ErrInvalidToken is returned when the token is invalid.
	ErrInvalidToken = errors.New("invalid token")
)

Functions

This section is empty.

Types

type APIError

type APIError = gocloak.APIError

A re-export of `gocloak.APIError` for convenience.

type Claims

type Claims struct {
	jwt.RegisteredClaims

	// Custom claims
	PreferredUsername string                `json:"preferred_username"`
	RealmAcess        RolesClaim            `json:"realm_access,omitempty"`
	ResourceAcess     map[string]RolesClaim `json:"resource_access,omitempty"`
}

Claims is a type that represents the claims of a JWT token

func ClaimsFromContext

func ClaimsFromContext(ctx context.Context) (*Claims, error)

ClaimsFromContext extracts the claims from the context.

func EnsureClaimsFromContext

func EnsureClaimsFromContext(ctx context.Context) *Claims

EnsureClaimsFromContext extracts the claims from the context or panics.

func (*Claims) GetAudience

func (c *Claims) GetAudience() (jwt.ClaimStrings, error)

func (*Claims) GetExpirationTime

func (c *Claims) GetExpirationTime() (*jwt.NumericDate, error)

func (*Claims) GetIssuedAt

func (c *Claims) GetIssuedAt() (*jwt.NumericDate, error)

func (*Claims) GetIssuer

func (c *Claims) GetIssuer() (string, error)

func (*Claims) GetNotBefore

func (c *Claims) GetNotBefore() (*jwt.NumericDate, error)

func (*Claims) GetSubject

func (c *Claims) GetSubject() (string, error)

type ClientConfig

type ClientConfig struct {
	AuthServerURL string `yaml:"authServerUrl"`
	Realm         string `yaml:"realm"`
	ClientID      string `yaml:"clientId"`
	ClientSecret  string `yaml:"clientSecret"`
}

ClientConfig is a struct to hold Keycloak client configuration.

func NewClientConfigFromURL

func NewClientConfigFromURL(u *url.URL) (*ClientConfig, error)

NewClientConfigFromURL creates a new `ClientConfig` from the given URL.

The URL must have the following format:

<scheme>://<client_id>:<client_secret>@<host>[:<port>]/<realm>

func (*ClientConfig) String

func (c *ClientConfig) String() string

func (*ClientConfig) ToURL

func (c *ClientConfig) ToURL() (*url.URL, error)

ToURL converts the `ClientConfig` to a URL.

func (*ClientConfig) UnmarshalYAML

func (c *ClientConfig) UnmarshalYAML(node *yaml.Node) (err error)

type ReCloak

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

ReCloak is a type that provides additional authorization capabilities over `gocloak` library

func NewClient

func NewClient(config *ClientConfig) (*ReCloak, error)

NewClient creates a new ReCloak instance

func (*ReCloak) Client

func (r *ReCloak) Client() *gocloak.GoCloak

Client returns the gocloak client

func (*ReCloak) Config

func (r *ReCloak) Config() *ClientConfig

Config returns the client configuration

func (*ReCloak) DecodeAccessToken

func (c *ReCloak) DecodeAccessToken(
	ctx context.Context,
	tokenString string,
) (Token, error)

DecodeAccessToken decodes a bearer access token and returns a Token instance

func (*ReCloak) GetRepresentation

func (r *ReCloak) GetRepresentation(ctx context.Context) (*gocloak.Client, error)

Gets client representation from the keycloak server.

func (*ReCloak) Login

func (r *ReCloak) Login(ctx context.Context) error

Login logs in the client

func (*ReCloak) Refresh

func (r *ReCloak) Refresh(ctx context.Context) error

Refresh refreshes the token

func (*ReCloak) RefreshIfExpired

func (r *ReCloak) RefreshIfExpired(ctx context.Context) error

RefreshIfExpired refreshes the token if it is expired

func (*ReCloak) Token

func (r *ReCloak) Token() *gocloak.JWT

Token returns the current token

type RolesClaim

type RolesClaim struct {
	Roles []string `json:"roles"`
}

RolesClaim is a type that represents the roles claim of a JWT token

func (RolesClaim) HasRole

func (c RolesClaim) HasRole(role string) bool

HasRole checks if the user has the given role.

type Token

type Token struct {
	*jwt.Token

	Claims *Claims // Custom claims
}

Token is a type that represents a JWT token.

func EnsureTokenFromContext

func EnsureTokenFromContext(ctx context.Context) Token

EnsureTokenFromContext returns a token from the context or panics

func TokenFromContext

func TokenFromContext(ctx context.Context) (Token, error)

TokenFromContext returns a token from the context

func (Token) WrapContext

func (t Token) WrapContext(ctx context.Context) context.Context

WrapContext wraps the claims in the context.

Directories

Path Synopsis
middleware

Jump to

Keyboard shortcuts

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