Source File
token.go
Belonging Package
golang.org/x/oauth2/internal
package internal
import (
)
Raw interface{}
}
type tokenJSON struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
ExpiresIn expirationTime `json:"expires_in"` // at least PayPal returns string, while most return number
}
func ( *tokenJSON) () ( time.Time) {
if := .ExpiresIn; != 0 {
return time.Now().Add(time.Duration() * time.Second)
}
return
}
type expirationTime int32
func ( *expirationTime) ( []byte) error {
if len() == 0 || string() == "null" {
return nil
}
var json.Number
:= json.Unmarshal(, &)
if != nil {
return
}
, := .Int64()
if != nil {
return
}
if > math.MaxInt32 {
= math.MaxInt32
}
* = expirationTime()
return nil
}
func ( string) {}
type AuthStyle int
const (
AuthStyleUnknown AuthStyle = 0
AuthStyleInParams AuthStyle = 1
AuthStyleInHeader AuthStyle = 2
)
var authStyleCache struct {
sync.Mutex
m map[string]AuthStyle // keyed by tokenURL
}
func () {
authStyleCache.Lock()
defer authStyleCache.Unlock()
authStyleCache.m = nil
}
func ( string) ( AuthStyle, bool) {
authStyleCache.Lock()
defer authStyleCache.Unlock()
, = authStyleCache.m[]
return
}
func ( string, AuthStyle) {
authStyleCache.Lock()
defer authStyleCache.Unlock()
if authStyleCache.m == nil {
authStyleCache.m = make(map[string]AuthStyle)
}
authStyleCache.m[] =
}
func (, , string, url.Values, AuthStyle) (*http.Request, error) {
if == AuthStyleInParams {
= cloneURLValues()
if != "" {
.Set("client_id", )
}
if != "" {
.Set("client_secret", )
}
}
, := http.NewRequest("POST", , strings.NewReader(.Encode()))
if != nil {
return nil,
}
.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if == AuthStyleInHeader {
.SetBasicAuth(url.QueryEscape(), url.QueryEscape())
}
return , nil
}
func ( url.Values) url.Values {
:= make(url.Values, len())
for , := range {
[] = append([]string(nil), ...)
}
return
}
func ( context.Context, , , string, url.Values, AuthStyle) (*Token, error) {
:= == 0
if {
if , := lookupAuthStyle(); {
=
= false
} else {
= AuthStyleInHeader // the first way we'll try
}
}
, := newTokenRequest(, , , , )
if != nil {
return nil,
}
, := doTokenRoundTrip(, )
= AuthStyleInParams // the second way we'll try
, _ = newTokenRequest(, , , , )
, = doTokenRoundTrip(, )
}
if && == nil {
setAuthStyle(, )
if != nil && .RefreshToken == "" {
.RefreshToken = .Get("refresh_token")
}
return ,
}
func ( context.Context, *http.Request) (*Token, error) {
, := ctxhttp.Do(, ContextClient(), )
if != nil {
return nil,
}
, := ioutil.ReadAll(io.LimitReader(.Body, 1<<20))
.Body.Close()
if != nil {
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", )
}
if := .StatusCode; < 200 || > 299 {
return nil, &RetrieveError{
Response: ,
Body: ,
}
}
var *Token
, , := mime.ParseMediaType(.Header.Get("Content-Type"))
switch {
case "application/x-www-form-urlencoded", "text/plain":
, := url.ParseQuery(string())
if != nil {
return nil,
}
= &Token{
AccessToken: .Get("access_token"),
TokenType: .Get("token_type"),
RefreshToken: .Get("refresh_token"),
Raw: ,
}
:= .Get("expires_in")
, := strconv.Atoi()
if != 0 {
.Expiry = time.Now().Add(time.Duration() * time.Second)
}
default:
var tokenJSON
if = json.Unmarshal(, &); != nil {
return nil,
}
= &Token{
AccessToken: .AccessToken,
TokenType: .TokenType,
RefreshToken: .RefreshToken,
Expiry: .expiry(),
Raw: make(map[string]interface{}),
}
json.Unmarshal(, &.Raw) // no error checks for optional fields
}
if .AccessToken == "" {
return nil, errors.New("oauth2: server response missing access_token")
}
return , nil
}
type RetrieveError struct {
Response *http.Response
Body []byte
}
func ( *RetrieveError) () string {
return fmt.Sprintf("oauth2: cannot fetch token: %v\nResponse: %s", .Response.Status, .Body)
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |