Skip to content

Maintains an active keycloak session using gocloak

License

Notifications You must be signed in to change notification settings

helio/gocloaksession

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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/master/token.go for complete JWT struct.

Example

// 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 testing you need to start a docker container. Simply run following commands prior to starting the tests:

docker pull quay.io/keycloak/keycloak:latest
docker run -d \
    -e KEYCLOAK_ADMIN=admin \
    -e KEYCLOAK_ADMIN_PASSWORD=secret \
    -v "$(pwd)/testdata:/opt/keycloak/data/import" \
    -p 8080:8080 \
    --name gocloak-test \
    quay.io/keycloak/keycloak:latest start-dev --import-realm

go test

To remove running docker container after completion of tests:

docker stop gocloak-test
docker rm gocloak-test

About

Maintains an active keycloak session using gocloak

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%