keycloak

package
v0.0.0-...-117364b Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultAdmin = ClientCredentials{ID: "admin-cli"}

* DefaultAdmin is the public "admin-cli" client Keycloak provisions in every * realm by default, with direct access grants already enabled -- the same * client gocloak's own LoginAdmin uses internally for its hardcoded admin * login. Pass this to CheckLoginUser unless the target realm needs a * different client.

View Source
var ErrUserNotFound = errors.New("user not found")

ErrUserNotFound is returned by Helper.GetUser when no user matches, distinguishing that case from a transport/auth failure so callers can branch on it with errors.Is.

Functions

func NewGlobalHelper

func NewGlobalHelper(opts ...Option) error

Types

type Auth

type Auth struct {
	Realm    string `json:"realm" yaml:"realm"`
	Username string `json:"username" yaml:"username"`
	Password string `json:"password" yaml:"password"`

	// ClientID/ClientSecret authenticate as a confidential client's service
	// account (client_credentials grant) instead of Username/Password.
	ClientID     string `json:"clientId" yaml:"clientId"`
	ClientSecret string `json:"clientSecret" yaml:"clientSecret"`
}

type Client

type Client interface {
	RestyClient() *resty.Client
	Login(context.Context, string, string, string, string, string) (*gocloak.JWT, error)
	LoginAdmin(context.Context, string, string, string) (*gocloak.JWT, error)
	LoginClient(ctx context.Context, clientID, clientSecret, realm string, scopes ...string) (*gocloak.JWT, error)
	GetUsers(context.Context, string, string, gocloak.GetUsersParams) ([]*gocloak.User, error)
	GetClients(context.Context, string, string, gocloak.GetClientsParams) ([]*gocloak.Client, error)
	CreateClient(context.Context, string, string, gocloak.Client) (string, error)
	CreateClientProtocolMapper(context.Context, string, string, string, gocloak.ProtocolMapperRepresentation) (string, error)
	GetClientSecret(ctx context.Context, token, realm, idOfClient string) (*gocloak.CredentialRepresentation, error)
	CreateUser(context.Context, string, string, gocloak.User) (string, error)
	SetPassword(context.Context, string, string, string, string, bool) error
	UpdateUser(context.Context, string, string, gocloak.User) error
	DeleteUser(context.Context, string, string, string) error
	GetClientRole(ctx context.Context, token string, realm string, idOfClient string, roleName string) (*gocloak.Role, error)
	AddClientRolesToUser(ctx context.Context, token string, realm string, idOfClient string, userID string, roles []gocloak.Role) error
	DeleteClientRolesFromUser(ctx context.Context, token string, realm string, idOfClient string, userID string, roles []gocloak.Role) error
	ExecuteActionsEmail(ctx context.Context, token, realm string, params gocloak.ExecuteActionsEmail) error

	/* Client mirrors methods gocloak.GoCloak already implements -- there's no
	 * implementation here because gocloak's real client satisfies this interface
	 * directly (assigned in SetKeycloakClient). This exists purely so mockery can
	 * generate a mock for tests.
	 */
	GetGroups(ctx context.Context, token string, realm string, params gocloak.GetGroupsParams) ([]*gocloak.Group, error)
	GetGroup(ctx context.Context, token string, realm string, groupID string) (*gocloak.Group, error)
	GetGroupByPath(ctx context.Context, token string, realm string, groupPath string) (*gocloak.Group, error)
	CreateGroup(ctx context.Context, token string, realm string, group gocloak.Group) (string, error)
	CreateChildGroup(ctx context.Context, token string, realm string, groupID string, group gocloak.Group) (string, error)
	GetUserGroups(ctx context.Context, token string, realm string, userID string, params gocloak.GetGroupsParams) ([]*gocloak.Group, error)
	GetGroupMembers(ctx context.Context, token string, realm string, groupID string, params gocloak.GetGroupsParams) ([]*gocloak.User, error)
	AddUserToGroup(ctx context.Context, token string, realm string, userID string, groupID string) error
	DeleteUserFromGroup(ctx context.Context, token string, realm string, userID string, groupID string) error
	GetClientRolesByUserID(ctx context.Context, token string, realm string, idOfClient string, userID string) ([]*gocloak.Role, error)
	GetUsersByClientRoleName(ctx context.Context, token string, realm string, idOfClient string, roleName string, params gocloak.GetUsersByRoleParams) ([]*gocloak.User, error)
	GetRealmRole(ctx context.Context, token string, realm string, roleName string) (*gocloak.Role, error)
	AddRealmRoleToUser(ctx context.Context, token string, realm string, userID string, roles []gocloak.Role) error
	LogoutUserSession(context.Context, string, string, string) error
	GetComponent(ctx context.Context, token string, realm string, componentID string) (*gocloak.Component, error)
}

type ClientCredentials

type ClientCredentials struct {
	ID     string
	Secret string
}

type Helper

type Helper struct {
	Client
	Token string

	Options
}

func GetGlobalHelper

func GetGlobalHelper() *Helper

func NewHelper

func NewHelper(opts ...Option) (*Helper, error)

func (*Helper) AddClientRolesToUser

func (h *Helper) AddClientRolesToUser(realm, clientId, userID string, roles []gocloak.Role) error

