-
Notifications
You must be signed in to change notification settings - Fork 0
/
credential.go
108 lines (79 loc) · 4.04 KB
/
credential.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package gokeycloak
import "context"
// ---------------
// Credentials API
// ---------------
// GetCredentialRegistrators returns credentials registrators
func (g *GoKeycloak) GetCredentialRegistrators(ctx context.Context, token, realm string) ([]string, error) {
const errMessage = "could not get user credential registrators"
var result []string
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "credential-registrators"))
if err := checkForError(resp, err, errMessage); err != nil {
return nil, err
}
return result, nil
}
// GetConfiguredUserStorageCredentialTypes returns credential types, which are provided by the user storage where user is stored
func (g *GoKeycloak) GetConfiguredUserStorageCredentialTypes(ctx context.Context, token, realm, userID string) ([]string, error) {
const errMessage = "could not get user credential registrators"
var result []string
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "users", userID, "configured-user-storage-credential-types"))
if err := checkForError(resp, err, errMessage); err != nil {
return nil, err
}
return result, nil
}
// GetCredentials returns credentials available for a given user
func (g *GoKeycloak) GetCredentials(ctx context.Context, token, realm, userID string) ([]*CredentialRepresentation, error) {
const errMessage = "could not get user credentials"
var result []*CredentialRepresentation
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "users", userID, "credentials"))
if err := checkForError(resp, err, errMessage); err != nil {
return nil, err
}
return result, nil
}
// DeleteCredentials deletes the given credential for a given user
func (g *GoKeycloak) DeleteCredentials(ctx context.Context, token, realm, userID, credentialID string) error {
const errMessage = "could not delete user credentials"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Delete(g.getAdminRealmURL(realm, "users", userID, "credentials", credentialID))
return checkForError(resp, err, errMessage)
}
// UpdateCredentialUserLabel updates label for the given credential for the given user
func (g *GoKeycloak) UpdateCredentialUserLabel(ctx context.Context, token, realm, userID, credentialID, userLabel string) error {
const errMessage = "could not update credential label for a user"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetHeader("Content-Type", "text/plain").
SetBody(userLabel).
Put(g.getAdminRealmURL(realm, "users", userID, "credentials", credentialID, "userLabel"))
return checkForError(resp, err, errMessage)
}
// DisableAllCredentialsByType disables all credentials for a user of a specific type
func (g *GoKeycloak) DisableAllCredentialsByType(ctx context.Context, token, realm, userID string, types []string) error {
const errMessage = "could not update disable credentials"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(types).
Put(g.getAdminRealmURL(realm, "users", userID, "disable-credential-types"))
return checkForError(resp, err, errMessage)
}
// MoveCredentialBehind move a credential to a position behind another credential
func (g *GoKeycloak) MoveCredentialBehind(ctx context.Context, token, realm, userID, credentialID, newPreviousCredentialID string) error {
const errMessage = "could not move credential"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Post(g.getAdminRealmURL(realm, "users", userID, "credentials", credentialID, "moveAfter", newPreviousCredentialID))
return checkForError(resp, err, errMessage)
}
// MoveCredentialToFirst move a credential to a first position in the credentials list of the user
func (g *GoKeycloak) MoveCredentialToFirst(ctx context.Context, token, realm, userID, credentialID string) error {
const errMessage = "could not move credential"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Post(g.getAdminRealmURL(realm, "users", userID, "credentials", credentialID, "moveToFirst"))
return checkForError(resp, err, errMessage)
}