From e334982485e670886b57141c3bda2b3fd633fe0c Mon Sep 17 00:00:00 2001 From: Johan Droz Date: Fri, 16 Mar 2018 22:55:10 +0100 Subject: [PATCH] Add client attribute certificate, this is temporary work, the keycloak_client's post method must be modified --- client_attribute_certificate.go | 53 +++++++++++++++++++++++++++++++++ users.go | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/client_attribute_certificate.go b/client_attribute_certificate.go index cf172a0..ff79e60 100644 --- a/client_attribute_certificate.go +++ b/client_attribute_certificate.go @@ -1 +1,54 @@ package keycloak + +import ( + "bytes" + + "gopkg.in/h2non/gentleman.v2/plugins/body" + "gopkg.in/h2non/gentleman.v2/plugins/url" +) + +const ( + clientAttrCertPath = "/auth/admin/realms/:realm/clients/:id/certificates/:attr" +) + +// GetKeyInfo returns the key info. idClient is the id of client (not client-id). +func (c *Client) GetKeyInfo(realmName, idClient, attr string) (CertificateRepresentation, error) { + var resp = CertificateRepresentation{} + var err = c.get(&resp, url.Path(clientAttrCertPath), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr)) + return resp, err +} + +// 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(realmName, idClient, attr string, keyStoreConfig KeyStoreConfig) ([]byte, error) { + var resp = []byte{} + var err = c.post(url.Path(clientAttrCertPath+"/download"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.JSON(keyStoreConfig)) + return respfrompost, err +} + +// GenerateCertificate generates a new certificate with new key pair. idClient is the id of client (not client-id). +func (c *Client) GenerateCertificate(realmName, idClient, attr string) (CertificateRepresentation, error) { + var resp = CertificateRepresentation{} + var err = c.post(url.Path(clientAttrCertPath+"/generate"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr)) + return respfrompost, err +} + +// GenerateKeyPairAndCertificate generates a keypair and certificate and serves the private key in a specified keystore format. +func (c *Client) GenerateKeyPairAndCertificate(realmName, idClient, attr string, keyStoreConfig KeyStoreConfig) ([]byte, error) { + var resp = []byte{} + var err = c.post(url.Path(clientAttrCertPath+"/generate-and-download"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.JSON(keyStoreConfig)) + return respfrompost, err +} + +// UploadCertificatePrivateKey uploads a certificate and eventually a private key. +func (c *Client) UploadCertificatePrivateKey(realmName, idClient, attr string, file []byte) (CertificateRepresentation, error) { + var resp = CertificateRepresentation{} + var err = c.post(url.Path(clientAttrCertPath+"/upload"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.Reader(bytes.NewReader(file))) + return respfrompost, err +} + +// UploadCertificate uploads only a certificate, not the private key. +func (c *Client) UploadCertificate(realmName, idClient, attr string, file []byte) (CertificateRepresentation, error) { + var resp = CertificateRepresentation{} + var err = c.post(url.Path(clientAttrCertPath+"/upload-certificate"), url.Param("realm", realmName), url.Param("id", idClient), url.Param("attr", attr), body.Reader(bytes.NewReader(file))) + return respfrompost, err +} diff --git a/users.go b/users.go index 77cd18e..3738cf6 100644 --- a/users.go +++ b/users.go @@ -47,7 +47,7 @@ func (c *Client) GetUser(realmName, userID string) (UserRepresentation, error) { return resp, err } -// UpdateUser update the user. +// UpdateUser updates 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)) }