GitLabgitlab-org
cli
main
1package oauth2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "net/http"
8 "sync"
9 "time"
11 "golang.org/x/oauth2"
13 "gitlab.com/gitlab-org/api/client-go/v3/gitlaboauth2"
15 "gitlab.com/gitlab-org/cli/internal/config"
16 "gitlab.com/gitlab-org/cli/internal/dbg"
17)
19// adoptAttempts bounds how many times we re-read the freshest credentials after
20// a refresh fails with invalid_grant before giving up. A concurrent process that
21// won the refresh race writes the rotated token to the keyring and then to the
22// config file; a small number of retries covers the brief window between those
23// writes and our failed request returning.
24const adoptAttempts = 3
26// adoptBackoff is the pause between adopt attempts after the first. It is a var
27// so tests can set it to zero and stay fast.
28var adoptBackoff = 50 * time.Millisecond
30// tokenGracePeriod is how much earlier a token is treated as expired, so a
31// git push doesn't outlive it.
32const tokenGracePeriod = 5 * time.Minute
34// validWithGrace reports whether token is usable and will remain so for at
35// least tokenGracePeriod. Token.Valid's own margin is not configurable.
36func validWithGrace(token *oauth2.Token) bool {
37 if !token.Valid() {
38 return false
39 }
40 if token.Expiry.IsZero() {
41 return true
42 }
43 return time.Now().Add(tokenGracePeriod).Before(token.Expiry)
44}
46type configTokenSource struct {
47 cfg config.Config
48 httpClient *http.Client
49 hostname string
51 // searchEnvForIdentity controls whether the initial and freshly re-read
52 // access token may come from GITLAB_TOKEN/GITLAB_ACCESS_TOKEN/OAUTH_TOKEN
53 // in addition to config. See WithoutTokenFromEnvironment's doc comment in
54 // unmarshal for why this must be false for a caller like the Docker
55 // credential helper.
56 searchEnvForIdentity bool
58 oauth2Config *oauth2.Config
60 // Token is not thread-safe
61 mu sync.Mutex
62}
64// NewConfigTokenSource returns an oauth2.TokenSource backed by cfg's stored
65// OAuth2 credentials for hostname, refreshing and persisting a rotated token
66// as needed. searchEnvForIdentity controls whether the access token may also
67// come from GITLAB_TOKEN/GITLAB_ACCESS_TOKEN/OAUTH_TOKEN; pass false wherever
68// glab acts on behalf of another process that inherits the user's shell
69// environment, such as the Docker credential helper, so a stray environment
70// variable cannot substitute a different identity's access token.
71func NewConfigTokenSource(cfg config.Config, httpClient *http.Client, protocol, hostname string, searchEnvForIdentity bool) (oauth2.TokenSource, error) {
72 // Background token refresh, not an interactive login flow: never prompt.
73 clientID, err := oauthClientID(cfg, hostname)
74 if err != nil {
75 return nil, err
76 }
77 if clientID == "" {
78 return nil, nonInteractiveClientIDErr(hostname)
79 }
81 baseURL, err := oauthBaseURL(cfg, hostname, protocol)
82 if err != nil {
83 return nil, err
84 }
86 oauth2Config := gitlaboauth2.NewOAuth2Config(baseURL, clientID, redirectURL, scopes)
88 token, err := unmarshal(hostname, cfg, searchEnvForIdentity)
89 if err != nil {
90 return nil, err
91 }
93 src := &configTokenSource{
94 cfg: cfg,
95 oauth2Config: oauth2Config,
96 httpClient: httpClient,
97 hostname: hostname,
98 searchEnvForIdentity: searchEnvForIdentity,
99 }
101 return oauth2.ReuseTokenSourceWithExpiry(token, src, tokenGracePeriod), nil
102}
104func (c *configTokenSource) Token() (*oauth2.Token, error) {
105 token, presented, err := c.refreshLocked()
106 if err == nil {
107 return token, nil
108 }
110 // A concurrent process may have won the refresh race and consumed the
111 // single-use refresh token out from under us. Re-read the freshest copy and
112 // adopt the winner's rotated token rather than forcing the user to
113 // re-authenticate. A genuinely revoked or expired session yields no valid
114 // token, so the original error is surfaced.
115 //
116 // adopt() only waits for the winner, which is a different process, to commit,
117 // so it runs outside refreshLocked's lock: holding the per-process lock during
118 // its polling would just block this process's other Token() callers, which
119 // would skip-if-valid once the winner has committed.
120 if isInvalidGrant(err) {
121 if adopted, ok := c.adopt(); ok {
122 return adopted, nil
123 }
124 c.forget(presented)
125 }
126 return nil, err
127}
129// forget deletes a refresh token GitLab rejected, so later commands fail
130// locally instead of each sending the same refresh request. It leaves a token
131// another process rotated in since then alone.
132func (c *configTokenSource) forget(rejected string) {
133 fresh, err := c.cfg.Reload()
134 if err != nil {
135 dbg.Debugf("oauth2: forget: could not re-read config for %q: %v", c.hostname, err)
136 return
137 }
139 stored, err := fresh.Get(c.hostname, "oauth2_refresh_token")
140 if err != nil {
141 dbg.Debugf("oauth2: forget: could not read the refresh token for %q: %v", c.hostname, err)
142 return
143 }
144 if stored != rejected {
145 return
146 }
148 if err := fresh.Set(c.hostname, "oauth2_refresh_token", ""); err != nil {
149 dbg.Debugf("oauth2: forget: could not clear the refresh token for %q: %v", c.hostname, err)
150 return
151 }
152 if err := fresh.Write(); err != nil {
153 dbg.Debugf("oauth2: forget: could not write config for %q: %v", c.hostname, err)
154 }
155}
157// refreshLocked runs the read-refresh-write cycle while holding the per-process
158// lock (scoped to this method via defer, so adopt() in Token stays outside it).
159// It returns the already-valid or newly refreshed token, or the refresh error
160// for Token to inspect for a lost race. When the token endpoint rejects the
161// refresh, it also returns the refresh token it presented.
162func (c *configTokenSource) refreshLocked() (*oauth2.Token, string, error) {
163 c.mu.Lock()
164 defer c.mu.Unlock()
166 // Read the freshest view of the credentials. GitLab OAuth refresh tokens are
167 // single-use, so a long-lived process that
168 // acted on its startup-time in-memory copy could present a token another
169 // process has already rotated, or launch a redundant refresh. Re-reading from
170 // disk and the keyring keeps concurrent processes from racing on a stale
171 // token. Writes go back through the same source so the config file is not
172 // rewritten from a stale in-memory document.
173 src, token, err := c.freshest()
174 if err != nil {
175 return nil, "", err
176 }
178 // Another process may have rotated the token while we were reading it. If the
179 // freshest copy is already valid, use it and skip the network refresh (and the
180 // redundant credential write that would follow).
181 if validWithGrace(token) {
182 return token, "", nil
183 }
185 // An access-token-only session has nothing to renew with, so hand the token
186 // back while it is still usable rather than fail five minutes early.
187 if token.RefreshToken == "" && token.Valid() {
188 return token, "", nil
189 }
191 // Everything below is irreversible: GitLab spends the single-use refresh
192 // token the moment it answers, so a write that fails afterwards loses the
193 // session, and the invalid_grant that follows surfaces on a later command
194 // with nothing pointing back at the write.
195 if err := config.CredentialWriteProbe(src, c.hostname); err != nil {
196 // The refreshed token could not be saved, but this one is still usable.
197 if token.Valid() {
198 dbg.Debugf("oauth2: not refreshing early for %q, the refreshed credentials could not be saved: %v", c.hostname, err)
199 return token, "", nil
200 }
201 return nil, "", fmt.Errorf("not refreshing the OAuth token for %q, because the refreshed credentials could not be saved: %w", c.hostname, err)
202 }
204 ctx := context.WithValue(context.Background(), oauth2.HTTPClient, c.httpClient)
206 // Refresh using the stored refresh token. Passing the whole token instead
207 // would skip the refresh: x/oauth2 treats it as valid until 10 seconds
208 // before expiry and returns it as is.
209 refreshedToken, err := c.oauth2Config.TokenSource(ctx, &oauth2.Token{RefreshToken: token.RefreshToken}).Token()
210 if err != nil {
211 return nil, token.RefreshToken, err
212 }
214 err = marshal(c.hostname, src, refreshedToken)
215 if err != nil {
216 return nil, "", err
217 }
219 err = src.Write()
220 if err != nil {
221 return nil, "", err
222 }
224 return refreshedToken, "", nil
225}
227// freshest returns the config to write back through and the freshest token for
228// the host. It prefers a copy re-read from disk and the keyring so a stale
229// in-memory document is neither acted on nor written back; it falls back to the
230// in-memory config when it cannot be re-read.
231func (c *configTokenSource) freshest() (config.Config, *oauth2.Token, error) {
232 src := c.cfg
233 token, err := unmarshal(c.hostname, c.cfg, c.searchEnvForIdentity)
235 fresh, reloadErr := c.cfg.Reload()
236 if reloadErr != nil {
237 dbg.Debugf("oauth2: could not re-read config for %q, using in-memory copy: %v", c.hostname, reloadErr)
238 return src, token, err
239 }
241 freshToken, freshErr := unmarshal(c.hostname, fresh, c.searchEnvForIdentity)
242 if freshErr != nil {
243 dbg.Debugf("oauth2: could not parse re-read config for %q, using in-memory copy: %v", c.hostname, freshErr)
244 return src, token, err
245 }
247 return fresh, freshToken, nil
248}
250// adopt re-reads the freshest credentials after a failed refresh and returns the
251// token if a concurrent process has rotated it to a valid one. It retries a
252// bounded number of times to cover the brief window between the winning process
253// writing the rotated token to the keyring and to the config file.
254func (c *configTokenSource) adopt() (*oauth2.Token, bool) {
255 for attempt := range adoptAttempts {
256 if attempt > 0 {
257 time.Sleep(adoptBackoff)
258 }
260 fresh, err := c.cfg.Reload()
261 if err != nil {
262 dbg.Debugf("oauth2: adopt: could not re-read config for %q: %v", c.hostname, err)
263 return nil, false
264 }
266 token, err := unmarshal(c.hostname, fresh, c.searchEnvForIdentity)
267 if err != nil {
268 dbg.Debugf("oauth2: adopt: could not parse re-read config for %q (attempt %d): %v", c.hostname, attempt+1, err)
269 continue
270 }
271 if token.Valid() {
272 return token, true
273 }
274 }
276 return nil, false
277}
279// isInvalidGrant reports whether err is an OAuth token endpoint error with the
280// RFC 6749 "invalid_grant" code, which GitLab returns when a single-use refresh
281// token has already been consumed (for example, by a concurrent process).
282func isInvalidGrant(err error) bool {
283 if retrieveErr, ok := errors.AsType[*oauth2.RetrieveError](err); ok {
284 return retrieveErr.ErrorCode == "invalid_grant"
285 }
286 return false
287}
Diagnostics6 git calls · 277 ms
CallStatusSizeTime0277 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchtree:0200468 B · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=468 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=c498f09dbd0cebbc59f9eff3b3ef8be66186fac6 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=468 git.pkt.payload.max=441 git.sideband.band1.bytes=441 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=441 git.pack.trailer=6420a98b3b2e923a6b61c550c00de326d620e479Open pack map
fetch2003.5 KiB270 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=270 git.response.bytes=3599 git.response.cache=MISS git.response.duration_ms=270 git.fetch.wants=6c0a0fda2ba1ead140a86ec51b4648382d9abfcc git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3599 git.pkt.payload.max=3572 git.sideband.band1.bytes=3572 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e1f5c81d4defe9
- Request
- f4f97d6e…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format