-
Notifications
You must be signed in to change notification settings - Fork 2
/
ids_client.go
96 lines (79 loc) · 2.44 KB
/
ids_client.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
96
// Copyright 2021 Edgecast Inc., Licensed under the terms of the Apache 2.0
// license. See LICENSE file in project root for terms.
package ecauth
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
const errorPrefix string = "authentication error:"
// Calls the IDS token endpoint
type IDSClient struct {
IDSBaseUrl *url.URL
}
// NewIDSClientWithURL -
func NewIDSClient(baseURL url.URL) IDSClient {
return IDSClient{IDSBaseUrl: &baseURL}
}
// Gets a new token from the IDS Token Endpoint
func (c IDSClient) GetToken(
credentials OAuth2Credentials,
) (*OAuth2TokenResponse, error) {
data := url.Values{}
data.Set("grant_type", "client_credentials")
data.Add("scope", credentials.Scope)
data.Add("client_id", credentials.ClientID)
data.Add("client_secret", credentials.ClientSecret)
idsTokenEndpoint := fmt.Sprintf("%s/connect/token", c.IDSBaseUrl.String())
dataString := data.Encode()
newTokenRequest, err := http.NewRequest(
"POST",
idsTokenEndpoint,
bytes.NewBufferString(dataString))
if err != nil {
return nil, fmt.Errorf("%s failed creating HTTP request: %w",
errorPrefix, err)
}
newTokenRequest.Header.Add(
"Content-Type",
"application/x-www-form-urlencoded")
newTokenRequest.Header.Add("Cache-Control", "no-cache")
newTokenRequest.Header.Add("Content-Length", strconv.Itoa(len(dataString)))
httpClient := &http.Client{}
resp, err := httpClient.Do(newTokenRequest)
if err != nil {
return nil, fmt.Errorf("%s HTTP request failed: %w", errorPrefix, err)
}
if resp.StatusCode == http.StatusBadRequest {
oAuth2Error := &OAuth2ErrorResponse{}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%s error reading HTTP response: %w",
errorPrefix, err)
}
err = json.Unmarshal(bodyBytes, oAuth2Error)
if err != nil {
// Cannot decode to oAuth2Error so return complete response body
return nil, fmt.Errorf("%s error parsing oAuth2Error response: %s",
errorPrefix, bodyBytes)
}
return nil, fmt.Errorf("%s bad request: %s",
errorPrefix, oAuth2Error.Error)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
"%s expected 200 OK, received status code %d", errorPrefix,
resp.StatusCode)
}
tokenResponse := &OAuth2TokenResponse{}
err = json.NewDecoder(resp.Body).Decode(&tokenResponse)
if err != nil {
return nil, fmt.Errorf("%s error decoding token response: %w",
errorPrefix, err)
}
return tokenResponse, nil
}