-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
32 lines (26 loc) · 940 Bytes
/
auth.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
package twitch
import (
"context"
"net/http"
"time"
"github.com/pkg/errors"
)
// GetTokenInfo requests a validation for the token set within the
// client and returns the authorized user, their granted scopes on this
// token and an error in case something went wrong.
func (c *Client) GetTokenInfo(ctx context.Context) (user string, scopes []string, expiresAt time.Time, err error) {
var payload OAuthTokenValidationResponse
if c.accessToken == "" {
return "", nil, time.Time{}, errors.New("no access token present")
}
if err := c.Request(ctx, ClientRequestOpts{
AuthType: AuthTypeBearerToken,
Method: http.MethodGet,
OKStatus: http.StatusOK,
Out: &payload,
URL: "https://id.twitch.tv/oauth2/validate",
}); err != nil {
return "", nil, time.Time{}, errors.Wrap(err, "validating token")
}
return payload.Login, payload.Scopes, time.Now().Add(time.Duration(payload.ExpiresIn) * time.Second), nil
}