-
Notifications
You must be signed in to change notification settings - Fork 21
/
oauth.go
155 lines (126 loc) · 3.29 KB
/
oauth.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package blizzard
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/FuzzyStatic/blizzard/v3/oauth"
"golang.org/x/oauth2"
)
// OAuth credentials and access token to access Blizzard API
type OAuth struct {
ClientID string
ClientSecret string
Token *oauth2.Token
}
// AuthorizeConfig returns OAuth2 config
func (c *Client) AuthorizeConfig(redirectURI string, profiles ...oauth.Profile) oauth2.Config {
var scopes []string
for _, profile := range profiles {
scopes = append(scopes, string(profile))
}
c.authorizedCfg = oauth2.Config{
ClientID: c.oauth.ClientID,
ClientSecret: c.oauth.ClientSecret,
Scopes: scopes,
RedirectURL: redirectURI,
Endpoint: oauth2.Endpoint{
AuthURL: c.oauthHost + "/authorize",
TokenURL: c.oauthHost + "/token",
},
}
return c.authorizedCfg
}
// AccessTokenRequest retrieves new OAuth2 Token
func (c *Client) AccessTokenRequest(ctx context.Context) error {
var (
req *http.Request
res *http.Response
body []byte
err error
)
req, err = http.NewRequestWithContext(ctx, "POST", c.clntCredCfg.TokenURL, strings.NewReader("grant_type=client_credentials"))
if err != nil {
return err
}
q := req.URL.Query()
q.Set("grant_type", "client_credentials")
req.URL.RawQuery = q.Encode()
req.SetBasicAuth(c.oauth.ClientID, c.oauth.ClientSecret)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
res, err = c.httpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err = io.ReadAll(res.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, &c.oauth.Token)
if err != nil {
return err
}
return nil
}
// UserInfoHeader returns basic information about the user associated with the current bearer token
func (c *Client) UserInfoHeader(token *oauth2.Token) (*oauth.UserInfo, []byte, error) {
var (
dat oauth.UserInfo
req *http.Request
client *http.Client
res *http.Response
b []byte
err error
)
req, err = http.NewRequest("GET", c.oauthHost+"/userinfo", nil)
if err != nil {
return &dat, b, err
}
client = c.authorizedCfg.Client(context.Background(), token)
res, err = client.Do(req)
if err != nil {
return &dat, b, err
}
defer res.Body.Close()
b, err = io.ReadAll(res.Body)
if err != nil {
return &dat, b, err
}
err = json.Unmarshal(b, &dat)
if err != nil {
return &dat, b, err
}
return &dat, b, nil
}
// TokenValidation verify that a given bearer token is valid and retrieve metadata about the token including the client_id used to create the token, expiration timestamp, and scopes granted to the token
func (c *Client) TokenValidation(ctx context.Context, token *oauth2.Token) (*oauth.TokenValidation, []byte, error) {
var (
dat oauth.TokenValidation
req *http.Request
res *http.Response
b []byte
err error
)
req, err = http.NewRequestWithContext(ctx, "GET", c.oauthHost+fmt.Sprintf("/v2/check_token?token=%s", token.AccessToken), nil)
if err != nil {
return &dat, b, err
}
req.Header.Set("Accept", "application/json")
res, err = c.httpClient.Do(req)
if err != nil {
return &dat, b, err
}
defer res.Body.Close()
b, err = io.ReadAll(res.Body)
if err != nil {
return &dat, b, err
}
err = json.Unmarshal(b, &dat)
if err != nil {
return &dat, b, err
}
return &dat, b, nil
}