github.com/brioux/go-keycloak@v0.0.0-20240929191119-b54a3a01d90b/user.go (about)

     1  package keycloak
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  )
     7  
     8  // AdminUserService handles communication with keycloak user management
     9  type AdminUserService service
    10  
    11  // User represents the Keycloak user
    12  type User struct {
    13  	Access                     *map[string]interface{} `json:"access,omitempty"`
    14  	Attributes                 *map[string]interface{} `json:"attributes,omitempty"`
    15  	ClientConsents             *[]UserConsent          `json:"clientConsents,omitempty"`
    16  	ClientRoles                *map[string]interface{} `json:"clientRoles,omitempty"`
    17  	CreatedTimestamp           *int64                  `json:"createdTimestamp,omitempty"`
    18  	Credentials                *[]Credential           `json:"credentials,omitempty"`
    19  	DisableableCredentialTypes *[]string               `json:"disableableCredentialTypes,omitempty"`
    20  	Email                      *string                 `json:"email,omitempty"`
    21  	EmailVerified              *bool                   `json:"emailVerified,omitempty"`
    22  	Enabled                    *bool                   `json:"enabled,omitempty"`
    23  	FederatedIdentities        *[]FederatedIdentity    `json:"federatedIdentities,omitempty"`
    24  	FederationLink             *string                 `json:"federationLink,omitempty"`
    25  	FirstName                  *string                 `json:"firstName,omitempty"`
    26  	Groups                     *[]string               `json:"groups,omitempty"`
    27  	ID                         *string                 `json:"id,omitempty"`
    28  	LastName                   *string                 `json:"lastName,omitempty"`
    29  	NotBefore                  *int32                  `json:"notBefore,omitempty"`
    30  	Origin                     *string                 `json:"origin,omitempty"`
    31  	RealmRoles                 *[]string               `json:"realmRoles,omitempty"`
    32  	RequiredActions            *[]string               `json:"requiredActions,omitempty"`
    33  	Self                       *string                 `json:"self,omitempty"`
    34  	ServiceAccountClientID     *string                 `json:"serviceAccountClientId,omitempty"`
    35  	Username                   *string                 `json:"username,omitempty"`
    36  }
    37  
    38  // FederatedIdentity represents third party signups
    39  type FederatedIdentity struct {
    40  	IdentityProvider *string `json:"identityProvider,omitempty"`
    41  	UserID           *string `json:"userId,omitempty"`
    42  	UserName         *string `json:"userName,omitempty"`
    43  }
    44  
    45  // UserConsent represents scopes that have been consented
    46  type UserConsent struct {
    47  	ClientID               *string                 `json:"clientId,omitempty"`
    48  	CreatedDate            *int64                  `json:"createdDate,omitempty"`
    49  	GrantedClientRoles     *map[string]interface{} `json:"grantedClientRoles,omitempty"`
    50  	GrantedProtocolMappers *map[string]interface{} `json:"grantedProtocolMappers,omitempty"`
    51  	GrantedRealmRoles      *[]string               `json:"grantedRealmRoles,omitempty"`
    52  	LastUpdatedDate        *int64                  `json:"lastUpdatedDate,omitempty"`
    53  }
    54  
    55  // Credential represents the user's credentials type
    56  type Credential struct {
    57  	Algorithm         *string             `json:"algorithm,omitempty"`
    58  	Config            *MultivaluedHashMap `json:"config,omitempty"`
    59  	Counter           *int32              `json:"counter,omitempty"`
    60  	CreatedDate       *int64              `json:"createdDate,omitempty"`
    61  	Device            *string             `json:"device,omitempty"`
    62  	Digits            *int32              `json:"digits,omitempty"`
    63  	HashIterations    *int32              `json:"hashIterations,omitempty"`
    64  	HashedSaltedValue *string             `json:"hashedSaltedValue,omitempty"`
    65  	Period            *int32              `json:"period,omitempty"`
    66  	Salt              *string             `json:"salt,omitempty"`
    67  	Temporary         *bool               `json:"temporary,omitempty"`
    68  	Type              *string             `json:"type,omitempty"`
    69  	Value             *string             `json:"value,omitempty"`
    70  }
    71  
    72  // MultivaluedHashMap ...
    73  type MultivaluedHashMap struct {
    74  	Empty      *bool  `json:"empty,omitempty"`
    75  	LoadFactor *int32 `json:"loadFactor,omitempty"`
    76  	Threshold  *int32 `json:"threshold,omitempty"`
    77  }
    78  
    79  // GetUserByID retrieves a user by ID
    80  func (c *AdminUserService) GetUserByID(
    81  	ctx context.Context,
    82  	ID string,
    83  ) (*User, *Response, error) {
    84  	path := fmt.Sprintf("%s/%s/users/%s", defaultAdminBase, c.client.realm, ID)
    85  
    86  	req, err := c.client.newRequest("GET", path, nil, headers{}, true)
    87  	if err != nil {
    88  		return nil, nil, err
    89  	}
    90  
    91  	user := new(User)
    92  	resp, err := c.client.do(ctx, req, user)
    93  	if err != nil {
    94  		return nil, resp, err
    95  	}
    96  
    97  	return user, resp, nil
    98  }