/
access_token.go
65 lines (55 loc) · 1.82 KB
/
access_token.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
package discord
import (
"time"
"github.com/disgoorg/json"
)
// AccessTokenResponse is the response from the OAuth2 exchange endpoint.
// See https://discord.com/developers/docs/topics/oauth2#authorization-code-grant for more information.
type AccessTokenResponse struct {
AccessToken string `json:"access_token"`
TokenType TokenType `json:"token_type"`
ExpiresIn time.Duration `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope []OAuth2Scope `json:"scope"`
// Webhook is only present if scopes include the OAuth2ScopeWebhookIncoming
Webhook *IncomingWebhook `json:"webhook"`
}
// UnmarshalJSON implements json.Unmarshaler.
func (e *AccessTokenResponse) UnmarshalJSON(data []byte) error {
type accessTokenResponse AccessTokenResponse
var v struct {
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
accessTokenResponse
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*e = AccessTokenResponse(v.accessTokenResponse)
e.ExpiresIn = time.Duration(v.ExpiresIn) * time.Second
e.Scope = SplitScopes(v.Scope)
return nil
}
func (e AccessTokenResponse) MarshalJSON() ([]byte, error) {
type accessTokenResponse AccessTokenResponse
return json.Marshal(struct {
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
accessTokenResponse
}{
ExpiresIn: int(e.ExpiresIn.Seconds()),
Scope: JoinScopes(e.Scope),
accessTokenResponse: (accessTokenResponse)(e),
})
}
// GrantType defines what type of request is being made.
type GrantType string
// Discord's supported GrantType(s).
const (
GrantTypeAuthorizationCode GrantType = "authorization_code"
GrantTypeRefreshToken GrantType = "refresh_token"
)
// String returns the GrantType as a string.
func (t GrantType) String() string {
return string(t)
}