diff --git a/client_role_mappings.go b/client_role_mappings.go index 8501d0d..ed01edd 100644 --- a/client_role_mappings.go +++ b/client_role_mappings.go @@ -6,23 +6,30 @@ import ( ) const ( - clientRoleMappingPath = "/auth/admin/realms/:realm/groups/:id/role-mappings/clients/:client" + clientRoleMappingPath = "/auth/admin/realms/:realm/users/:id/role-mappings/clients/:client" + realmRoleMappingPath = "/auth/admin/realms/:realm/users/:id/role-mappings/realm" ) -// CreateClientsRoleMapping add client-level roles to the user role mapping. -func (c *Client) CreateClientsRoleMapping(accessToken string, realmName, groupID, clientID string, roles []RoleRepresentation) error { - _, err := c.post(accessToken, nil, url.Path(clientRoleMappingPath), url.Param("realm", realmName), url.Param("id", groupID), url.Param("client", clientID), body.JSON(roles)) +// AddClientRoleMapping add client-level roles to the user role mapping. +func (c *Client) AddClientRolesToUserRoleMapping(accessToken string, realmName, userID, clientID string, roles []RoleRepresentation) error { + _, err := c.post(accessToken, nil, url.Path(clientRoleMappingPath), url.Param("realm", realmName), url.Param("id", userID), url.Param("client", clientID), body.JSON(roles)) return err } -// GetClientsRoleMapping gets client-level role mappings for the user, and the app. -func (c *Client) GetClientsRoleMapping(accessToken string, realmName, groupID, clientID string) ([]RoleRepresentation, error) { +// GetClientRoleMappings gets client-level role mappings for the user, and the app. +func (c *Client) GetClientRoleMappings(accessToken string, realmName, userID, clientID string) ([]RoleRepresentation, error) { var resp = []RoleRepresentation{} - var err = c.get(accessToken, &resp, url.Path(clientRoleMappingPath), url.Param("realm", realmName), url.Param("id", groupID), url.Param("client", clientID)) + var err = c.get(accessToken, &resp, url.Path(clientRoleMappingPath), url.Param("realm", realmName), url.Param("id", userID), url.Param("client", clientID)) return resp, err } -// DeleteClientsRoleMapping deletes client-level roles from user role mapping. -func (c *Client) DeleteClientsRoleMapping(accessToken string, realmName, groupID, clientID string) error { - return c.delete(accessToken, url.Path(clientRoleMappingPath), url.Param("realm", realmName), url.Param("id", groupID), url.Param("client", clientID)) +// DeleteClientRolesFromUserRoleMapping deletes client-level roles from user role mapping. +func (c *Client) DeleteClientRolesFromUserRoleMapping(accessToken string, realmName, userID, clientID string) error { + return c.delete(accessToken, url.Path(clientRoleMappingPath), url.Param("realm", realmName), url.Param("id", userID), url.Param("client", clientID)) +} + +func (c *Client) GetRealmLevelRoleMappings(accessToken string, realmName, userID string) ([]RoleRepresentation, error) { + var resp = []RoleRepresentation{} + var err = c.get(accessToken, url.Path(realmRoleMappingPath), url.Param("realm", realmName), url.Param("id", userID)) + return resp, err } diff --git a/integration/integration.go b/integration/integration.go index e09a4ba..8b77fa0 100644 --- a/integration/integration.go +++ b/integration/integration.go @@ -15,6 +15,20 @@ const ( user = "version" ) + +// This should be oncverted into +// GetClient(accessToken string, realmName, idClient string) (kc.ClientRepresentation, error) +// GetClientRoleMappings(accessToken string, realmName, userID, clientID string) ([]kc.RoleRepresentation, error) +// AddClientRolesToUserRoleMapping(accessToken string, realmName, userID, clientID string, roles []kc.RoleRepresentation) error +// GetRealmLevelRoleMappings(accessToken string, realmName, userID string) ([]kc.RoleRepresentation, error) +// ResetPassword(accessToken string, realmName string, userID string) error +// SendVerifyEmail(accessToken string, realmName string, userID string) error + +// GetRoles(accessToken string, realmName string) ([]kc.RoleRepresentation, error) +// GetRole(accessToken string, realmName string, roleID string) (kc.RoleRepresentation, error) +// GetClientRoles(accessToken string, realmName, idClient string) ([]kc.RoleRepresentation, error) +// CreateClientRole(accessToken string, realmName, clientID string, role kc.RoleRepresentation) (string, error) + func main() { var conf = getKeycloakConfig() var client, err = keycloak.New(*conf) @@ -122,6 +136,8 @@ func main() { if err != nil { log.Fatalf("could not create test users: %v", err) } + + } // Check that all users where created. { @@ -147,6 +163,17 @@ func main() { if len(users) != 50 { log.Fatalf("there should be 50 users") } + + user, err := client.GetUser(accessToken, tstRealm, *(users[0].Id)) + if err != nil { + log.Fatalf("could not get user") + } + + if !(*(user.Username) != ""){ + log.Fatalf("Username should not be empty") + } + + fmt.Println("Test user retrieved.") } { // email. @@ -209,6 +236,7 @@ func main() { log.Fatalf("there should be 7 users matched by search") } } + fmt.Println("Test users retrieved.") } @@ -317,26 +345,8 @@ func main() { } } -/* -// GetUser get the represention of the user. -func (c *Client) GetUser(realmName, userID string) (UserRepresentation, error) { - var resp = UserRepresentation{} - var err = c.get(&resp, url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID)) - return resp, err -} - -// UpdateUser update the user. -func (c *Client) UpdateUser(realmName, userID string, user UserRepresentation) error { - return c.put(url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID), body.JSON(user)) -} - -// DeleteUser deletes the user. -func (c *Client) DeleteUser(realmName, userID string) error { - return c.delete(url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID)) -} -*/ func getKeycloakConfig() *keycloak.Config { var adr = pflag.String("url", "http://localhost:8080", "keycloak address") diff --git a/roles.go b/roles.go new file mode 100644 index 0000000..d00d717 --- /dev/null +++ b/roles.go @@ -0,0 +1,38 @@ +package keycloak + +import ( + "gopkg.in/h2non/gentleman.v2/plugins/body" + "gopkg.in/h2non/gentleman.v2/plugins/url" +) + +const ( + rolePath = "/auth/admin/realms/:realm/roles" + roleByIDPath = "/auth/admin/realms/:realm/roles-by-id/:id" + clientRolePath = "/auth/admin/realms/:realm/clients/:id/roles" +) + +// GetClientRoles gets all roles for the realm or client +func (c *Client) GetClientRoles(accessToken string, realmName, idClient string) ([]RoleRepresentation, error) { + var resp = []RoleRepresentation{} + var err = c.get(accessToken, &resp, url.Path(clientRolePath), url.Param("realm", realmName), url.Param("id", idClient)) + return resp, err +} + +// CreateClientRole creates a new role for the realm or client +func (c *Client) CreateClientRole(accessToken string, realmName, clientID string, role RoleRepresentation) (string, error) { + return c.post(accessToken, nil, url.Path(clientRolePath), url.Param("realm", realmName), url.Param("client", clientID), body.JSON(role)) +} + +// GetRoles gets all roles for the realm or client +func (c *Client) GetRoles(accessToken string, realmName string) ([]RoleRepresentation, error) { + var resp = []RoleRepresentation{} + var err = c.get(accessToken, &resp, url.Path(rolePath), url.Param("realm", realmName)) + return resp, err +} + +// GetRole gets a specific role’s representation +func (c *Client) GetRole(accessToken string, realmName string, roleID string) (RoleRepresentation, error) { + var resp = RoleRepresentation{} + var err = c.get(accessToken, &resp, url.Path(roleByIDPath), url.Param("realm", realmName), url.Param("id", roleID)) + return resp, err +} diff --git a/users.go b/users.go index df22b4e..b3c6272 100644 --- a/users.go +++ b/users.go @@ -11,6 +11,8 @@ const ( userPath = "/auth/admin/realms/:realm/users" userCountPath = userPath + "/count" userIDPath = userPath + "/:id" + resetPasswordPath = userIDPath + "/reset-password" + sendVerifyEmailPath = userIDPath + "/send-verify-email" ) // GetUsers returns a list of users, filtered according to the query parameters. @@ -56,3 +58,19 @@ func (c *Client) UpdateUser(accessToken string, realmName, userID string, user U func (c *Client) DeleteUser(accessToken string, realmName, userID string) error { return c.delete(accessToken, url.Path(userIDPath), url.Param("realm", realmName), url.Param("id", userID)) } + +// ResetPassword resets password of the user. +func (c *Client) ResetPassword(accessToken string, realmName, userID string, cred CredentialRepresentation) error { + return c.put(accessToken, url.Path(resetPasswordPath), url.Param("realm", realmName), url.Param("id", userID), body.JSON(cred)) +} + +// SendVerifyEmail sends an email-verification email to the user An email contains a link the user can click to verify their email address. +func (c *Client) SendVerifyEmail(accessToken string, realmName string, userID string, paramKV ...string) error { + if len(paramKV)%2 != 0 { + return fmt.Errorf("the number of key/val parameters should be even") + } + + var plugins = append(createQueryPlugins(paramKV...), url.Path(sendVerifyEmailPath), url.Param("realm", realmName), url.Param("id", userID)) + + return c.put(accessToken, plugins...) +}