Skip to content

Commit

Permalink
Support issuing Lets Encrypt SSL certs
Browse files Browse the repository at this point in the history
  • Loading branch information
wolveix committed Mar 2, 2024
1 parent 27e46b0 commit 63b7793
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions ssl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package directadmin

import (
"errors"
"fmt"
"net/http"
"net/url"

"github.com/spf13/cast"
)

// IssueSSL (user) requests a lets encrypt certificate for the given hostnames
func (c *UserContext) IssueSSL(domain string, hostnamesToCertify ...string) error {
var response apiGenericResponse

if len(hostnamesToCertify) == 0 {
return errors.New("at least one hostname is required for the certificate")
}

body := url.Values{
"type": {"create"},
"request": {"letsencrypt"},
"name": {hostnamesToCertify[0]},
"domain": {domain},
"keysize": {"secp384r1"},
"encryption": {"sha256"},
"wildcard": {"no"},
"background": {"auto"},
"action": {"save"},
"acme_provider": {"letsencrypt"},
}

for index, certDomain := range hostnamesToCertify {
body.Set("le_select"+cast.ToString(index), certDomain)
}

if _, err := c.api.makeRequest(http.MethodPost, "API_SSL", c.credentials, body, &response); err != nil {
return err
}

if response.Success != "Certificate and Key Saved." {
return fmt.Errorf("failed to issue SSL certificate: %v", response.Result)
}

return nil
}

0 comments on commit 63b7793

Please sign in to comment.