-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.go
73 lines (65 loc) · 2.11 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
package taigo
// AuthService is a handle to actions related to Auths
//
// https://taigaio.github.io/taiga-doc/dist/api.html#auths
type AuthService struct {
client *Client
defaultProjectID int
Endpoint string
}
// RefreshAuthToken => https://docs.taiga.io/api.html#auth-refresh
//
// Generates a new pair of bearer and refresh token
// If `selfUpdate` is true, `*Client` is refreshed with the returned token values
func (s *AuthService) RefreshAuthToken(selfUpdate bool) (RefreshResponse *RefreshToken, err error) {
url := s.client.MakeURL(s.Endpoint, "refresh")
data := RefreshToken{
AuthToken: s.client.Token,
Refresh: s.client.RefreshToken,
}
_, err = s.client.Request.Post(url, &data, &RefreshResponse)
if err != nil {
return nil, err
}
if selfUpdate {
s.client.Token = RefreshResponse.AuthToken
s.client.RefreshToken = RefreshResponse.Refresh
}
return
}
// PublicRegistry => https://taigaio.github.io/taiga-doc/dist/api.html#auth-public-registry
/*
type with value "public"
username (required)
password (required)
email (required)
full_name (required)
accepted_terms (required): boolean
*/
func (s *AuthService) PublicRegistry(credentials *Credentials) (*UserAuthenticationDetail, error) {
url := s.client.MakeURL(s.Endpoint, "register")
u := UserAuthenticationDetail{}
credentials.Type = "public"
credentials.AcceptedTerms = true // Hardcoded for simplicity; otherwise this func would be useless
_, err := s.client.Request.Post(url, &credentials, &u)
if err != nil {
return nil, err
}
return &u, nil
}
// PrivateRegistry => https://taigaio.github.io/taiga-doc/dist/api.html#auth-private-registry
// TODO: TO BE IMPLEMENTED
// func (s *AuthService) PrivateRegistry(credentials *Credentials) {}
// login authenticates to Taiga
func (s *AuthService) login(credentials *Credentials) (*UserAuthenticationDetail, error) {
url := s.client.MakeURL(s.Endpoint)
u := UserAuthenticationDetail{}
_, err := s.client.Request.Post(url, &credentials, &u)
if err != nil {
return nil, err
}
s.client.Token = u.AuthToken
s.client.RefreshToken = u.Refresh
s.client.setToken()
return &u, nil
}