-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
221 lines (184 loc) · 5.44 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
ClientUser User
common service
Entries *Entries
Vaults *Vaults
}
type service struct {
client *Client
}
type credentials struct {
username string
password string
token string
}
type loginResponse struct {
Data struct {
Message string
Result ServerLoginResult
TokenId string
}
}
type loginReqBody struct {
Username string `json:"userName"`
LoginParameters loginParameters `json:"LoginParameters"`
}
type loginParameters struct {
Password string `json:"Password"`
Client string `json:"Client"`
Version string `json:"Version,omitempty"`
LocalMachineName string `json:"LocalMachineName,omitempty"`
LocalUserName string `json:"LocalUserName,omitempty"`
}
// User represents a DVLS user.
type User struct {
ID string
Username string
UserType UserAuthenticationType
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (u *User) UnmarshalJSON(d []byte) error {
raw := struct {
Data struct {
TokenId string
UserEntity struct {
Id string
Display string
UserSecurity struct {
AuthenticationType UserAuthenticationType
}
}
}
Result ServerLoginResult
Message string
}{}
err := json.Unmarshal(d, &raw)
if err != nil {
return err
}
u.ID = raw.Data.UserEntity.Id
u.Username = raw.Data.UserEntity.Display
u.UserType = raw.Data.UserEntity.UserSecurity.AuthenticationType
return nil
}
const (
loginEndpoint string = "/api/login/partial"
isLoggedEndpoint string = "/api/is-logged"
)
// 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(username string, password string, baseUri string) (Client, error) {
credential := credentials{username: username, password: password}
client := Client{
client: &http.Client{},
baseUri: baseUri,
credential: credential,
}
user, err := client.login()
if err != nil {
return Client{}, fmt.Errorf("login failed \"%w\"", err)
}
client.ClientUser = user
client.common.client = &client
client.Entries = &Entries{
UserCredential: (*EntryUserCredentialService)(&client.common),
Certificate: (*EntryCertificateService)(&client.common),
Website: (*EntryWebsiteService)(&client.common),
}
client.Vaults = (*Vaults)(&client.common)
return client, nil
}
func (c *Client) login() (User, error) {
loginBody := loginReqBody{
Username: c.credential.username,
LoginParameters: loginParameters{
Password: c.credential.password,
Client: "Cli",
},
}
loginJson, err := json.Marshal(loginBody)
if err != nil {
return User{}, fmt.Errorf("failed to marshal login body. error: %w", err)
}
reqUrl, err := url.JoinPath(c.baseUri, loginEndpoint)
if err != nil {
return User{}, fmt.Errorf("failed to build login url. error: %w", err)
}
resp, err := c.rawRequest(reqUrl, http.MethodPost, bytes.NewBuffer(loginJson))
if err != nil {
return User{}, fmt.Errorf("error while submitting refreshtoken request. error: %w", err)
}
var loginResponse loginResponse
err = json.Unmarshal(resp.Response, &loginResponse)
if err != nil {
return User{}, fmt.Errorf("failed to unmarshal response body. error: %w", err)
}
if loginResponse.Data.Result != ServerLoginSuccess {
return User{}, fmt.Errorf("failed to refresh token (%s) : %s", loginResponse.Data.Result, loginResponse.Data.Message)
}
var user User
err = json.Unmarshal(resp.Response, &user)
if err != nil {
return User{}, fmt.Errorf("failed to unmarshal user body. error: %w", err)
}
c.credential.token = loginResponse.Data.TokenId
return user, nil
}
func (c *Client) refreshToken() error {
loginBody := loginReqBody{
Username: c.credential.username,
LoginParameters: loginParameters{
Password: c.credential.password,
Client: "Cli",
},
}
loginJson, err := json.Marshal(loginBody)
if err != nil {
return fmt.Errorf("failed to marshal login body. error: %w", err)
}
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, bytes.NewBuffer(loginJson))
if err != nil {
return fmt.Errorf("error while submitting refreshtoken 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)
}
if loginResponse.Data.Result != ServerLoginSuccess {
return fmt.Errorf("failed to refresh token (%s) : %s", loginResponse.Data.Result, loginResponse.Data.Message)
}
c.credential.token = loginResponse.Data.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, 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
}