### Create New User with gocloak Source: https://pkg.go.dev/github.com/Nerzal/gocloak Example of creating a new user in Keycloak using gocloak. It logs in as admin, creates a user object, and calls CreateUser. Panics on errors. ```go gocloak := gocloak.NewClient("https://mycool.keycloak.instance") token, err := gocloak.LoginAdmin("user", "password", "realmName") if err != nil { panic("Something wrong with the credentials or url") } user := gocloak.User{ FirstName: "Bob", LastName: "Uncle", EMail: "something@really.wrong", Enabled: true, Username: "CoolGuy", } gocloak.CreateUser(token.AccessToken, "realm", user) if err != nil { panic("Oh no!, failed to create user :(") } ``` -------------------------------- ### Install gocloak v1 for Keycloak < 4.8 Source: https://pkg.go.dev/github.com/Nerzal/gocloak Use this command to install the gocloak v1 package when using Keycloak server versions below 4.8. ```bash go get gopkg.in/nerzal/gocloak.v1 ``` -------------------------------- ### GetCerts Source: https://pkg.go.dev/github.com/Nerzal/gocloak Gets the public keys for the given realm. ```APIDOC ## GetCerts ### Description Gets the public keys for the given realm. ### Method GetCerts ### Signature GetCerts(realm string) (*CertResponse, error) ### Parameters - **realm** (string) - Required - The realm name. ### Returns - **CertResponse** - The certificate response. - **error** - An error if the operation fails. ``` -------------------------------- ### GoCloak interface methods Source: https://pkg.go.dev/github.com/Nerzal/gocloak Defines the GoCloak interface with methods for interacting with Keycloak, such as getting users, groups, roles, and clients. ```go // GetKeyStoreConfig gets the keyStoreConfig GetKeyStoreConfig(accessToken string, realm string) (*KeyStoreConfig, error) // GetUserByID gets the user with the given id GetUserByID(accessToken string, realm string, userID string) (*User, error) // GetUser count returns the userCount of the given realm GetUserCount(accessToken string, realm string) (int, error) // GetUsers gets all users of the given realm GetUsers(accessToken string, realm string) (*[]User, error) // GetUserGroups gets the groups of the given user GetUserGroups(accessToken string, realm string, userID string) (*[]UserGroup, error) // GetComponents gets components of the given realm GetComponents(accessToken string, realm string) (*[]Component, error) // GetGroups gets all groups of the given realm GetGroups(accessToken string, realm string) (*[]Group, error) // GetGroup gets the given group GetGroup(accessToken string, realm, groupID string) (*Group, error) // GetRoles get all roles of the given realm GetRoles(accessToken string, realm string) (*[]Role, error) // GetRoleMappingByGroupID gets the rolemapping for the given group id GetRoleMappingByGroupID(accessToken string, realm string, groupID string) (*[]RoleMapping, error) // GetRoleMappingByUserID gets the rolemapping for the given user id GetRoleMappingByUserID(accessToken string, realm string, userID string) (*[]RoleMapping, error) // GetRolesByClientID gets roles for the given client GetRolesByClientID(accessToken string, realm string, clientID string) (*[]Role, error) // GetClients gets the clients in the realm GetClients(accessToken string, realm string) (*[]Client, error) // GetRealmRolesByUserID gets roles for the given uerID GetRealmRolesByUserID(accessToken string, realm string, userID string) (*[]Role, error) // GetRealmRolesByGroupID gets roles for given groupID GetRealmRolesByGroupID(accessToken string, realm string, groupID string) (*[]Role, error) // GetUsersByRoleName gets users for given roleName GetUsersByRoleName(token string, realm string, roleName string) (*[]User, error) // UserAttributeContains checks if the given attribute has the given value UserAttributeContains(attributes map[string][]string, attribute string, value string) bool } ``` -------------------------------- ### Introspect Token with gocloak Source: https://pkg.go.dev/github.com/Nerzal/gocloak Example of introspecting a token using gocloak. It logs in as a client, retrieves a token, and calls RetrospectToken to check if the token is active and get permissions. ```go client := NewClient(hostname) token, err := client.LoginClient(clientid, clientSecret, realm) if err != nil { panic("Login failed:"+ err.Error()) } rptResult, err := client.RetrospectToken(token.AccessToken, clientid, clientSecret, realm) if err != nil { panic("Inspection failed:"+ err.Error()) } if !rptResult.Active { panic("Token is not active") } permissions := rptResult.Permissions //Do something with the permissions ;) ``` -------------------------------- ### KeyStoreConfig struct Source: https://pkg.go.dev/github.com/Nerzal/gocloak Holds the key store configuration with active keys and key list. ```go type KeyStoreConfig struct { ActiveKeys ActiveKeys `json:"active"` Key []Key `json:"keys"` } ``` -------------------------------- ### CreateClient Source: https://pkg.go.dev/github.com/Nerzal/gocloak Creates a new client. ```APIDOC ## CreateClient ### Description Creates a new client. ### Method CreateClient ### Signature CreateClient(accessToken string, realm string, clientID Client) error ### Parameters - **accessToken** (string) - Required - The access token. - **realm** (string) - Required - The realm name. - **clientID** (Client) - Required - The client object. ### Returns - **error** - An error if the operation fails. ``` -------------------------------- ### CreateClientScope Source: https://pkg.go.dev/github.com/Nerzal/gocloak Creates a new clientScope. ```APIDOC ## CreateClientScope ### Description Creates a new clientScope. ### Method CreateClientScope ### Signature CreateClientScope(accessToken string, realm string, scope ClientScope) error ### Parameters - **accessToken** (string) - Required - The access token. - **realm** (string) - Required - The realm name. - **scope** (ClientScope) - Required - The client scope object. ### Returns - **error** - An error if the operation fails. ``` -------------------------------- ### NewClient function Source: https://pkg.go.dev/github.com/Nerzal/gocloak Creates a new GoCloak client with the given base path. ```go func NewClient(basePath string) GoCloak ``` -------------------------------- ### CreateUser Source: https://pkg.go.dev/github.com/Nerzal/gocloak Creates a new user. ```APIDOC ## CreateUser ### Description Creates a new user. ### Method CreateUser ### Signature CreateUser(token string, realm string, user User) (*string, error) ### Parameters - **token** (string) - Required - The access token. - **realm** (string) - Required - The realm name. - **user** (User) - Required - The user object. ### Returns - **string** - The ID of the created user. - **error** - An error if the operation fails. ``` -------------------------------- ### LoginAdmin Source: https://pkg.go.dev/github.com/Nerzal/gocloak Login as admin. ```APIDOC ## LoginAdmin ### Description Login as admin. ### Method LoginAdmin ### Signature LoginAdmin(username, password, realm string) (*JWT, error) ### Parameters - **username** (string) - Required - The admin username. - **password** (string) - Required - The admin password. - **realm** (string) - Required - The realm name. ### Returns - **JWT** - The JSON Web Token. - **error** - An error if the operation fails. ``` -------------------------------- ### GetComponents Source: https://pkg.go.dev/github.com/Nerzal/gocloak Retrieves all components in a realm. ```APIDOC ## GetComponents ### Description Gets components of the given realm. ### Method GET ### Endpoint /realms/{realm}/components ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **components** (array) - List of components #### Response Example [] ``` -------------------------------- ### GetClients Source: https://pkg.go.dev/github.com/Nerzal/gocloak Retrieves all clients in a realm. ```APIDOC ## GetClients ### Description Gets the clients in the realm. ### Method GET ### Endpoint /realms/{realm}/clients ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **clients** (array) - List of clients #### Response Example [] ``` -------------------------------- ### CreateComponent Source: https://pkg.go.dev/github.com/Nerzal/gocloak Creates a new component. ```APIDOC ## CreateComponent ### Description Creates a new component. ### Method CreateComponent ### Signature CreateComponent(accessToken string, realm string, component Component) error ### Parameters - **accessToken** (string) - Required - The access token. - **realm** (string) - Required - The realm name. - **component** (Component) - Required - The component object. ### Returns - **error** - An error if the operation fails. ``` -------------------------------- ### GetKeyStoreConfig Source: https://pkg.go.dev/github.com/Nerzal/gocloak Retrieves the keyStoreConfig for a given realm using the provided access token. ```APIDOC ## GetKeyStoreConfig ### Description Gets the keyStoreConfig for the given realm. ### Method GET ### Endpoint /realms/{realm}/keys ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **activeKeys** (object) - Active keys configuration - **key** (array) - List of keys #### Response Example { "activeKeys": {}, "key": [] } ``` -------------------------------- ### NewAuthenticationMiddleWare Source: https://pkg.go.dev/github.com/nerzal/gocloak-echo Constructor for the AuthenticationMiddleWare interface. ```APIDOC ## NewAuthenticationMiddleWare ### Description Instantiates a new AuthenticationMiddleWare. ### Signature ```go func NewAuthenticationMiddleWare(gocloak gocloak.GoCloak, realm, clientID, clientSecret, allowedScope string, customHeaderName *string) AuthenticationMiddleWare ``` ### Parameters - **gocloak** (gocloak.GoCloak) - Required - The gocloak client instance. - **realm** (string) - Required - The realm to use. - **clientID** (string) - Required - The client ID. - **clientSecret** (string) - Required - The client secret. - **allowedScope** (string) - Required - The allowed scope. - **customHeaderName** (*string) - Optional - The custom header name for token extraction. ### Returns - **AuthenticationMiddleWare** - The middleware for JWT validation. ``` -------------------------------- ### KeyRealm constant Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Defines a constant KeyRealm with the value "realm", likely used as a context key for storing the realm in echo middleware. ```go const ( KeyRealm = "realm" ) ``` -------------------------------- ### ObjectAllreadyExists struct and Error method Source: https://pkg.go.dev/github.com/Nerzal/gocloak Represents an error when Keycloak responds with 409 Conflict. ```go type ObjectAllreadyExists struct{} ``` ```go func (o *ObjectAllreadyExists) Error() string ``` -------------------------------- ### ProtocolMappers struct Source: https://pkg.go.dev/github.com/Nerzal/gocloak Represents protocol mappers configuration. ```go type ProtocolMappers struct { ID string `json:"id"` Name string `json:"name"` Protocol string `json:"protocol"` ProtocolMapper string `json:"protocolMapper"` ConsentRequired bool `json:"consentRequired"` ProtocolMappersConfig ProtocolMappersConfig `json:"config"` } ``` -------------------------------- ### ProtocolMappersConfig struct definition Source: https://pkg.go.dev/github.com/Nerzal/gocloak Defines the configuration for a protocol mapper, with JSON tags for userinfo token claim, user attribute, ID token claim, access token claim, claim name, and JSON type label. ```go type ProtocolMappersConfig struct { UserinfoTokenClaim string `json:"userinfo.token.claim"` UserAttribute string `json:"user.attribute"` IDTokenClaim string `json:"id.token.claim"` AccessTokenClaim string `json:"access.token.claim"` ClaimName string `json:"claim.name"` JSONTypeLabel string `json:"jsonType.label"` } ``` -------------------------------- ### RoleMapping struct definition Source: https://pkg.go.dev/github.com/Nerzal/gocloak Defines a role mapping with an ID, client, and a list of client role mappings. ```go type RoleMapping struct { ID string `json:"id"` Client string `json:"client"` Mappings []ClientRoleMappingRole `json:"mappings"` } ``` -------------------------------- ### CreateRole Source: https://pkg.go.dev/github.com/Nerzal/gocloak Creates a new role. ```APIDOC ## CreateRole ### Description Creates a new role. ### Method CreateRole ### Signature CreateRole(accessToken string, realm string, clientID string, role Role) error ### Parameters - **accessToken** (string) - Required - The access token. - **realm** (string) - Required - The realm name. - **clientID** (string) - Required - The client ID. - **role** (Role) - Required - The role object. ### Returns - **error** - An error if the operation fails. ``` -------------------------------- ### NewAuthenticationMiddleWare function Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Function signature for creating a new AuthenticationMiddleWare. It takes gocloak client, realm, client credentials, allowed scope, and an optional custom header name. ```go func NewAuthenticationMiddleWare(gocloak gocloak.GoCloak, realm, clientID, clientSecret, allowedScope string, customHeaderName *string) AuthenticationMiddleWare ``` -------------------------------- ### Authenticate struct Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Defines the Authenticate struct that holds authentication information such as client credentials, realm, scope, username, and password. Used for authenticating clients and users. ```go type Authenticate struct { ClientID string `json:"clientID"` ClientSecret string `json:"clientSecret"` Realm string `json:"realm,omitempty"` Scope string `json:"scope,omitempty"` UserName *string `json:"username,omitempty"` Password *string `json:"password,omitempty"` } ``` -------------------------------- ### IssuerResponse struct Source: https://pkg.go.dev/github.com/Nerzal/gocloak Represents the response from the issuer endpoint. ```go type IssuerResponse struct { Realm string `json:"realm"` PublicKey string `json:"public_key"` TokenService string `json:"token-service"` AccountService string `json:"account-service"` TokensNotBefore int `json:"tokens-not-before"` } ``` -------------------------------- ### GetUsers Source: https://pkg.go.dev/github.com/Nerzal/gocloak Retrieves all users in a realm. ```APIDOC ## GetUsers ### Description Gets all users of the given realm. ### Method GET ### Endpoint /realms/{realm}/users ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **users** (array) - List of users #### Response Example [ { "id": "string", "username": "string", "email": "string", "enabled": true } ] ``` -------------------------------- ### UserGroup struct definition Source: https://pkg.go.dev/github.com/Nerzal/gocloak Defines a user group with ID, name, and path. ```go type UserGroup struct { ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` Path string `json:"path,omitempty"` } ``` -------------------------------- ### Login Source: https://pkg.go.dev/github.com/Nerzal/gocloak Sends a request to the token endpoint using user and client credentials. ```APIDOC ## Login ### Description Sends a request to the token endpoint using user and client credentials. ### Method Login ### Signature Login(clientID string, clientSecret string, realm string, username string, password string) (*JWT, error) ### Parameters - **clientID** (string) - Required - The client ID. - **clientSecret** (string) - Required - The client secret. - **realm** (string) - Required - The realm name. - **username** (string) - Required - The username. - **password** (string) - Required - The password. ### Returns - **JWT** - The JSON Web Token. - **error** - An error if the operation fails. ``` -------------------------------- ### Group struct Source: https://pkg.go.dev/github.com/Nerzal/gocloak Represents a Keycloak group with ID, name, path, and sub-groups. ```go type Group struct { ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` Path string `json:"path,omitempty"` SubGroups []interface{} `json:"subGroups,omitempty"` } ``` -------------------------------- ### LoginClient Source: https://pkg.go.dev/github.com/Nerzal/gocloak Sends a request to the token endpoint using client credentials. ```APIDOC ## LoginClient ### Description Sends a request to the token endpoint using client credentials. ### Method LoginClient ### Signature LoginClient(clientID, clientSecret, realm string) (*JWT, error) ### Parameters - **clientID** (string) - Required - The client ID. - **clientSecret** (string) - Required - The client secret. - **realm** (string) - Required - The realm name. ### Returns - **JWT** - The JSON Web Token. - **error** - An error if the operation fails. ``` -------------------------------- ### NewAuthenticationHandler function Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Function signature for creating a new AuthenticationHandler. The realm parameter is optional. ```go func NewAuthenticationHandler(gocloak gocloak.GoCloak, realm *string) AuthenticationHandler ``` -------------------------------- ### Import gocloak v1 Source: https://pkg.go.dev/github.com/Nerzal/gocloak Import the gocloak v1 package in your Go code when using Keycloak versions below 4.8. ```go import "gopkg.in/nerzal/gocloak.v1" ``` -------------------------------- ### CreateGroup Source: https://pkg.go.dev/github.com/Nerzal/gocloak Creates a new group. ```APIDOC ## CreateGroup ### Description Creates a new group. ### Method CreateGroup ### Signature CreateGroup(accessToken string, realm string, group Group) error ### Parameters - **accessToken** (string) - Required - The access token. - **realm** (string) - Required - The realm name. - **group** (Group) - Required - The group object. ### Returns - **error** - An error if the operation fails. ``` -------------------------------- ### GetGroups Source: https://pkg.go.dev/github.com/Nerzal/gocloak Retrieves all groups in a realm. ```APIDOC ## GetGroups ### Description Gets all groups of the given realm. ### Method GET ### Endpoint /realms/{realm}/groups ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **groups** (array) - List of groups #### Response Example [ { "id": "string", "name": "string", "path": "string", "subGroups": [] } ] ``` -------------------------------- ### AuthenticateClient Source: https://pkg.go.dev/github.com/nerzal/gocloak-echo Authenticates a client using the provided authentication information. ```APIDOC ## AuthenticateClient ### Description Authenticates a client using the provided authentication information. ### Signature ```go AuthenticateClient(Authenticate) (*JWT, error) ``` ### Parameters - **auth** (Authenticate) - Required - The authentication information including client ID, client secret, realm, and scope. ### Returns - **JWT** - The JWT token on success. - **error** - An error if authentication fails. ``` -------------------------------- ### AuthenticateClient Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Authenticates a client using the provided authentication information and returns a JWT. ```APIDOC ## [AUTH] AuthenticateClient ### Description Authenticates a client using the provided authentication information and returns a JWT. ### Method AuthenticateClient ### Parameters #### Request Body - **auth** (Authenticate) - Required - Authentication information including ClientID, ClientSecret, Realm, Scope, UserName, and Password. ### Response #### Success Response - **JWT** - The JWT containing access token and refresh token. #### Response Example ```json { "accessToken": "string", "expiresIn": 0, "refreshExpiresIn": 0, "refreshToken": "string", "tokenType": "string", "notBeforePolicy": 0, "sessionState": "string", "scope": "string" } ``` ``` -------------------------------- ### User struct definition Source: https://pkg.go.dev/github.com/Nerzal/gocloak Defines a Keycloak user with fields for ID, timestamps, username, enabled, TOTP, email verification, names, email, federation link, attributes, credential types, required actions, and access. ```go type User struct { ID string `json:"id,omitempty"` CreatedTimestamp int64 `json:"createdTimestamp,omitempty"` Username string `json:"username,omitempty"` Enabled bool `json:"enabled,omitempty"` Totp bool `json:"totp,omitempty"` EmailVerified bool `json:"emailVerified,omitempty"` FirstName string `json:"firstName,omitempty"` LastName string `json:"lastName,omitempty"` Email string `json:"email,omitempty"` FederationLink string `json:"federationLink,omitempty"` Attributes map[string][]string `json:"attributes,omitempty"` DisableableCredentialTypes []interface{} `json:"disableableCredentialTypes,omitempty"` RequiredActions []interface{} `json:"requiredActions,omitempty"` Access map[string]bool `json:"access,omitempty"` } ``` -------------------------------- ### NewAuthenticationHandler Source: https://pkg.go.dev/github.com/nerzal/gocloak-echo Constructor for the AuthenticationHandler interface. ```APIDOC ## NewAuthenticationHandler ### Description Instantiates a new AuthenticationHandler. Setting realm is optional. ### Signature ```go func NewAuthenticationHandler(gocloak gocloak.GoCloak, realm *string) AuthenticationHandler ``` ### Parameters - **gocloak** (gocloak.GoCloak) - Required - The gocloak client instance. - **realm** (*string) - Optional - The realm to use. If nil, the realm from the Authenticate struct will be used. ### Returns - **AuthenticationHandler** - The handler for authentication operations. ``` -------------------------------- ### AuthenticationHandler interface Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Defines the AuthenticationHandler interface for authenticating with the API. It includes methods for client authentication, user authentication, and token refresh. ```go type AuthenticationHandler interface { AuthenticateClient(Authenticate) (*JWT, error) AuthenticateUser(Authenticate) (*JWT, error) RefreshToken(Refresh) (*JWT, error) } ``` -------------------------------- ### GetRoles Source: https://pkg.go.dev/github.com/Nerzal/gocloak Retrieves all roles in a realm. ```APIDOC ## GetRoles ### Description Get all roles of the given realm. ### Method GET ### Endpoint /realms/{realm}/roles ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **roles** (array) - List of roles #### Response Example [] ``` -------------------------------- ### CheckToken Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Middleware that checks the token. ```APIDOC ## [MIDDLEWARE] CheckToken ### Description Middleware that checks the token. ### Method CheckToken ### Parameters #### Parameters - **next** (echo.HandlerFunc) - Required - The next handler in the chain. ### Response #### Success Response - **echo.HandlerFunc** - The wrapped handler. ``` -------------------------------- ### JWT struct Source: https://pkg.go.dev/github.com/Nerzal/gocloak Represents a JSON Web Token (JWT) with standard fields. ```go type JWT struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` RefreshExpiresIn int `json:"refresh_expires_in"` RefreshToken string `json:"refresh_token"` TokenType string `json:"token_type"` NotBeforePolicy int `json:"not-before-policy"` SessionState string `json:"session_state"` Scope string `json:"scope"` } ``` -------------------------------- ### Role struct definition Source: https://pkg.go.dev/github.com/Nerzal/gocloak Defines a Keycloak role with fields for ID, name, scope parameter required, composite, client role, container ID, and description. ```go type Role struct { ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` ScopeParamRequired bool `json:"scopeParamRequired,omitempty"` Composite bool `json:"composite,omitempty"` ClientRole bool `json:"clientRole,omitempty"` ContainerID string `json:"containerId,omitempty"` Description string `json:"description,omitempty,omitempty"` } ``` -------------------------------- ### GoCloak interface definition Source: https://pkg.go.dev/github.com/Nerzal/gocloak Defines the GoCloak interface with methods for login, token handling, user/group/role/client management, and realm operations. Used as the main API contract for the library. ```go // GoCloak holds all methods a client should fullfill type GoCloak interface { Login(clientID string, clientSecret string, realm string, username string, password string) (*JWT, error) LoginClient(clientID, clientSecret, realm string) (*JWT, error) LoginAdmin(username, password, realm string) (*JWT, error) RequestPermission(clientID string, clientSecret string, realm string, username string, password string, permission string) (*JWT, error) RefreshToken(refreshToken string, clientID, clientSecret, realm string) (*JWT, error) DecodeAccessToken(accessToken string, realm string) (*jwt.Token, *jwt.MapClaims, error) DecodeAccessTokenCustomClaims(accessToken string, realm string, claims jwt.Claims) (*jwt.Token, error) RetrospectToken(accessToken string, clientID, clientSecret string, realm string) (*RetrospecTokenResult, error) GetIssuer(realm string) (*IssuerResponse, error) GetCerts(realm string) (*CertResponse, error) SetPassword(token string, userID string, realm string, password string, temporary bool) error CreateUser(token string, realm string, user User) (*string, error) CreateGroup(accessToken string, realm string, group Group) error CreateRole(accessToken string, realm string, clientID string, role Role) error CreateClient(accessToken string, realm string, clientID Client) error CreateClientScope(accessToken string, realm string, scope ClientScope) error CreateComponent(accessToken string, realm string, component Component) error UpdateUser(accessToken string, realm string, user User) error UpdateGroup(accessToken string, realm string, group Group) error UpdateRole(accessToken string, realm string, clientID string, role Role) error UpdateClient(accessToken string, realm string, clientID Client) error UpdateClientScope(accessToken string, realm string, scope ClientScope) error DeleteUser(accessToken string, realm, userID string) error DeleteComponent(accessToken string, realm, componentID string) error DeleteGroup(accessToken string, realm, groupID string) error DeleteRole(accessToken string, realm, clientID, roleName string) error DeleteClient(accessToken string, realm, clientID string) error DeleteClientScope(accessToken string, realm, scopeID string) error GetKeyStoreConfig(accessToken string, realm string) (*KeyStoreConfig, error) GetUserByID(accessToken string, realm string, userID string) (*User, error) GetUserCount(accessToken string, realm string) (int, error) GetUsers(accessToken string, realm string) (*[]User, error) GetUserGroups(accessToken string, realm string, userID string) (*[]UserGroup, error) GetComponents(accessToken string, realm string) (*[]Component, error) UserAttributeContains(attributes map[string][]string, attribute string, value string) bool GetGroups(accessToken string, realm string) (*[]Group, error) GetGroup(accessToken string, realm, groupID string) (*Group, error) GetRoles(accessToken string, realm string) (*[]Role, error) GetRoleMappingByGroupID(accessToken string, realm string, groupID string) (*[]RoleMapping, error) GetRoleMappingByUserID(accessToken string, realm string, userID string) (*[]RoleMapping, error) GetRolesByClientID(accessToken string, realm string, clientID string) (*[]Role, error) GetClients(accessToken string, realm string) (*[]Client, error) GetRealmRolesByUserID(accessToken string, realm string, userID string) (*[]Role, error) GetRealmRolesByGroupID(accessToken string, realm string, groupID string) (*[]Role, error) GetUsersByRoleName(token string, realm string, roleName string) (*[]User, error) } ``` -------------------------------- ### SetPasswordRequest struct definition Source: https://pkg.go.dev/github.com/Nerzal/gocloak Defines a request to set a new password, with type, temporary flag, and password value. ```go type SetPasswordRequest struct { Type string `json:"type"` Temporary bool `json:"temporary"` Password string `json:"value"` } ``` -------------------------------- ### AuthenticateUser Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Authenticates a user using the provided authentication information and returns a JWT. ```APIDOC ## [AUTH] AuthenticateUser ### Description Authenticates a user using the provided authentication information and returns a JWT. ### Method AuthenticateUser ### Parameters #### Request Body - **auth** (Authenticate) - Required - Authentication information including ClientID, ClientSecret, Realm, Scope, UserName, and Password. ### Response #### Success Response - **JWT** - The JWT containing access token and refresh token. #### Response Example ```json { "accessToken": "string", "expiresIn": 0, "refreshExpiresIn": 0, "refreshToken": "string", "tokenType": "string", "notBeforePolicy": 0, "sessionState": "string", "scope": "string" } ``` ``` -------------------------------- ### AuthenticateUser Source: https://pkg.go.dev/github.com/nerzal/gocloak-echo Authenticates a user using the provided authentication information. ```APIDOC ## AuthenticateUser ### Description Authenticates a user using the provided authentication information. ### Signature ```go AuthenticateUser(Authenticate) (*JWT, error) ``` ### Parameters - **auth** (Authenticate) - Required - The authentication information including client ID, client secret, realm, username, and password. ### Returns - **JWT** - The JWT token on success. - **error** - An error if authentication fails. ``` -------------------------------- ### RequestPermission Source: https://pkg.go.dev/github.com/Nerzal/gocloak Sends a request to the token endpoint with permission parameter. ```APIDOC ## RequestPermission ### Description Sends a request to the token endpoint with permission parameter. ### Method RequestPermission ### Signature RequestPermission(clientID string, clientSecret string, realm string, username string, password string, permission string) (*JWT, error) ### Parameters - **clientID** (string) - Required - The client ID. - **clientSecret** (string) - Required - The client secret. - **realm** (string) - Required - The realm name. - **username** (string) - Required - The username. - **password** (string) - Required - The password. - **permission** (string) - Required - The permission parameter. ### Returns - **JWT** - The JSON Web Token. - **error** - An error if the operation fails. ``` -------------------------------- ### AuthenticationHandler interface for gocloak-echo Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Defines the AuthenticationHandler interface for authenticating clients and users and refreshing tokens using the gocloak client. It is part of the gocloak-echo package and is used together with the keycloak client gocloak. ```go // AuthenticationHandler is used to authenticate with the api type AuthenticationHandler interface { AuthenticateClient(Authenticate) (*gocloak.JWT, error) AuthenticateUser(Authenticate) (*gocloak.JWT, error) RefreshToken(Refresh) (*gocloak.JWT, error) } ``` -------------------------------- ### AuthenticationMiddleWare interface with DecodeAndValidateToken Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Defines the AuthenticationMiddleWare interface for validating JWTs. It includes methods for decoding and validating tokens, checking tokens, checking tokens with custom headers, and checking scopes. The last two methods require higher client permissions in the realm. ```go type AuthenticationMiddleWare interface { // Decodes the token and checks if it is valid DecodeAndValidateToken(next echo.HandlerFunc) echo.HandlerFunc CheckToken(next echo.HandlerFunc) echo.HandlerFunc // The following 2 methods need higher permissions of the client in the realm CheckTokenCustomHeader(next echo.HandlerFunc) echo.HandlerFunc CheckScope(next echo.HandlerFunc) echo.HandlerFunc } ``` -------------------------------- ### Key struct Source: https://pkg.go.dev/github.com/Nerzal/gocloak Represents a cryptographic key used by Keycloak. ```go type Key struct { ProviderID string `json:"providerId"` ProviderPriority int `json:"providerPriority"` Kid string `json:"kid"` Status string `json:"status"` Type string `json:"type"` Algorithm string `json:"algorithm"` PublicKey string `json:"publicKey,omitempty"` Certificate string `json:"certificate,omitempty"` } ``` -------------------------------- ### UpdateClientScope Source: https://pkg.go.dev/github.com/Nerzal/gocloak Updates the given clientScope. ```APIDOC ## UpdateClientScope ### Description Updates the given clientScope. ### Method UpdateClientScope ### Signature UpdateClientScope(accessToken string, realm string, scope ClientScope) error ### Parameters - **accessToken** (string) - Required - The access token. - **realm** (string) - Required - The realm name. - **scope** (ClientScope) - Required - The client scope object. ### Returns - **error** - An error if the operation fails. ``` -------------------------------- ### UpdateClient Source: https://pkg.go.dev/github.com/Nerzal/gocloak Updates the given client. ```APIDOC ## UpdateClient ### Description Updates the given client. ### Method UpdateClient ### Signature UpdateClient(accessToken string, realm string, clientID Client) error ### Parameters - **accessToken** (string) - Required - The access token. - **realm** (string) - Required - The realm name. - **clientID** (Client) - Required - The client object. ### Returns - **error** - An error if the operation fails. ``` -------------------------------- ### GetUserCount Source: https://pkg.go.dev/github.com/Nerzal/gocloak Returns the total number of users in a realm. ```APIDOC ## GetUserCount ### Description Returns the user count of the given realm. ### Method GET ### Endpoint /realms/{realm}/users/count ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **count** (integer) - Number of users #### Response Example { "count": 0 } ``` -------------------------------- ### CheckTokenCustomHeader Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Middleware that checks the token using a custom header. Requires higher permissions of the client in the realm. ```APIDOC ## [MIDDLEWARE] CheckTokenCustomHeader ### Description Middleware that checks the token using a custom header. Requires higher permissions of the client in the realm. ### Method CheckTokenCustomHeader ### Parameters #### Parameters - **next** (echo.HandlerFunc) - Required - The next handler in the chain. ### Response #### Success Response - **echo.HandlerFunc** - The wrapped handler. ``` -------------------------------- ### CheckScope Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Middleware that checks the scope. Requires higher permissions of the client in the realm. ```APIDOC ## [MIDDLEWARE] CheckScope ### Description Middleware that checks the scope. Requires higher permissions of the client in the realm. ### Method CheckScope ### Parameters #### Parameters - **next** (echo.HandlerFunc) - Required - The next handler in the chain. ### Response #### Success Response - **echo.HandlerFunc** - The wrapped handler. ``` -------------------------------- ### AuthenticationMiddleWare interface for JWT validation Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Defines the AuthenticationMiddleWare interface for validating JWTs in echo handlers. It includes CheckToken and CheckScope methods that wrap echo handler functions. ```go // AuthenticationMiddleWare is used to validate the JWT type AuthenticationMiddleWare interface { CheckToken(next echo.HandlerFunc) echo.HandlerFunc CheckScope(next echo.HandlerFunc) echo.HandlerFunc } ``` -------------------------------- ### RetrospecTokenResult struct definition Source: https://pkg.go.dev/github.com/Nerzal/gocloak Defines the result of a token introspection, with fields for permissions, expiration, not-before, issued-at, audience, active status, auth time, JTI, and type. ```go type RetrospecTokenResult struct { Permissions map[string]string `json:"permissions,omitempty"` Exp int `json:"exp,omitempty"` Nbf int `json:"nbf,omitempty"` Iat int `json:"iat,omitempty"` Aud string `json:"aud,omitempty"` Active bool `json:"active,omitempty"` AuthTime int `json:"auth_time,omitempty"` Jti string `json:"jti,omitempty"` Type string `json:"typ,omitempty"` } ``` -------------------------------- ### GetIssuer Source: https://pkg.go.dev/github.com/Nerzal/gocloak Calls the issuer endpoint for the given realm. ```APIDOC ## GetIssuer ### Description Calls the issuer endpoint for the given realm. ### Method GetIssuer ### Signature GetIssuer(realm string) (*IssuerResponse, error) ### Parameters - **realm** (string) - Required - The realm name. ### Returns - **IssuerResponse** - The issuer response. - **error** - An error if the operation fails. ``` -------------------------------- ### DecodeAccessToken Source: https://pkg.go.dev/github.com/Nerzal/gocloak Decodes the access token. ```APIDOC ## DecodeAccessToken ### Description Decodes the access token. ### Method DecodeAccessToken ### Signature DecodeAccessToken(accessToken string, realm string) (*jwt.Token, *jwt.MapClaims, error) ### Parameters - **accessToken** (string) - Required - The access token. - **realm** (string) - Required - The realm name. ### Returns - **jwt.Token** - The decoded JWT token. - **jwt.MapClaims** - The claims from the token. - **error** - An error if the operation fails. ``` -------------------------------- ### DecodeAccessTokenCustomClaims Source: https://pkg.go.dev/github.com/Nerzal/gocloak Decodes the access token and fills the given claims. ```APIDOC ## DecodeAccessTokenCustomClaims ### Description Decodes the access token and fills the given claims. ### Method DecodeAccessTokenCustomClaims ### Signature DecodeAccessTokenCustomClaims(accessToken string, realm string, claims jwt.Claims) (*jwt.Token, error) ### Parameters - **accessToken** (string) - Required - The access token. - **realm** (string) - Required - The realm name. - **claims** (jwt.Claims) - Required - The claims to fill. ### Returns - **jwt.Token** - The decoded JWT token. - **error** - An error if the operation fails. ``` -------------------------------- ### UpdateUser Source: https://pkg.go.dev/github.com/Nerzal/gocloak Updates the given user. ```APIDOC ## UpdateUser ### Description Updates the given user. ### Method UpdateUser ### Signature UpdateUser(accessToken string, realm string, user User) error ### Parameters - **accessToken** (string) - Required - The access token. - **realm** (string) - Required - The realm name. - **user** (User) - Required - The user object. ### Returns - **error** - An error if the operation fails. ``` -------------------------------- ### CheckScope Source: https://pkg.go.dev/github.com/nerzal/gocloak-echo Middleware that checks the scope of the JWT token. ```APIDOC ## CheckScope ### Description Checks the scope of the JWT token. Requires higher permissions of the client in the realm. ### Signature ```go CheckScope(next echo.HandlerFunc) echo.HandlerFunc ``` ### Parameters - **next** (echo.HandlerFunc) - Required - The next handler in the chain. ### Returns - **echo.HandlerFunc** - The middleware handler. ``` -------------------------------- ### GetRoleMappingByUserID Source: https://pkg.go.dev/github.com/Nerzal/gocloak Retrieves the role mappings for a specific user. ```APIDOC ## GetRoleMappingByUserID ### Description Gets the rolemapping for the given user id. ### Method GET ### Endpoint /realms/{realm}/users/{userID}/role-mappings ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name - **userID** (string) - Required - The user's ID #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **roleMappings** (array) - List of role mappings #### Response Example [] ``` -------------------------------- ### CheckTokenCustomHeader Source: https://pkg.go.dev/github.com/nerzal/gocloak-echo Middleware that checks the JWT token from a custom header. ```APIDOC ## CheckTokenCustomHeader ### Description Checks the JWT token from a custom header. Requires higher permissions of the client in the realm. ### Signature ```go CheckTokenCustomHeader(next echo.HandlerFunc) echo.HandlerFunc ``` ### Parameters - **next** (echo.HandlerFunc) - Required - The next handler in the chain. ### Returns - **echo.HandlerFunc** - The middleware handler. ``` -------------------------------- ### DecodeAndValidateToken Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Middleware that decodes the token and checks if it is valid. ```APIDOC ## [MIDDLEWARE] DecodeAndValidateToken ### Description Middleware that decodes the token and checks if it is valid. ### Method DecodeAndValidateToken ### Parameters #### Parameters - **next** (echo.HandlerFunc) - Required - The next handler in the chain. ### Response #### Success Response - **echo.HandlerFunc** - The wrapped handler. ``` -------------------------------- ### GetGroup Source: https://pkg.go.dev/github.com/Nerzal/gocloak Retrieves a specific group by its ID. ```APIDOC ## GetGroup ### Description Gets the given group. ### Method GET ### Endpoint /realms/{realm}/groups/{groupID} ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name - **groupID** (string) - Required - The group's ID #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **id** (string) - Group ID - **name** (string) - Group name - **path** (string) - Group path - **subGroups** (array) - Subgroups #### Response Example { "id": "string", "name": "string", "path": "string", "subGroups": [] } ``` -------------------------------- ### SetPassword Source: https://pkg.go.dev/github.com/Nerzal/gocloak Sets a new password for the user with the given id. Needs elevated privileges. ```APIDOC ## SetPassword ### Description Sets a new password for the user with the given id. Needs elevated privileges. ### Method SetPassword ### Signature SetPassword(token string, userID string, realm string, password string, temporary bool) error ### Parameters - **token** (string) - Required - The access token. - **userID** (string) - Required - The user ID. - **realm** (string) - Required - The realm name. - **password** (string) - Required - The new password. - **temporary** (bool) - Required - Whether the password is temporary. ### Returns - **error** - An error if the operation fails. ``` -------------------------------- ### Refresh struct Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Defines the Refresh struct used to refresh a JWT. It contains client credentials, realm, and the refresh token. ```go type Refresh struct { ClientID string `json:"clientID"` ClientSecret string `json:"clientSecret"` Realm string `json:"realm,omitempty"` RefreshToken string `json:"refreshToken,omitempty"` } ``` -------------------------------- ### JWT struct Source: https://pkg.go.dev/github.com/Nerzal/gocloak-echo Defines the JWT struct representing a JSON Web Token with fields for access token, expiration times, refresh token, token type, not-before policy, session state, and scope. ```go type JWT struct { AccessToken string `json:"accessToken"` ExpiresIn int `json:"expiresIn"` RefreshExpiresIn int `json:"refreshExpiresIn"` RefreshToken string `json:"refreshToken"` TokenType string `json:"tokenType"` NotBeforePolicy int `json:"notBeforePolicy"` SessionState string `json:"sessionState"` Scope string `json:"scope"` } ``` -------------------------------- ### GetRoleMappingByGroupID Source: https://pkg.go.dev/github.com/Nerzal/gocloak Retrieves the role mappings for a specific group. ```APIDOC ## GetRoleMappingByGroupID ### Description Gets the rolemapping for the given group id. ### Method GET ### Endpoint /realms/{realm}/groups/{groupID}/role-mappings ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name - **groupID** (string) - Required - The group's ID #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **roleMappings** (array) - List of role mappings #### Response Example [] ``` -------------------------------- ### GetUserGroups Source: https://pkg.go.dev/github.com/Nerzal/gocloak Retrieves the groups associated with a specific user. ```APIDOC ## GetUserGroups ### Description Gets the groups of the given user. ### Method GET ### Endpoint /realms/{realm}/users/{userID}/groups ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name - **userID** (string) - Required - The user's ID #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **groups** (array) - List of groups #### Response Example [ { "id": "string", "name": "string", "path": "string", "subGroups": [] } ] ``` -------------------------------- ### DeleteClient Source: https://pkg.go.dev/github.com/Nerzal/gocloak Deletes the given client. ```APIDOC ## DeleteClient ### Description Deletes the given client. ### Method DeleteClient ### Signature DeleteClient(accessToken string, realm, clientID string) error ### Parameters - **accessToken** (string) - Required - The access token. - **realm** (string) - Required - The realm name. - **clientID** (string) - Required - The client ID. ### Returns - **error** - An error if the operation fails. ``` -------------------------------- ### GetUserByID Source: https://pkg.go.dev/github.com/Nerzal/gocloak Retrieves a user by their ID within a realm. ```APIDOC ## GetUserByID ### Description Gets the user with the given id. ### Method GET ### Endpoint /realms/{realm}/users/{userID} ### Parameters #### Path Parameters - **realm** (string) - Required - The realm name - **userID** (string) - Required - The user's ID #### Query Parameters - **accessToken** (string) - Required - Access token for authorization ### Response #### Success Response (200) - **id** (string) - User ID - **username** (string) - Username - **email** (string) - Email - **enabled** (boolean) - Whether the user is enabled #### Response Example { "id": "string", "username": "string", "email": "string", "enabled": true } ```