@@ -14,10 +14,10 @@ type Client struct {
1414 client * http.Client
1515 baseUri string
1616 credential credentials
17- ClientUser User
1817
1918 common service
2019
20+ Entry * Entry
2121 Entries * Entries
2222 Vaults * Vaults
2323}
@@ -27,178 +27,69 @@ type service struct {
2727}
2828
2929type credentials struct {
30- username string
31- password string
32- token string
30+ appKey string
31+ appSecret string
32+ token string
3333}
3434
3535type loginResponse struct {
36- Data struct {
37- Message string
38- Result ServerLoginResult
39- TokenId string
40- }
41- }
42-
43- type loginReqBody struct {
44- Username string `json:"userName"`
45- LoginParameters loginParameters `json:"LoginParameters"`
46- }
47-
48- type loginParameters struct {
49- Password string `json:"Password"`
50- Client string `json:"Client"`
51- Version string `json:"Version,omitempty"`
52- LocalMachineName string `json:"LocalMachineName,omitempty"`
53- LocalUserName string `json:"LocalUserName,omitempty"`
54- }
55-
56- // User represents a DVLS user.
57- type User struct {
58- ID string
59- Username string
60- UserType UserAuthenticationType
61- }
62-
63- // UnmarshalJSON implements the json.Unmarshaler interface.
64- func (u * User ) UnmarshalJSON (d []byte ) error {
65- raw := struct {
66- Data struct {
67- TokenId string
68- UserEntity struct {
69- Id string
70- Display string
71- UserSecurity struct {
72- AuthenticationType UserAuthenticationType
73- }
74- }
75- }
76- Result ServerLoginResult
77- Message string
78- }{}
79- err := json .Unmarshal (d , & raw )
80- if err != nil {
81- return err
82- }
83-
84- u .ID = raw .Data .UserEntity .Id
85- u .Username = raw .Data .UserEntity .Display
86- u .UserType = raw .Data .UserEntity .UserSecurity .AuthenticationType
87-
88- return nil
36+ TokenId string
8937}
9038
9139const (
92- loginEndpoint string = "/api/login/partial "
40+ loginEndpoint string = "/api/v1/login "
9341 isLoggedEndpoint string = "/api/is-logged"
9442)
9543
44+ const loginContentType = "application/x-www-form-urlencoded"
45+
9646// NewClient returns a new Client configured with the specified credentials and
9747// base URI. baseUri should be the full URI to your DVLS instance (ex.: https://dvls.your-dvls-instance.com)
98- func NewClient (username string , password string , baseUri string ) (Client , error ) {
99- credential := credentials {username : username , password : password }
48+ func NewClient (appKey string , appSecret string , baseUri string ) (Client , error ) {
49+ credential := credentials {appKey : appKey , appSecret : appSecret }
10050 client := Client {
10151 client : & http.Client {},
10252 baseUri : baseUri ,
10353 credential : credential ,
10454 }
10555
106- user , err := client .login ()
56+ err := client .login ()
10757 if err != nil {
10858 return Client {}, fmt .Errorf ("login failed \" %w\" " , err )
10959 }
11060
111- client .ClientUser = user
112-
11361 client .common .client = & client
11462
11563 client .Entries = & Entries {
116- UserCredential : (* EntryUserCredentialService )(& client .common ),
117- Certificate : (* EntryCertificateService )(& client .common ),
118- Website : (* EntryWebsiteService )(& client .common ),
119- Host : (* EntryHostService )(& client .common ),
64+ Certificate : (* EntryCertificateService )(& client .common ),
65+ Website : (* EntryWebsiteService )(& client .common ),
66+ Host : (* EntryHostService )(& client .common ),
12067 }
12168 client .Vaults = (* Vaults )(& client .common )
12269
12370 return client , nil
12471}
12572
126- func (c * Client ) login () (User , error ) {
127- loginBody := loginReqBody {
128- Username : c .credential .username ,
129- LoginParameters : loginParameters {
130- Password : c .credential .password ,
131- Client : "Cli" ,
132- },
133- }
134- loginJson , err := json .Marshal (loginBody )
135- if err != nil {
136- return User {}, fmt .Errorf ("failed to marshal login body. error: %w" , err )
137- }
138-
139- reqUrl , err := url .JoinPath (c .baseUri , loginEndpoint )
140- if err != nil {
141- return User {}, fmt .Errorf ("failed to build login url. error: %w" , err )
142- }
143-
144- resp , err := c .rawRequest (reqUrl , http .MethodPost , bytes .NewBuffer (loginJson ))
145- if err != nil {
146- return User {}, fmt .Errorf ("error while submitting refreshtoken request. error: %w" , err )
147- }
148-
149- var loginResponse loginResponse
150- err = json .Unmarshal (resp .Response , & loginResponse )
151- if err != nil {
152- return User {}, fmt .Errorf ("failed to unmarshal response body. error: %w" , err )
153- }
154- if loginResponse .Data .Result != ServerLoginSuccess {
155- return User {}, fmt .Errorf ("failed to refresh token (%s) : %s" , loginResponse .Data .Result , loginResponse .Data .Message )
156- }
157-
158- var user User
159- err = json .Unmarshal (resp .Response , & user )
160- if err != nil {
161- return User {}, fmt .Errorf ("failed to unmarshal user body. error: %w" , err )
162- }
163-
164- c .credential .token = loginResponse .Data .TokenId
165-
166- return user , nil
167- }
168-
169- func (c * Client ) refreshToken () error {
170- loginBody := loginReqBody {
171- Username : c .credential .username ,
172- LoginParameters : loginParameters {
173- Password : c .credential .password ,
174- Client : "Cli" ,
175- },
176- }
177- loginJson , err := json .Marshal (loginBody )
178- if err != nil {
179- return fmt .Errorf ("failed to marshal login body. error: %w" , err )
180- }
73+ func (c * Client ) login () error {
74+ loginBody := fmt .Sprintf ("AppKey=%s&AppSecret=%s" , c .credential .appKey , c .credential .appSecret )
18175
18276 reqUrl , err := url .JoinPath (c .baseUri , loginEndpoint )
18377 if err != nil {
18478 return fmt .Errorf ("failed to build login url. error: %w" , err )
18579 }
18680
187- resp , err := c .rawRequest (reqUrl , http .MethodPost , bytes .NewBuffer ( loginJson ))
81+ resp , err := c .rawRequest (reqUrl , http .MethodPost , loginContentType , bytes .NewBufferString ( loginBody ))
18882 if err != nil {
189- return fmt .Errorf ("error while submitting refreshtoken request. error: %w" , err )
83+ return fmt .Errorf ("error while submitting login request. error: %w" , err )
19084 }
19185
19286 var loginResponse loginResponse
19387 err = json .Unmarshal (resp .Response , & loginResponse )
19488 if err != nil {
19589 return fmt .Errorf ("failed to unmarshal response body. error: %w" , err )
19690 }
197- if loginResponse .Data .Result != ServerLoginSuccess {
198- return fmt .Errorf ("failed to refresh token (%s) : %s" , loginResponse .Data .Result , loginResponse .Data .Message )
199- }
20091
201- c .credential .token = loginResponse .Data . TokenId
92+ c .credential .token = loginResponse .TokenId
20293
20394 return nil
20495}
@@ -209,7 +100,7 @@ func (c *Client) isLogged() (bool, error) {
209100 return false , fmt .Errorf ("failed to build isLogged url. error: %w" , err )
210101 }
211102
212- resp , err := c .rawRequest (reqUrl , http .MethodGet , nil )
103+ resp , err := c .rawRequest (reqUrl , http .MethodGet , defaultContentType , nil )
213104 if err != nil && ! strings .Contains (err .Error (), "json: cannot unmarshal bool into Go value" ) {
214105 return false , fmt .Errorf ("error while submitting isLogged request. error: %w" , err )
215106 }
0 commit comments