/
oauth_token_request.go
102 lines (86 loc) · 2.4 KB
/
oauth_token_request.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
97
98
99
100
101
102
package client
import (
"encoding/json"
"net/http"
"strconv"
"time"
)
func postToOAuthToken(httpClient *http.Client, config Config, body map[string]string) (Token, error) {
bytes, err := UnauthenticatedRequester{}.PostForm(httpClient, config, config.GetActiveTarget().TokenEndpoint, body)
if err != nil {
return Token{}, err
}
resp := tokenResponse{}
err = json.Unmarshal(bytes, &resp)
if err != nil {
return Token{}, parseError(err.Error(), bytes)
}
return Token{
AccessToken: resp.AccessToken,
RefreshToken: resp.RefreshToken,
TokenType: resp.TokenType,
ExpiresAt: time.Now().Add(time.Duration(resp.ExpiresIn) * time.Second).Unix(),
}, nil
}
type AuthorizationCodeClient struct {
ClientID string
ClientSecret string
}
type RefreshTokenClient struct {
ClientID string
ClientSecret string
}
func (acc AuthorizationCodeClient) RequestToken(httpClient *http.Client, config Config, code string, redirectURI string) (Token, error) {
body := map[string]string{
"grant_type": string(AuthCode),
"client_id": acc.ClientID,
"client_secret": acc.ClientSecret,
"response_type": "token",
"redirect_uri": redirectURI,
"code": code,
}
return postToOAuthToken(httpClient, config, body)
}
func (rc RefreshTokenClient) RequestToken(httpClient *http.Client, config Config, refreshToken string) (Token, error) {
body := map[string]string{
"grant_type": string(RefreshToken),
"refresh_token": refreshToken,
"client_id": rc.ClientID,
"client_secret": rc.ClientSecret,
"response_type": "token",
}
return postToOAuthToken(httpClient, config, body)
}
type GrantType string
const (
RefreshToken = GrantType("refresh_token")
AuthCode = GrantType("authorization_code")
)
type FlexInt int64
func (fi *FlexInt) UnmarshalJSON(b []byte) error {
if b[0] != '"' {
return json.Unmarshal(b, (*int64)(fi))
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
i, err := strconv.Atoi(s)
if err != nil {
return err
}
*fi = FlexInt(i)
return nil
}
type tokenResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
ExpiresIn FlexInt `json:"expires_in"`
}
type Token struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
ExpiresAt int64 `json:"expires_at"`
}