Fix oauth cross repository push

Cross repository push tokens were not being cached and could not be used,
now any returned token will be used and the caching is hidden in the getToken function.

Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
This commit is contained in:
Derek McGowan 2016-03-04 15:13:27 -08:00
parent 274825f403
commit a8aa2f78de

View file

@ -213,16 +213,18 @@ func (th *tokenHandler) AuthorizeRequest(req *http.Request, params map[string]st
Actions: []string{"pull"}, Actions: []string{"pull"},
}.String()) }.String())
} }
if err := th.refreshToken(params, additionalScopes...); err != nil {
token, err := th.getToken(params, additionalScopes...)
if err != nil {
return err return err
} }
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", th.tokenCache)) req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
return nil return nil
} }
func (th *tokenHandler) refreshToken(params map[string]string, additionalScopes ...string) error { func (th *tokenHandler) getToken(params map[string]string, additionalScopes ...string) (string, error) {
th.tokenLock.Lock() th.tokenLock.Lock()
defer th.tokenLock.Unlock() defer th.tokenLock.Unlock()
scopes := make([]string, 0, len(th.scopes)+len(additionalScopes)) scopes := make([]string, 0, len(th.scopes)+len(additionalScopes))
@ -239,7 +241,7 @@ func (th *tokenHandler) refreshToken(params map[string]string, additionalScopes
if now.After(th.tokenExpiration) || addedScopes { if now.After(th.tokenExpiration) || addedScopes {
token, expiration, err := th.fetchToken(params, scopes) token, expiration, err := th.fetchToken(params, scopes)
if err != nil { if err != nil {
return err return "", err
} }
// do not update cache for added scope tokens // do not update cache for added scope tokens
@ -247,9 +249,11 @@ func (th *tokenHandler) refreshToken(params map[string]string, additionalScopes
th.tokenCache = token th.tokenCache = token
th.tokenExpiration = expiration th.tokenExpiration = expiration
} }
return token, nil
} }
return nil return th.tokenCache, nil
} }
type postTokenResponse struct { type postTokenResponse struct {