github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/oauth2/oauth2.go (about) 1 package oauth2 2 3 import ( 4 "time" 5 ) 6 7 type Endpoint interface { 8 ExchangeTokenURL(code string) string // 通过code换取access_token的地址 9 RefreshTokenURL(refreshToken string) string // 刷新access_token的地址 10 } 11 12 type TokenStorage interface { 13 Token() (*Token, error) 14 PutToken(*Token) error 15 } 16 17 type Token struct { 18 AccessToken string `json:"access_token"` // 网页授权接口调用凭证 19 CreatedAt int64 `json:"created_at"` // access_token 创建时间, unixtime, 分布式系统要求时间同步, 建议使用 NTP 20 ExpiresIn int64 `json:"expires_in"` // access_token 接口调用凭证超时时间, 单位: 秒 21 RefreshToken string `json:"refresh_token,omitempty"` // 刷新 access_token 的凭证 22 23 OpenId string `json:"openid,omitempty"` 24 UnionId string `json:"unionid,omitempty"` 25 Scope string `json:"scope,omitempty"` // 用户授权的作用域, 使用逗号(,)分隔 26 } 27 28 // Expired 判断 token.AccessToken 是否过期, 过期返回 true, 否则返回 false. 29 func (token *Token) Expired() bool { 30 return time.Now().Unix() >= token.CreatedAt+token.ExpiresIn 31 }