This repository has been archived by the owner on Jan 4, 2020. It is now read-only.
/
access_token.go
66 lines (55 loc) · 1.72 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
66
package facebook
import (
"fmt"
"time"
)
// AccessToken represents an Graph API access token
type AccessToken struct {
token string
valid bool
expires time.Time
permissions []string
}
// String returns the access token as a string
func (at AccessToken) String() string {
return at.token
}
// Info returns a formatted description of the access token, including the token itself, the permissions, the expiry and the validity.
func (at AccessToken) Info() string {
return fmt.Sprintf("%s, valid: %t, expires: %s, perms: %s", at.token, at.valid, at.expires, at.permissions)
}
// Empty returns true if the access token is equal to the empty string.
func (at AccessToken) Empty() bool {
return at.token == ""
}
// Valid returns the validity status for the token (but doesn't attempt to determine it if not present)
func (at AccessToken) Valid() bool {
return at.valid
}
// Lint figures out what permissions are attached to the current access token.
func (at *AccessToken) Lint(f *Client) error {
if at.token == "" {
return fmt.Errorf("Access token not set")
}
req := f.Get("/debug_token", GraphQueryString{
"input_token": []string{at.token},
"access_token": []string{f.appID + "|" + f.secret},
})
target := struct {
Data struct {
AppID string `json:"app_id"`
Valid bool `json:"is_valid"`
Application string `json:"application"`
UserID int64 `json:"user_id,string"`
ExpiresAt int64 `json:"expires_at"`
Scopes []string `json:"scopes"`
} `json:"data"`
}{}
err := req.Exec(&target)
if err == nil && target.Data.Valid {
at.valid = target.Data.Valid
at.expires = time.Unix(target.Data.ExpiresAt, 0)
at.permissions = target.Data.Scopes
}
return err
}