github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/golang.org/x/oauth2/token.go (about) 1 // Copyright 2014 The oauth2 Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package oauth2 6 7 import ( 8 "net/http" 9 "net/url" 10 "strings" 11 "time" 12 13 "golang.org/x/net/context" 14 "golang.org/x/oauth2/internal" 15 ) 16 17 // expiryDelta determines how earlier a token should be considered 18 // expired than its actual expiration time. It is used to avoid late 19 // expirations due to client-server time mismatches. 20 const expiryDelta = 10 * time.Second 21 22 // Token represents the crendentials used to authorize 23 // the requests to access protected resources on the OAuth 2.0 24 // provider's backend. 25 // 26 // Most users of this package should not access fields of Token 27 // directly. They're exported mostly for use by related packages 28 // implementing derivative OAuth2 flows. 29 type Token struct { 30 // AccessToken is the token that authorizes and authenticates 31 // the requests. 32 AccessToken string `json:"access_token"` 33 34 // TokenType is the type of token. 35 // The Type method returns either this or "Bearer", the default. 36 TokenType string `json:"token_type,omitempty"` 37 38 // RefreshToken is a token that's used by the application 39 // (as opposed to the user) to refresh the access token 40 // if it expires. 41 RefreshToken string `json:"refresh_token,omitempty"` 42 43 // Expiry is the optional expiration time of the access token. 44 // 45 // If zero, TokenSource implementations will reuse the same 46 // token forever and RefreshToken or equivalent 47 // mechanisms for that TokenSource will not be used. 48 Expiry time.Time `json:"expiry,omitempty"` 49 50 // raw optionally contains extra metadata from the server 51 // when updating a token. 52 raw interface{} 53 } 54 55 // Type returns t.TokenType if non-empty, else "Bearer". 56 func (t *Token) Type() string { 57 if strings.EqualFold(t.TokenType, "bearer") { 58 return "Bearer" 59 } 60 if strings.EqualFold(t.TokenType, "mac") { 61 return "MAC" 62 } 63 if strings.EqualFold(t.TokenType, "basic") { 64 return "Basic" 65 } 66 if t.TokenType != "" { 67 return t.TokenType 68 } 69 return "Bearer" 70 } 71 72 // SetAuthHeader sets the Authorization header to r using the access 73 // token in t. 74 // 75 // This method is unnecessary when using Transport or an HTTP Client 76 // returned by this package. 77 func (t *Token) SetAuthHeader(r *http.Request) { 78 r.Header.Set("Authorization", t.Type()+" "+t.AccessToken) 79 } 80 81 // WithExtra returns a new Token that's a clone of t, but using the 82 // provided raw extra map. This is only intended for use by packages 83 // implementing derivative OAuth2 flows. 84 func (t *Token) WithExtra(extra interface{}) *Token { 85 t2 := new(Token) 86 *t2 = *t 87 t2.raw = extra 88 return t2 89 } 90 91 // Extra returns an extra field. 92 // Extra fields are key-value pairs returned by the server as a 93 // part of the token retrieval response. 94 func (t *Token) Extra(key string) interface{} { 95 if vals, ok := t.raw.(url.Values); ok { 96 // TODO(jbd): Cast numeric values to int64 or float64. 97 return vals.Get(key) 98 } 99 if raw, ok := t.raw.(map[string]interface{}); ok { 100 return raw[key] 101 } 102 return nil 103 } 104 105 // expired reports whether the token is expired. 106 // t must be non-nil. 107 func (t *Token) expired() bool { 108 if t.Expiry.IsZero() { 109 return false 110 } 111 return t.Expiry.Add(-expiryDelta).Before(time.Now()) 112 } 113 114 // Valid reports whether t is non-nil, has an AccessToken, and is not expired. 115 func (t *Token) Valid() bool { 116 return t != nil && t.AccessToken != "" && !t.expired() 117 } 118 119 // tokenFromInternal maps an *internal.Token struct into 120 // a *Token struct. 121 func tokenFromInternal(t *internal.Token) *Token { 122 if t == nil { 123 return nil 124 } 125 return &Token{ 126 AccessToken: t.AccessToken, 127 TokenType: t.TokenType, 128 RefreshToken: t.RefreshToken, 129 Expiry: t.Expiry, 130 raw: t.Raw, 131 } 132 } 133 134 // retrieveToken takes a *Config and uses that to retrieve an *internal.Token. 135 // This token is then mapped from *internal.Token into an *oauth2.Token which is returned along 136 // with an error.. 137 func retrieveToken(ctx context.Context, c *Config, v url.Values) (*Token, error) { 138 tk, err := internal.RetrieveToken(ctx, c.ClientID, c.ClientSecret, c.Endpoint.TokenURL, v) 139 if err != nil { 140 return nil, err 141 } 142 return tokenFromInternal(tk), nil 143 }