diff --git a/authentication_management.go b/authentication_management.go index 6706cb1..45a3de1 100644 --- a/authentication_management.go +++ b/authentication_management.go @@ -49,8 +49,7 @@ func (c *Client) DeleteAuthenticatorConfig(accessToken string, realmName, config // CreateAuthenticationExecution add new authentication execution func (c *Client) CreateAuthenticationExecution(accessToken string, realmName string, authExec AuthenticationExecutionRepresentation) (string, error) { - var location string - return location, c.post(accessToken, nil, &location, url.Path(authenticationManagementPath+"/executions"), url.Param("realm", realmName), body.JSON(authExec)) + return c.post(accessToken, nil, url.Path(authenticationManagementPath+"/executions"), url.Param("realm", realmName), body.JSON(authExec)) } // DeleteAuthenticationExecution deletes the execution. @@ -60,22 +59,26 @@ func (c *Client) DeleteAuthenticationExecution(accessToken string, realmName, ex // UpdateAuthenticationExecution update execution with new configuration. func (c *Client) UpdateAuthenticationExecution(accessToken string, realmName, executionID string, authConfig AuthenticatorConfigRepresentation) error { - return c.post(accessToken, nil, nil, url.Path(authenticationManagementPath+"/executions/:id/config"), url.Param("realm", realmName), url.Param("id", executionID), body.JSON(authConfig)) + _, err := c.post(accessToken, nil, url.Path(authenticationManagementPath+"/executions/:id/config"), url.Param("realm", realmName), url.Param("id", executionID), body.JSON(authConfig)) + return err } // LowerExecutionPriority lowers the execution’s priority. func (c *Client) LowerExecutionPriority(accessToken string, realmName, executionID string) error { - return c.post(accessToken, nil, nil, url.Path(authenticationManagementPath+"/executions/:id/lower-priority"), url.Param("realm", realmName), url.Param("id", executionID)) + _, err := c.post(accessToken, nil, url.Path(authenticationManagementPath+"/executions/:id/lower-priority"), url.Param("realm", realmName), url.Param("id", executionID)) + return err } // RaiseExecutionPriority raise the execution’s priority. func (c *Client) RaiseExecutionPriority(accessToken string, realmName, executionID string) error { - return c.post(accessToken, nil, nil, url.Path(authenticationManagementPath+"/executions/:id/raise-priority"), url.Param("realm", realmName), url.Param("id", executionID)) + _, err := c.post(accessToken, nil, url.Path(authenticationManagementPath+"/executions/:id/raise-priority"), url.Param("realm", realmName), url.Param("id", executionID)) + return err } // CreateAuthenticationFlow creates a new authentication flow. func (c *Client) CreateAuthenticationFlow(accessToken string, realmName string, authFlow AuthenticationFlowRepresentation) error { - return c.post(accessToken, nil, nil, url.Path(authenticationManagementPath+"/flows"), url.Param("realm", realmName), body.JSON(authFlow)) + _, err := c.post(accessToken, nil, url.Path(authenticationManagementPath+"/flows"), url.Param("realm", realmName), body.JSON(authFlow)) + return err } // GetAuthenticationFlows returns a list of authentication flows. @@ -90,7 +93,8 @@ func (c *Client) GetAuthenticationFlows(accessToken string, realmName string) ([ // 'newName' is the new name of the authentication flow. func (c *Client) CopyExistingAuthenticationFlow(accessToken string, realmName, flowAlias, newName string) error { var m = map[string]string{"newName": newName} - return c.post(accessToken, nil, nil, url.Path(authenticationManagementPath+"/flows/:flowAlias/copy"), url.Param("realm", realmName), url.Param("flowAlias", flowAlias), body.JSON(m)) + _, err := c.post(accessToken, nil, url.Path(authenticationManagementPath+"/flows/:flowAlias/copy"), url.Param("realm", realmName), url.Param("flowAlias", flowAlias), body.JSON(m)) + return err } // GetAuthenticationExecutionForFlow returns the authentication executions for a flow. @@ -109,16 +113,14 @@ func (c *Client) UpdateAuthenticationExecutionForFlow(accessToken string, realmN // 'flowAlias' is the alias of the parent flow. func (c *Client) CreateAuthenticationExecutionForFlow(accessToken string, realmName, flowAlias, provider string) (string, error) { var m = map[string]string{"provider": provider} - var location string - return location, c.post(accessToken, nil, &location, url.Path(authenticationManagementPath+"/flows/:flowAlias/executions/execution"), url.Param("realm", realmName), url.Param("flowAlias", flowAlias), body.JSON(m)) + return c.post(accessToken, nil, url.Path(authenticationManagementPath+"/flows/:flowAlias/executions/execution"), url.Param("realm", realmName), url.Param("flowAlias", flowAlias), body.JSON(m)) } // CreateFlowWithExecutionForExistingFlow add a new flow with a new execution to an existing flow. // 'flowAlias' is the alias of the parent authentication flow. func (c *Client) CreateFlowWithExecutionForExistingFlow(accessToken string, realmName, flowAlias, alias, flowType, provider, description string) (string, error) { var m = map[string]string{"alias": alias, "type": flowType, "provider": provider, "description": description} - var location string - return location, c.post(accessToken, nil, &location, url.Path(authenticationManagementPath+"/flows/:flowAlias/executions/flow"), url.Param("realm", realmName), url.Param("flowAlias", flowAlias), body.JSON(m)) + return c.post(accessToken, nil, url.Path(authenticationManagementPath+"/flows/:flowAlias/executions/flow"), url.Param("realm", realmName), url.Param("flowAlias", flowAlias), body.JSON(m)) } // GetAuthenticationFlow gets the authentication flow for id. @@ -157,7 +159,8 @@ func (c *Client) GetConfigDescriptionForClients(accessToken string, realmName st // RegisterRequiredAction register a new required action. func (c *Client) RegisterRequiredAction(accessToken string, realmName, providerID, name string) error { var m = map[string]string{"providerId": providerID, "name": name} - return c.post(accessToken, nil, nil, url.Path(authenticationManagementPath+"/register-required-action"), url.Param("realm", realmName), body.JSON(m)) + _, err := c.post(accessToken, nil, url.Path(authenticationManagementPath+"/register-required-action"), url.Param("realm", realmName), body.JSON(m)) + return err } // GetRequiredActions returns a list of required actions. diff --git a/client_attribute_certificate.go b/client_attribute_certificate.go index a3ba1fb..0cdfe4e 100644 --- a/client_attribute_certificate.go +++ b/client_attribute_certificate.go @@ -21,34 +21,34 @@ func (c *Client) GetKeyInfo(accessToken string, realmName, idClient, attr string // GetKeyStore returns a keystore file for the client, containing private key and public certificate. idClient is the id of client (not client-id). func (c *Client) GetKeyStore(accessToken string, realmName, idClient, attr string, keyStoreConfig KeyStoreConfig) ([]byte, error) { var resp = []byte{} - var err = c.post(accessToken, &resp, nil, url.Path(clientAttrCertPath+"/download"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.JSON(keyStoreConfig)) + _, err := c.post(accessToken, &resp, url.Path(clientAttrCertPath+"/download"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.JSON(keyStoreConfig)) return resp, err } // GenerateCertificate generates a new certificate with new key pair. idClient is the id of client (not client-id). func (c *Client) GenerateCertificate(accessToken string, realmName, idClient, attr string) (CertificateRepresentation, error) { var resp = CertificateRepresentation{} - var err = c.post(accessToken, &resp, nil, url.Path(clientAttrCertPath+"/generate"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr)) + _, err := c.post(accessToken, &resp, url.Path(clientAttrCertPath+"/generate"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr)) return resp, err } // GenerateKeyPairAndCertificate generates a keypair and certificate and serves the private key in a specified keystore format. func (c *Client) GenerateKeyPairAndCertificate(accessToken string, realmName, idClient, attr string, keyStoreConfig KeyStoreConfig) ([]byte, error) { var resp = []byte{} - var err = c.post(accessToken, &resp, nil, url.Path(clientAttrCertPath+"/generate-and-download"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.JSON(keyStoreConfig)) + _, err := c.post(accessToken, &resp, url.Path(clientAttrCertPath+"/generate-and-download"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.JSON(keyStoreConfig)) return resp, err } // UploadCertificatePrivateKey uploads a certificate and eventually a private key. func (c *Client) UploadCertificatePrivateKey(accessToken string, realmName, idClient, attr string, file []byte) (CertificateRepresentation, error) { var resp = CertificateRepresentation{} - var err = c.post(accessToken, &resp, nil, url.Path(clientAttrCertPath+"/upload"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.Reader(bytes.NewReader(file))) + _, err := c.post(accessToken, &resp, url.Path(clientAttrCertPath+"/upload"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.Reader(bytes.NewReader(file))) return resp, err } // UploadCertificate uploads only a certificate, not the private key. func (c *Client) UploadCertificate(accessToken string, realmName, idClient, attr string, file []byte) (CertificateRepresentation, error) { var resp = CertificateRepresentation{} - var err = c.post(accessToken, &resp, nil, url.Path(clientAttrCertPath+"/upload-certificate"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.Reader(bytes.NewReader(file))) + _, err := c.post(accessToken, &resp, url.Path(clientAttrCertPath+"/upload-certificate"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.Reader(bytes.NewReader(file))) return resp, err } diff --git a/client_initial_access.go b/client_initial_access.go index ed05991..1c9a1b9 100644 --- a/client_initial_access.go +++ b/client_initial_access.go @@ -12,7 +12,7 @@ const ( // CreateClientInitialAccess creates a new initial access token. func (c *Client) CreateClientInitialAccess(accessToken string, realmName string, access ClientInitialAccessCreatePresentation) (ClientInitialAccessPresentation, error) { var resp = ClientInitialAccessPresentation{} - var err = c.post(accessToken, &resp, nil, url.Path(clientInitialAccessPath), url.Param("realm", realmName), body.JSON(access)) + _, err := c.post(accessToken, &resp, nil, url.Path(clientInitialAccessPath), url.Param("realm", realmName), body.JSON(access)) return resp, err } diff --git a/client_role_mappings.go b/client_role_mappings.go index 3377e35..8501d0d 100644 --- a/client_role_mappings.go +++ b/client_role_mappings.go @@ -11,7 +11,8 @@ const ( // CreateClientsRoleMapping add client-level roles to the user role mapping. func (c *Client) CreateClientsRoleMapping(accessToken string, realmName, groupID, clientID string, roles []RoleRepresentation) error { - return c.post(accessToken, nil, nil, url.Path(clientRoleMappingPath), url.Param("realm", realmName), url.Param("id", groupID), url.Param("client", clientID), body.JSON(roles)) + _, err := c.post(accessToken, nil, url.Path(clientRoleMappingPath), url.Param("realm", realmName), url.Param("id", groupID), url.Param("client", clientID), body.JSON(roles)) + return err } // GetClientsRoleMapping gets client-level role mappings for the user, and the app. diff --git a/keycloak_client.go b/keycloak_client.go index fac3fb5..6db4359 100644 --- a/keycloak_client.go +++ b/keycloak_client.go @@ -23,8 +23,8 @@ type Config struct { // Client is the keycloak client. type Client struct { - url *url.URL - httpClient *gentleman.Client + url *url.URL + httpClient *gentleman.Client } // New returns a keycloak client. @@ -150,7 +150,7 @@ func (c *Client) get(accessToken string, data interface{}, plugins ...plugin.Plu } } -func (c *Client) post(accessToken string, data interface{}, location *string, plugins ...plugin.Plugin) error { +func (c *Client) post(accessToken string, data interface{}, plugins ...plugin.Plugin) (string, error) { var req = c.httpClient.Post() req = applyPlugins(req, accessToken, plugins...) var resp *gentleman.Response @@ -158,29 +158,28 @@ func (c *Client) post(accessToken string, data interface{}, location *string, pl var err error resp, err = req.Do() if err != nil { - return errors.Wrap(err, "could not get response") + return "", errors.Wrap(err, "could not get response") } switch { case resp.StatusCode == http.StatusUnauthorized: - return fmt.Errorf("unauthorized request: '%v': %v", resp.RawResponse.Status, string(resp.Bytes())) + return "", fmt.Errorf("unauthorized request: '%v': %v", resp.RawResponse.Status, string(resp.Bytes())) case resp.StatusCode >= 400: - return fmt.Errorf("invalid status code: '%v': %v", resp.RawResponse.Status, string(resp.Bytes())) + return "", fmt.Errorf("invalid status code: '%v': %v", resp.RawResponse.Status, string(resp.Bytes())) case resp.StatusCode >= 200: - var l = resp.Header.Get("Location") - location = &l + var location = resp.Header.Get("Location") switch resp.Header.Get("Content-Type") { case "application/json": - return resp.JSON(data) + return location, resp.JSON(data) case "application/octet-stream": data = resp.Bytes() - return nil + return location, nil default: - return nil + return location, nil } default: - return fmt.Errorf("unknown response status code: %v", resp.StatusCode) + return "", fmt.Errorf("unknown response status code: %v", resp.StatusCode) } } } diff --git a/realm.go b/realm.go index 225aad2..b850bbb 100644 --- a/realm.go +++ b/realm.go @@ -13,7 +13,7 @@ const ( // GetRealms get the top level represention of all the realms. Nested information like users are // not included. -func (c *Client) GetRealms(accessToken string) ([]RealmRepresentation, error){ +func (c *Client) GetRealms(accessToken string) ([]RealmRepresentation, error) { var resp = []RealmRepresentation{} var err = c.get(accessToken, &resp, url.Path(realmRootPath)) return resp, err @@ -21,8 +21,7 @@ func (c *Client) GetRealms(accessToken string) ([]RealmRepresentation, error){ // CreateRealm creates the realm from its RealmRepresentation. func (c *Client) CreateRealm(accessToken string, realm RealmRepresentation) (string, error) { - var location string - return location, c.post(accessToken, nil, &location, url.Path(realmRootPath), body.JSON(realm)) + return c.post(accessToken, nil, url.Path(realmRootPath), body.JSON(realm)) } // GetRealm get the top level represention of the realm. Nested information like users are diff --git a/users.go b/users.go index 2218a3b..df22b4e 100644 --- a/users.go +++ b/users.go @@ -30,8 +30,7 @@ func (c *Client) GetUsers(accessToken string, realmName string, paramKV ...strin // CreateUser creates the user from its UserRepresentation. The username must be unique. func (c *Client) CreateUser(accessToken string, realmName string, user UserRepresentation) (string, error) { - var location string - return location, c.post(accessToken, nil, &location, url.Path(userPath), url.Param("realm", realmName), body.JSON(user)) + return c.post(accessToken, nil, url.Path(userPath), url.Param("realm", realmName), body.JSON(user)) } // CountUsers returns the number of users in the realm.