func (*Helper) AddRealmRoleToUser

func (h *Helper) AddRealmRoleToUser(realm, userID string, roles []gocloak.Role) error

func (*Helper) AddUserToGroup

func (h *Helper) AddUserToGroup(realm, userID, groupID string) error

func (*Helper) CheckLoginUser

func (h *Helper) CheckLoginUser(username, password string, client ClientCredentials) (*gocloak.JWT, error)

func (*Helper) CreateClient

func (h *Helper) CreateClient(realm string, opts gocloak.Client) (string, error)

func (*Helper) CreateClientProtocolMapper

func (h *Helper) CreateClientProtocolMapper(realm, clientId string, opts gocloak.ProtocolMapperRepresentation) (string, error)

func (*Helper) CreateUser

func (h *Helper) CreateUser(realm string, user gocloak.User) (string, error)

func (*Helper) DeleteClientRolesFromUser

func (h *Helper) DeleteClientRolesFromUser(realm, clientId, userID string, roles []gocloak.Role) error

func (*Helper) DeleteUser

func (h *Helper) DeleteUser(realm, userID string) error

func (*Helper) DeleteUserFromGroup

func (h *Helper) DeleteUserFromGroup(realm, userID, groupID string) error

func (*Helper) ExecuteActionsEmail

func (h *Helper) ExecuteActionsEmail(realm, userID string, actions []string) error

func (*Helper) GetClientRole

func (h *Helper) GetClientRole(realm, clientId, roleName string) (*gocloak.Role, error)

func (*Helper) GetClientSecret

func (h *Helper) GetClientSecret(realm, clientId string) (*gocloak.CredentialRepresentation, error)

func (*Helper) GetClients

func (h *Helper) GetClients(realm string, params gocloak.GetClientsParams) ([]*gocloak.Client, error)

func (*Helper) GetComponent

func (h *Helper) GetComponent(realm, componentID string) (*gocloak.Component, error)

func (*Helper) GetGroupByPath

func (h *Helper) GetGroupByPath(realm, path string) (*gocloak.Group, error)

func (*Helper) GetGroupMembers

func (h *Helper) GetGroupMembers(realm, groupID string) ([]*gocloak.User, error)

func (*Helper) GetGroups

func (h *Helper) GetGroups(realm string, params gocloak.GetGroupsParams) ([]*gocloak.Group, error)

func (*Helper) GetOrCreateGroupPath

func (h *Helper) GetOrCreateGroupPath(realm, path string) (*gocloak.Group, error)

func (*Helper) GetRealmRole

func (h *Helper) GetRealmRole(realm, roleName string) (*gocloak.Role, error)

func (*Helper) GetUser

func (h *Helper) GetUser(realm, name string) (*gocloak.User, error)

func (*Helper) GetUserByEmail

func (h *Helper) GetUserByEmail(realm, email string) (*gocloak.User, error)

func (*Helper) GetUserGroups

func (h *Helper) GetUserGroups(realm, userID string) ([]*gocloak.Group, error)

func (*Helper) GetUsersByClientRoleName

func (h *Helper) GetUsersByClientRoleName(realm, clientID, roleName string) ([]*gocloak.User, error)

func (*Helper) HasClientRole

func (h *Helper) HasClientRole(realm, clientID, userID, roleName string) (bool, error)

func (*Helper) LoginAdmin

func (h *Helper) LoginAdmin() error

func (*Helper) LoginServiceAccount

func (h *Helper) LoginServiceAccount() error

* LoginServiceAccount authenticates as a confidential client's service account * (client_credentials grant) instead of a human admin's username/password. * Unlike LoginAdmin, this has no separate expiry to manage beyond the resulting * access token itself: the client secret backing it doesn't expire on its own, * so a fresh token is always a re-login away.

func (*Helper) LogoutUserSession

func (h *Helper) LogoutUserSession(realm, sessionID string) error

func (*Helper) SetKeycloakClient

func (h *Helper) SetKeycloakClient() error

func (*Helper) SetPassword

func (h *Helper) SetPassword(realm, userID, password string) error

func (*Helper) UpdateUser

func (h *Helper) UpdateUser(realm string, user gocloak.User) error

type Host

type Host struct {
	Scheme                string `json:"scheme" yaml:"scheme"`
	Ip                    string `json:"ip" yaml:"ip"`
	Port                  int    `json:"port" yaml:"port"`
	Path                  string `json:"path" yaml:"path"`
	TlsInsecureSkipVerify bool   `json:"tlsInsecureSkipVerify" yaml:"tlsInsecureSkipVerify"`
}

type Option

type Option func(*Options)

func ClientID

func ClientID(clientID string) Option

func ClientSecret

func ClientSecret(clientSecret string) Option

func Insecure

func Insecure(insecure bool) Option

func Ip

func Ip(ip string) Option

func Password

func Password(password string) Option

func Path

func Path(path string) Option

func Port

func Port(port int) Option

func Realm

func Realm(realm string) Option

func Scheme

func Scheme(scheme string) Option

func Username

func Username(username string) Option

type Options

type Options struct {
	Host `json:"host" yaml:"host"`
	Auth `json:"auth" yaml:"auth"`
}

Jump to

Keyboard shortcuts

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