Skip to content

Commit 716271a

Browse files
committed
feat(github): support static token transport for GraphQL and REST clients
add StaticRoundTripper for PAT authentication and use it in the shared http client. since the same client is used by both REST and GraphQL, auth handling must distinguish between refreshable tokens and static tokens. avoid applying refresh/retry logic to PAT. ensures correct behavior across clients and prevents unnecessary retries for static auth.
1 parent 62e59d5 commit 716271a

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

backend/plugins/github/tasks/http_client.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,28 @@ func CreateAuthenticatedHttpClient(
4848
tp = token.NewAppInstallationTokenProvider(connection, db, baseClient, logger, encryptionSecret)
4949
}
5050

51-
if tp != nil {
52-
baseTransport := baseClient.Transport
53-
if baseTransport == nil {
54-
baseTransport = http.DefaultTransport
55-
}
51+
baseTransport := baseClient.Transport
52+
if baseTransport == nil {
53+
baseTransport = http.DefaultTransport
54+
}
5655

56+
if tp != nil {
5757
baseClient.Transport = token.NewRefreshRoundTripper(baseTransport, tp)
5858
logger.Info(
5959
"Installed token refresh round tripper for connection %d (authMethod=%s)",
6060
connection.ID,
6161
connection.AuthMethod,
6262
)
63+
64+
} else if connection.Token != "" {
65+
baseClient.Transport = token.NewStaticRoundTripper(
66+
baseTransport,
67+
connection.Token,
68+
)
69+
logger.Info(
70+
"Installed static token round tripper for connection %d",
71+
connection.ID,
72+
)
6373
}
6474

6575
// Persist the freshly minted token so the DB has a correctly encrypted value.
@@ -73,6 +83,7 @@ func CreateAuthenticatedHttpClient(
7383
logger.Info("Persisted initial token for connection %d", connection.ID)
7484
}
7585
}
86+
println("http client HIT3")
7687

7788
return baseClient, nil
7889
}

backend/plugins/github/token/round_tripper.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,26 @@ func (rt *RefreshRoundTripper) roundTripWithRetry(req *http.Request, refreshAtte
9393

9494
return resp, nil
9595
}
96+
97+
// StaticRoundTripper is an HTTP transport that injects a fixed bearer token.
98+
// Unlike RefreshRoundTripper, it does NOT attempt refresh or retries.
99+
type StaticRoundTripper struct {
100+
base http.RoundTripper
101+
token string
102+
}
103+
104+
func NewStaticRoundTripper(base http.RoundTripper, token string) *StaticRoundTripper {
105+
if base == nil {
106+
base = http.DefaultTransport
107+
}
108+
return &StaticRoundTripper{
109+
base: base,
110+
token: token,
111+
}
112+
}
113+
114+
func (rt *StaticRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
115+
reqClone := req.Clone(req.Context())
116+
reqClone.Header.Set("Authorization", "Bearer "+rt.token)
117+
return rt.base.RoundTrip(reqClone)
118+
}

0 commit comments

Comments
 (0)