-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.go
46 lines (38 loc) · 1.05 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
package dockerhub
import (
"context"
"errors"
"net/http"
)
// AuthService handles communication with the auth related
// methods of the Dockerhub API.
type AuthService service
// LoginRequest represents the payload to be sent to login to the
// Dockerhub API.
type LoginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
// LoginResponse represents the payload to be responded to a successful
// Dockerhub API login request.
type LoginResponse struct {
Token string `json:"token"`
}
// Login authenticates with the Dockerhub API with the given given
// username and password.
func (s *AuthService) Login(ctx context.Context, username, password string) error {
p := &LoginRequest{username, password}
req, err := s.client.NewRequest(http.MethodPost, "/users/login/", p)
if err != nil {
return err
}
res := &LoginResponse{}
if _, err := s.client.Do(ctx, req, res); err != nil {
return err
}
if len(res.Token) == 0 {
return errors.New("Did not recieve token")
}
s.client.SetAuthToken(res.Token)
return nil
}