-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoken.go
44 lines (37 loc) · 855 Bytes
/
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
/*
* Go OAuth2 Client
*
* MIT License
*
* Copyright (c) 2015 Globo.com
*/
package galf
import (
"encoding/json"
"fmt"
"io"
"strings"
"time"
"github.com/facebookgo/stackerr"
)
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Authorization string
expiresOn time.Time
}
func (t *Token) isValid() bool {
return time.Now().Before(t.expiresOn)
}
func newToken(body io.Reader) (*Token, error) {
var token Token
err := json.NewDecoder(body).Decode(&token)
if err != nil {
return nil, stackerr.Wrap(err)
}
token.TokenType = strings.Title(token.TokenType)
token.expiresOn = time.Now().Add(time.Duration(token.ExpiresIn) * time.Second)
token.Authorization = fmt.Sprintf("%s %s", token.TokenType, token.AccessToken)
return &token, nil
}