GitLabgitlab-org
cli
main
1package api
3import (
4 "context"
5 "errors"
6 "fmt"
7 "time"
9 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
10)
12// CredentialKind names the scheme a caller must use to present a credential.
13type CredentialKind string
15const (
16 CredentialPAT CredentialKind = "pat"
17 CredentialOAuth2 CredentialKind = "oauth2"
18 CredentialJobToken CredentialKind = "job-token"
19)
21var (
22 ErrUnauthenticated = errors.New("glab is not authenticated; run `glab auth login` to authenticate")
24 // Callers that need a particular kind should report this as "not that kind"
25 // in their own words rather than surfacing the wrapped Go type name.
26 ErrUnsupportedAuthSource = errors.New("unsupported authentication method")
28 // ErrRefreshFailed marks a failed OAuth2 token refresh.
29 ErrRefreshFailed = errors.New("failed to refresh the OAuth2 token")
30)
32// unauthenticatedError matches ErrUnauthenticated but keeps its own message,
33// which names the host to log in to.
34type unauthenticatedError struct {
35 err error
36}
38func (e unauthenticatedError) Error() string {
39 return e.err.Error()
40}
42func (e unauthenticatedError) Unwrap() []error {
43 return []error{e.err, ErrUnauthenticated}
44}
46// Credential is the resolved credential a client authenticates with. Callers
47// that only need to set a request header should use AuthSource().Header().
48type Credential struct {
49 Kind CredentialKind
50 Token string
52 // Zero and empty respectively when the credential does not expire and when
53 // the session behind it cannot be renewed.
54 Expiry time.Time
55 RefreshToken string
56}
58// CredentialKind reports the kind of credential the client holds without
59// resolving it, so a caller that accepts only some kinds can refuse before
60// Credential renews an OAuth2 token it is not going to use.
61func (c *Client) CredentialKind() (CredentialKind, error) {
62 return credentialKind(c.AuthSource())
63}
65// Credential resolves the credential the client authenticates with, renewing an
66// expired OAuth2 access token on the way. ctx does not reach that renewal:
67// x/oauth2's TokenSource has no context-aware Token method.
68func (c *Client) Credential(ctx context.Context) (Credential, error) {
69 as := c.AuthSource()
71 kind, err := credentialKind(as)
72 if err != nil {
73 return Credential{}, err
74 }
76 cred := Credential{Kind: kind}
77 switch as := as.(type) {
78 case gitlab.OAuthTokenSource:
79 token, err := as.TokenSource.Token()
80 if err != nil {
81 return Credential{}, fmt.Errorf("%w: %w", ErrRefreshFailed, err)
82 }
83 cred.Token = token.AccessToken
84 cred.Expiry = token.Expiry
85 cred.RefreshToken = token.RefreshToken
86 case oauth2AccessTokenOnlyAuthSource:
87 // The Duo CLI shows its "session expired" message only when the error
88 // contains "failed to refresh the OAuth2 token", so keep that prefix.
89 if err := as.checkExpiry(); err != nil {
90 return Credential{}, fmt.Errorf("%w: %w", ErrRefreshFailed, err)
91 }
92 cred.Token = as.token
93 cred.Expiry = as.expiry
94 case gitlab.AccessTokenAuthSource:
95 cred.Token = as.Token
96 case gitlab.JobTokenAuthSource:
97 cred.Token = as.Token
98 }
100 if cred.Token == "" {
101 return Credential{}, fmt.Errorf("%w: %T holds no token", ErrUnsupportedAuthSource, as)
102 }
103 return cred, nil
104}
106// credentialKind is the single place that maps an auth source onto a kind.
107func credentialKind(as gitlab.AuthSource) (CredentialKind, error) {
108 switch as.(type) {
109 case gitlab.OAuthTokenSource, oauth2AccessTokenOnlyAuthSource:
110 return CredentialOAuth2, nil
111 case gitlab.AccessTokenAuthSource:
112 return CredentialPAT, nil
113 case gitlab.JobTokenAuthSource:
114 return CredentialJobToken, nil
115 case gitlab.Unauthenticated:
116 return "", ErrUnauthenticated
117 default:
118 return "", fmt.Errorf("%w: %T", ErrUnsupportedAuthSource, as)
119 }
120}
Diagnostics6 git calls · 221 ms
CallStatusSizeTime0221 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 KiB2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=2 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 · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=6 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:02001.1 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=211 git.response.bytes=1165 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=71c8407e0ff0e0af10f6a041ae330f754b68e7eb 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=1165 git.pkt.payload.max=1138 git.sideband.band1.bytes=1138 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1138 git.pack.trailer=61886367c69619d8db2b2cf2d7d24dca2016141cOpen pack map
fetch2001.3 KiB211 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1364 git.response.cache=MISS git.response.duration_ms=211 git.fetch.wants=f2228bd6d6d0ed8354bee2f1f15300a43d2cfc5e 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=1364 git.pkt.payload.max=1337 git.sideband.band1.bytes=1337 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
- a5e16ae9cdaa4b2a
- Request
- 313ec57d…
- 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