-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
95 lines (78 loc) · 2.25 KB
/
auth.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
package vmmanagerapi
import (
"encoding/json"
"fmt"
"time"
"github.com/gadost/go-vmmanager-api/types"
)
type AuthByEmailAndPassword struct {
Email string `json:"email"`
Password string `json:"password"`
}
type AuthByKey struct {
Key string `json:"key"`
}
type AuthData struct {
Confirmed bool `json:"confirmed"`
ID string `json:"id"`
Token string `json:"token"`
Error Error `json:"error,omitempty"`
}
type Auth struct {
AuthByKey
AuthByEmailAndPassword
authUri string
}
// Auth get session and token
func (a *Api) Auth(auth *Auth) *Api {
var payload []byte
if auth.AuthByEmailAndPassword.Email != "" && auth.AuthByEmailAndPassword.Password != "" {
auth.authUri = AuthV4Uri
payload, _ = json.Marshal(auth.AuthByEmailAndPassword)
} else if auth.AuthByKey.Key != "" {
auth.authUri = AuthByKeyUri
payload, _ = json.Marshal(auth.AuthByKey)
} else {
return nil
}
bodyResp, err := a.NewRequest(payload, auth.authUri, requestTypePost, AuthService)
if err != nil {
return nil
}
json.Unmarshal(bodyResp, &a.AuthData)
return a
}
// ChangePasswordByToken POST request to "/public/token/{token_id}/change_password"
func (a *Api) ChangePasswordByToken(password string) error {
payload, _ := json.Marshal(&types.Password{Password: password})
_, err := a.NewRequest(
payload,
fmt.Sprintf("/public/token/%s/change_password", a.AuthData.Token),
requestTypePost, DefaultService)
if err != nil {
return err
}
return nil
}
// WithExtendedTokenLifetime extend token lifetime. default 1h
func (a *Api) WithExtendedTokenLifetime(expiresAt time.Time, desc string) *Api {
payload, _ := json.Marshal(&types.TokenLifetime{
ExpiresAt: expiresAt.Format("2006-01-02 15:04:05"),
Description: desc,
})
bodyResp, _ := a.NewRequest(payload, "/token", requestTypePost, AuthService)
json.Unmarshal(bodyResp, &a.AuthData)
return a
}
// InviteUserByToken POST request to /public/token/{token_id}/invite_user
func (a *Api) InviteUserByToken(password string, lang string) error {
payload, _ := json.Marshal(&types.InviteUser{Password: password, Lang: lang})
_, err := a.NewRequest(
payload,
fmt.Sprintf("/public/token/%s/invite_user", a.AuthData.Token),
requestTypePost, DefaultService)
if err != nil {
return err
}
return nil
}