|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package tasks |
| 19 | + |
| 20 | +import ( |
| 21 | + "net/http" |
| 22 | + |
| 23 | + "github.com/apache/incubator-devlake/core/errors" |
| 24 | + "github.com/apache/incubator-devlake/core/plugin" |
| 25 | + "github.com/apache/incubator-devlake/plugins/github/models" |
| 26 | + "github.com/apache/incubator-devlake/plugins/github/token" |
| 27 | +) |
| 28 | + |
| 29 | +func CreateAuthenticatedHttpClient( |
| 30 | + taskCtx plugin.TaskContext, |
| 31 | + connection *models.GithubConnection, |
| 32 | + baseClient *http.Client, |
| 33 | +) (*http.Client, errors.Error) { |
| 34 | + |
| 35 | + logger := taskCtx.GetLogger() |
| 36 | + db := taskCtx.GetDal() |
| 37 | + encryptionSecret := taskCtx.GetConfig(plugin.EncodeKeyEnvStr) |
| 38 | + |
| 39 | + if baseClient == nil { |
| 40 | + baseClient = &http.Client{} |
| 41 | + } |
| 42 | + |
| 43 | + // Inject TokenProvider for OAuth refresh or GitHub App installation tokens. |
| 44 | + var tp *token.TokenProvider |
| 45 | + if connection.RefreshToken != "" { |
| 46 | + tp = token.NewTokenProvider(connection, db, baseClient, logger, encryptionSecret) |
| 47 | + } else if connection.AuthMethod == models.AppKey && connection.InstallationID != 0 { |
| 48 | + tp = token.NewAppInstallationTokenProvider(connection, db, baseClient, logger, encryptionSecret) |
| 49 | + } |
| 50 | + |
| 51 | + if tp != nil { |
| 52 | + baseTransport := baseClient.Transport |
| 53 | + if baseTransport == nil { |
| 54 | + baseTransport = http.DefaultTransport |
| 55 | + } |
| 56 | + |
| 57 | + baseClient.Transport = token.NewRefreshRoundTripper(baseTransport, tp) |
| 58 | + logger.Info( |
| 59 | + "Installed token refresh round tripper for connection %d (authMethod=%s)", |
| 60 | + connection.ID, |
| 61 | + connection.AuthMethod, |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + // Persist the freshly minted token so the DB has a correctly encrypted value. |
| 66 | + // PrepareApiClient (called by NewApiClientFromConnection) mints the token |
| 67 | + // in-memory but does not persist it; without this, the DB may contain a stale |
| 68 | + // or corrupted token that breaks GET /connections. |
| 69 | + if connection.AuthMethod == models.AppKey && connection.Token != "" { |
| 70 | + if err := token.PersistEncryptedTokenColumns(db, connection, encryptionSecret, logger, false); err != nil { |
| 71 | + logger.Warn(err, "Failed to persist initial token for connection %d", connection.ID) |
| 72 | + } else { |
| 73 | + logger.Info("Persisted initial token for connection %d", connection.ID) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return baseClient, nil |
| 78 | +} |
0 commit comments