-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauthentication.go
113 lines (89 loc) · 2.74 KB
/
authentication.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package dvls
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
// Client represents the DVLS client used to communicate with the API.
type Client struct {
client *http.Client
baseUri string
credential credentials
common service
Entries *Entries
Vaults *Vaults
}
type service struct {
client *Client
}
type credentials struct {
appKey string
appSecret string
token string
}
type loginResponse struct {
TokenId string
}
const (
loginEndpoint string = "/api/v1/#"
isLoggedEndpoint string = "/api/is-logged"
)
const loginContentType = "application/x-www-form-urlencoded"
// NewClient returns a new Client configured with the specified credentials and
// base URI. baseUri should be the full URI to your DVLS instance (ex.: https://dvls.your-dvls-instance.com)
func NewClient(appKey string, appSecret string, baseUri string) (Client, error) {
credential := credentials{appKey: appKey, appSecret: appSecret}
client := Client{
client: &http.Client{},
baseUri: baseUri,
credential: credential,
}
err := client.login()
if err != nil {
return Client{}, fmt.Errorf("login failed \"%w\"", err)
}
client.common.client = &client
client.Entries = &Entries{
UserCredential: (*EntryUserCredentialService)(&client.common),
Certificate: (*EntryCertificateService)(&client.common),
Website: (*EntryWebsiteService)(&client.common),
Host: (*EntryHostService)(&client.common),
}
client.Vaults = (*Vaults)(&client.common)
return client, nil
}
func (c *Client) login() error {
loginBody := fmt.Sprintf("AppKey=%s&AppSecret=%s", c.credential.appKey, c.credential.appSecret)
reqUrl, err := url.JoinPath(c.baseUri, loginEndpoint)
if err != nil {
return fmt.Errorf("failed to build login url. error: %w", err)
}
resp, err := c.rawRequest(reqUrl, http.MethodPost, loginContentType, bytes.NewBufferString(loginBody))
if err != nil {
return fmt.Errorf("error while submitting login request. error: %w", err)
}
var loginResponse loginResponse
err = json.Unmarshal(resp.Response, &loginResponse)
if err != nil {
return fmt.Errorf("failed to unmarshal response body. error: %w", err)
}
c.credential.token = loginResponse.TokenId
return nil
}
func (c *Client) isLogged() (bool, error) {
reqUrl, err := url.JoinPath(c.baseUri, isLoggedEndpoint)
if err != nil {
return false, fmt.Errorf("failed to build isLogged url. error: %w", err)
}
resp, err := c.rawRequest(reqUrl, http.MethodGet, defaultContentType, nil)
if err != nil && !strings.Contains(err.Error(), "json: cannot unmarshal bool into Go value") {
return false, fmt.Errorf("error while submitting isLogged request. error: %w", err)
}
if string(resp.Response) == "false" {
return false, nil
}
return true, nil
}