yats.git

ref: e3514c03dbe4aa75d064ac589d0b71c991538ca3

client/pkiclient.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
/**
 * Yats - yats
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Paolo Lulli <kevwe.com>
 * @copyright Paolo Lulli 2024
 */
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
	"time"
)

func (c *YatsClient) PkiPostCsr(endpoint string, certRequest CertificateRequest) []byte {
	client := http.Client{
		Timeout: 30 * time.Second,
	}

	body, err := json.Marshal(certRequest)

	//fmt.Printf("body: -> %s\n", body)
	req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(body))
	req.Header.Set("Content-Type", "application/json; charset=UTF-8")
	if err != nil {
		fmt.Println("Unable to make POST request", err)
		os.Exit(1)
	}
	req.Header.Add("Accept", "*/*")
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer resp.Body.Close()
	data, _ := io.ReadAll(resp.Body)

	return data
}

type CertificateRequest struct {
	ClientCn  string `json:"clientCn"`
	Csr       string `json:"csr"`
	SecretPin string `json:"pin"`
}

type CertificateResponse struct {
	Base64Certificate string `json:"base64cert"`
}