OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package internal | 5 package internal |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "time" | 9 "time" |
10 | 10 |
11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
12 "golang.org/x/oauth2" | 12 "golang.org/x/oauth2" |
13 | 13 |
14 "github.com/luci/luci-go/common/errors" | |
15 "github.com/luci/luci-go/common/logging" | 14 "github.com/luci/luci-go/common/logging" |
| 15 "github.com/luci/luci-go/common/retry/transient" |
16 ) | 16 ) |
17 | 17 |
18 type userAuthTokenProvider struct { | 18 type userAuthTokenProvider struct { |
19 config *oauth2.Config | 19 config *oauth2.Config |
20 cacheKey CacheKey | 20 cacheKey CacheKey |
21 } | 21 } |
22 | 22 |
23 // NewUserAuthTokenProvider returns TokenProvider that can perform 3-legged | 23 // NewUserAuthTokenProvider returns TokenProvider that can perform 3-legged |
24 // OAuth flow involving interaction with a user. | 24 // OAuth flow involving interaction with a user. |
25 func NewUserAuthTokenProvider(ctx context.Context, clientID, clientSecret string
, scopes []string) (TokenProvider, error) { | 25 func NewUserAuthTokenProvider(ctx context.Context, clientID, clientSecret string
, scopes []string) (TokenProvider, error) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 } | 81 } |
82 | 82 |
83 func (p *userAuthTokenProvider) RefreshToken(ctx context.Context, prev, base *oa
uth2.Token) (*oauth2.Token, error) { | 83 func (p *userAuthTokenProvider) RefreshToken(ctx context.Context, prev, base *oa
uth2.Token) (*oauth2.Token, error) { |
84 // Clear expiration time to force token refresh. Do not use 0 since it m
eans | 84 // Clear expiration time to force token refresh. Do not use 0 since it m
eans |
85 // that token never expires. | 85 // that token never expires. |
86 t := *prev | 86 t := *prev |
87 t.Expiry = time.Unix(1, 0) | 87 t.Expiry = time.Unix(1, 0) |
88 switch newTok, err := grabToken(p.config.TokenSource(ctx, &t)); { | 88 switch newTok, err := grabToken(p.config.TokenSource(ctx, &t)); { |
89 case err == nil: | 89 case err == nil: |
90 return newTok, nil | 90 return newTok, nil |
91 » case errors.IsTransient(err): | 91 » case transient.Tag.In(err): |
92 logging.Warningf(ctx, "Transient error when refreshing the token
- %s", err) | 92 logging.Warningf(ctx, "Transient error when refreshing the token
- %s", err) |
93 return nil, err | 93 return nil, err |
94 default: | 94 default: |
95 logging.Warningf(ctx, "Bad refresh token - %s", err) | 95 logging.Warningf(ctx, "Bad refresh token - %s", err) |
96 return nil, ErrBadRefreshToken | 96 return nil, ErrBadRefreshToken |
97 } | 97 } |
98 } | 98 } |
OLD | NEW |