-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentification.go
60 lines (55 loc) · 1.55 KB
/
authentification.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
package gorte
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/hashicorp/go-retryablehttp"
)
type AuthToken struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
ExpiryDate time.Time `json:"expiry_date"`
}
func (c *Client) authenticate(req *retryablehttp.Request) error {
if time.Now().After(c.token.ExpiryDate) {
var err error
c.token, err = c.newToken()
if err != nil {
return err
}
c.logger.Info(fmt.Sprintf("New token generated! expires at %s", c.token.ExpiryDate.Format("2006-01-02 15:04:05")))
}
req.Header.Set("Authorization", c.token.TokenType+" "+c.token.AccessToken)
return nil
}
func (c *Client) newToken() (*AuthToken, error) {
authURL, err := URLGenerator(c.baseURL, "token/oauth/")
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", authURL.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Basic "+c.config.Key)
resp, err := c.client.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var token AuthToken
if err := json.Unmarshal(body, &token); err != nil {
return nil, err
}
token.ExpiryDate = time.Now().Add(time.Duration(token.ExpiresIn) * time.Second)
c.logger.Debug(fmt.Sprintf("token was successfully generated! expires in %s", token.ExpiryDate.Format("2006-01-02 15:04:05")))
return &token, nil
}