-
Notifications
You must be signed in to change notification settings - Fork 9
/
validate.go
55 lines (46 loc) · 1.25 KB
/
validate.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
package login
import (
"context"
"encoding/json"
"io/ioutil"
"net/url"
"github.com/hookdeck/hookdeck-cli/pkg/hookdeck"
)
// ValidateAPIKeyResponse returns the user and team associated with a key
type ValidateAPIKeyResponse struct {
UserID string `json:"user_id"`
UserName string `json:"user_name"`
UserEmail string `json:"user_email"`
OrganizationName string `json:"organization_name"`
OrganizationID string `json:"organization_id"`
TeamID string `json:"team_id"`
TeamName string `json:"team_name_no_org"`
TeamMode string `json:"team_mode"`
ClientID string `json:"client_id"`
}
func ValidateKey(baseURL string, key string, teamId string) (*ValidateAPIKeyResponse, error) {
parsedBaseURL, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
client := &hookdeck.Client{
BaseURL: parsedBaseURL,
APIKey: key,
TeamID: teamId,
}
res, err := client.Get(context.Background(), "/cli-auth/validate", "", nil)
if err != nil {
return nil, err
}
var response ValidateAPIKeyResponse
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
return &response, nil
}