-
Notifications
You must be signed in to change notification settings - Fork 6
/
auth.go
80 lines (68 loc) · 2.19 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
// Copyright (c) 2019, NewReleases Go client AUTHORS.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package newreleases
import (
"context"
"net"
"net/http"
"strings"
)
// AuthService provides information about API authentication.
type AuthService service
// AuthKey represents API authentication secret key, with its descriptive name
// and authorized networks.
type AuthKey struct {
Name string `json:"name"`
Secret string `json:"secret"`
AuthorizedNetworks []net.IPNet `json:"authorized_networks"`
}
// List returns all authentication keys.
func (s *AuthService) List(ctx context.Context) (keys []AuthKey, err error) {
var r authKeysResponse
err = s.client.request(ctx, http.MethodGet, "v1/auth/keys", nil, &r)
return r.AuthKeys(), err
}
// GetAuthKeys returns a list of all auth keys for an account by authenticating
// with account's email address and a password. This function can be used to get
// the authentication key without providing it explicitly to the client, but
// first asking for already known (to the user) credentials.
func GetAuthKeys(ctx context.Context, email, password string, o *ClientOptions) (keys []AuthKey, err error) {
return newBasicAuthClient(email, password, o).Auth.List(ctx)
}
type authKeysResponse struct {
Keys []authKey `json:"keys"`
}
func (r *authKeysResponse) AuthKeys() (ak []AuthKey) {
ak = make([]AuthKey, len(r.Keys))
for i, k := range r.Keys {
ak[i] = AuthKey{
Name: k.Name,
Secret: k.Secret,
AuthorizedNetworks: k.authorizedNetworks(),
}
}
return ak
}
type authKey struct {
Name string `json:"name"`
Secret string `json:"secret"`
AuthorizedNetworks []ipNet `json:"authorized_networks"`
}
func (k *authKey) authorizedNetworks() (an []net.IPNet) {
an = make([]net.IPNet, len(k.AuthorizedNetworks))
for i, n := range k.AuthorizedNetworks {
an[i] = net.IPNet(n)
}
return an
}
type ipNet net.IPNet
func (n *ipNet) UnmarshalJSON(data []byte) (err error) {
_, nn, err := net.ParseCIDR(strings.Trim(string(data), `"`))
if err != nil {
return err
}
*n = ipNet(*nn)
return nil
}