-
Notifications
You must be signed in to change notification settings - Fork 14
/
requests.go
55 lines (42 loc) · 1.52 KB
/
requests.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
package k8s
import (
"bytes"
"fmt"
"net/http"
"net/url"
"github.com/cyberark/conjur-authn-k8s-client/pkg/log"
)
// LoginRequest sends a login request
func LoginRequest(authnURL string, conjurVersion string, csrBytes []byte, usernamePrefix string) (*http.Request, error) {
var authenticateURL string
if conjurVersion == "4" {
authenticateURL = fmt.Sprintf("%s/users/login", authnURL)
} else if conjurVersion == "5" {
authenticateURL = fmt.Sprintf("%s/inject_client_cert", authnURL)
}
log.Debug(log.CAKC045, authenticateURL)
req, err := http.NewRequest("POST", authenticateURL, bytes.NewBuffer(csrBytes))
if err != nil {
return nil, log.RecordedError(log.CAKC024, err)
}
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("Host-Id-Prefix", usernamePrefix)
return req, nil
}
// AuthenticateRequest sends an authenticate request
func AuthenticateRequest(authnURL string, conjurVersion string, account string, username string) (*http.Request, error) {
var authenticateURL string
var err error
var req *http.Request
if conjurVersion == "4" {
authenticateURL = fmt.Sprintf("%s/users/%s/authenticate", authnURL, url.QueryEscape(username))
} else if conjurVersion == "5" {
authenticateURL = fmt.Sprintf("%s/%s/%s/authenticate", authnURL, account, url.QueryEscape(username))
}
log.Debug(log.CAKC046, authenticateURL)
if req, err = http.NewRequest("POST", authenticateURL, nil); err != nil {
return nil, log.RecordedError(log.CAKC023, err)
}
req.Header.Set("Content-Type", "text/plain")
return req, nil
}