gocloaksession

package module
v1.13.2 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: Apache-2.0 Imports: 6 Imported by: 2

README

gocloaksession

This client uses: gocloak and resty

Installation

go get github.com/Clarilab/gocloaksession

Importing

import "github.com/Clarilab/gocloaksession"

Features

// GoCloakSession holds all callable methods
type GoCloakSession interface {
	// GetKeycloakAuthToken returns a JWT object, containing the AccessToken and more
	GetKeycloakAuthToken() (*gocloak.JWT, error)

	// Sets the Authentication Header for the response
	// Can be used as Middleware in resty
	AddAuthTokenToRequest(*resty.Client, *resty.Request) error

	// GetGoCloakInstance returns the currently used GoCloak instance.
	GetGoCloakInstance() gocloak.GoCloak

	// ForceAuthenticate ignores all checks and executes an authentication.
	ForceAuthenticate() error

	// ForceRefresh ignores all checks and executes a refresh.
	ForceRefresh() error
}

See https://github.com/Nerzal/gocloak/blob/main/token.go for complete JWT struct.

Examples

// Create a new session
session := NewSession(clientId, clientSecret, realm, uri)

// Authenticate or refresh the token
token, err := session.GetKeycloakAuthToken()

If you want to use it as middleware in resty, you can use the following example

session := NewSession(clientId, clientSecret, realm, uri)

restyClient.OnBeforeRequest(session.AddAuthTokenToRequest)

In case you need the GoCloak instance to execute your own commands.

gocloakInstance := session.GetGoCloakInstance()

Developing & Testing

For local development you need to start a docker container:

docker-compose up -d

To remove running docker container afterwards:

docker-compose down

To run the tests simply use:

make test-all

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FunctionalOption added in v1.11.0

type FunctionalOption func(*goCloakSession) error

FunctionalOption configures a Session

func PrematureRefreshThresholdOption added in v1.1.0

func PrematureRefreshThresholdOption(accessToken, refreshToken time.Duration) FunctionalOption

PrematureRefreshThresholdOption sets the threshold for a premature token refresh

func RequestSkipperCallOption added in v1.0.7

func RequestSkipperCallOption(requestSkipper RequestSkipper) FunctionalOption

RequestSkipperCallOption appends a RequestSkipper to the skipConditions

func SetGocloak added in v1.7.0

func SetGocloak(gc *gocloak.GoCloak) FunctionalOption

SetGocloak manually set a goCloak client.

func SetSkipRefreshToken added in v1.11.0

func SetSkipRefreshToken() FunctionalOption

SetSkipRefreshToken configures gocloakSession to skip refresh tokens.

func WithScopes added in v1.13.0

func WithScopes(scopes ...string) FunctionalOption

WithScopes sets the scopes to use when making requests.

func WithWildFlySupport added in v1.10.0

func WithWildFlySupport(uri string) FunctionalOption

WithWildFlySupport initializes gocloak client with legacy wildfly support.

type GoCloakSession

type GoCloakSession interface {
	// GetKeycloakAuthToken returns a JWT object, containing the AccessToken and more
	GetKeycloakAuthToken() (*gocloak.JWT, error)

	// AddAuthTokenToRequest sets the Authentication Header for the response
	AddAuthTokenToRequest(*resty.Client, *resty.Request) error

	// GetGoCloakInstance returns the currently used GoCloak instance.
	GetGoCloakInstance() *gocloak.GoCloak

	// ForceAuthenticate ignores all checks and executes an authentication.
	ForceAuthenticate() error

	// ForceRefresh ignores all checks and executes a refresh.
	ForceRefresh() error
}

GoCloakSession holds all callable methods

func NewSession

func NewSession(clientID, clientSecret, realm, uri string, option ...FunctionalOption) (GoCloakSession, error)

NewSession returns a new instance of a gocloak Session

type RequestSkipper added in v1.0.7

type RequestSkipper func(*resty.Request) bool

RequestSkipper is a function signature that can be used to skip a certain request if needed.

func SubstringRequestSkipper added in v1.0.7

func SubstringRequestSkipper(subStr string) RequestSkipper

SubstringRequestSkipper is a RequestSkipper that skips a request when the url in the request contains a certain substring

Jump to

Keyboard shortcuts

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