@@ -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 * EntryUserCredential
2121 Entries * Entries
2222 Vaults * Vaults
2323}
@@ -27,89 +27,37 @@ 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 {
@@ -123,82 +71,26 @@ func NewClient(username string, password string, baseUri string) (Client, error)
12371 return client , nil
12472}
12573
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- }
74+ func (c * Client ) login () error {
75+ loginBody := fmt .Sprintf ("AppKey=%s&AppSecret=%s" , c .credential .appKey , c .credential .appSecret )
18176
18277 reqUrl , err := url .JoinPath (c .baseUri , loginEndpoint )
18378 if err != nil {
18479 return fmt .Errorf ("failed to build login url. error: %w" , err )
18580 }
18681
187- resp , err := c .rawRequest (reqUrl , http .MethodPost , bytes .NewBuffer ( loginJson ))
82+ resp , err := c .rawRequest (reqUrl , http .MethodPost , loginContentType , bytes .NewBufferString ( loginBody ))
18883 if err != nil {
189- return fmt .Errorf ("error while submitting refreshtoken request. error: %w" , err )
84+ return fmt .Errorf ("error while submitting login request. error: %w" , err )
19085 }
19186
19287 var loginResponse loginResponse
19388 err = json .Unmarshal (resp .Response , & loginResponse )
19489 if err != nil {
19590 return fmt .Errorf ("failed to unmarshal response body. error: %w" , err )
19691 }
197- if loginResponse .Data .Result != ServerLoginSuccess {
198- return fmt .Errorf ("failed to refresh token (%s) : %s" , loginResponse .Data .Result , loginResponse .Data .Message )
199- }
20092
201- c .credential .token = loginResponse .Data . TokenId
93+ c .credential .token = loginResponse .TokenId
20294
20395 return nil
20496}
@@ -209,7 +101,7 @@ func (c *Client) isLogged() (bool, error) {
209101 return false , fmt .Errorf ("failed to build isLogged url. error: %w" , err )
210102 }
211103
212- resp , err := c .rawRequest (reqUrl , http .MethodGet , nil )
104+ resp , err := c .rawRequest (reqUrl , http .MethodGet , defaultContentType , nil )
213105 if err != nil && ! strings .Contains (err .Error (), "json: cannot unmarshal bool into Go value" ) {
214106 return false , fmt .Errorf ("error while submitting isLogged request. error: %w" , err )
215107 }
0 commit comments