-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdggoauth.go
149 lines (130 loc) · 3.76 KB
/
dggoauth.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
package dggoauth
import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/dchest/uniuri"
)
const (
// AuthURL ...
AuthURL = "https://www.destiny.gg/oauth/authorize"
// TokenURL ...
TokenURL = "https://www.destiny.gg/oauth/token"
)
// Client ...
type Client struct {
// mu sync.Mutex
opts *Options
}
// Options ...
type Options struct {
ClientID string
ClientSecret string
RedirectURI string
HTTPClient *http.Client
}
// OauthError ...
type OauthError struct {
Error string `json:"error"`
Message string `json:"message"`
Code int `json:"code"`
}
// NewClient ...
func NewClient(options *Options) (*Client, error) {
if options.ClientID == "" {
return nil, errors.New("A client ID was not provided but is required")
}
if options.ClientSecret == "" {
return nil, errors.New("A client secret was not provided but is required")
}
if options.RedirectURI == "" {
return nil, errors.New("A redirect uri was not provided but is required")
}
if options.HTTPClient == nil {
options.HTTPClient = http.DefaultClient
}
return &Client{
opts: options,
}, nil
}
func (c *Client) generateCodeChallenge(verifier string) string {
secret := fmt.Sprintf("%x", sha256.Sum256([]byte(c.opts.ClientSecret)))
v := []byte(verifier + secret)
sum := fmt.Sprintf("%x", sha256.Sum256(v))
return base64.StdEncoding.EncodeToString([]byte(sum))
}
// GetAuthorizationURL returns the authorization url you need to redirect the user to
func (c *Client) GetAuthorizationURL(state string) (url string, verifier string) {
// min lenght for verifier is 43
verifier = uniuri.NewLen(45)
challenge := c.generateCodeChallenge(verifier)
url = fmt.Sprintf("%s?client_id=%s&redirect_uri=%s&response_type=code&state=%s&code_challenge=%s",
AuthURL,
c.opts.ClientID,
c.opts.RedirectURI,
state,
challenge,
)
return url, verifier
}
// AccessTokenResponse ...
type AccessTokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
TokenType string `json:"token_type"`
}
// GetAccessToken returns the access token with the code and verifier provided
func (c *Client) GetAccessToken(code, verifier string) (*AccessTokenResponse, error) {
s := fmt.Sprintf("%s?grant_type=authorization_code&code=%s&client_id=%s&redirect_uri=%s&code_verifier=%s",
TokenURL,
code,
c.opts.ClientID,
c.opts.RedirectURI,
verifier,
)
response, err := c.opts.HTTPClient.Get(s)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
var oe OauthError
err := json.NewDecoder(response.Body).Decode(&oe)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("error getting access_token, code: %d, message: %s, err: %s", oe.Code, oe.Message, oe.Error)
}
var accessToken AccessTokenResponse
err = json.NewDecoder(response.Body).Decode(&accessToken)
return &accessToken, err
}
// RenewAccessToken renews the access token with the refresh token provided
func (c *Client) RenewAccessToken(refreshToken string) (*AccessTokenResponse, error) {
s := fmt.Sprintf("%s?grant_type=refresh_token&client_id=%s&refresh_token=%s",
TokenURL,
c.opts.ClientID,
refreshToken,
)
response, err := c.opts.HTTPClient.Get(s)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
var oe OauthError
err := json.NewDecoder(response.Body).Decode(&oe)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("error renewing access_token, code: %d, message: %s, err: %s", oe.Code, oe.Message, oe.Error)
}
var accessToken AccessTokenResponse
err = json.NewDecoder(response.Body).Decode(&accessToken)
return &accessToken, err
}