Skip to content

Commit ecf6b64

Browse files
authored
Upg: use proxy for oauth (#11540)
1 parent 1d303f7 commit ecf6b64

File tree

11 files changed

+59
-16
lines changed

11 files changed

+59
-16
lines changed

core/src/oauth/connection.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,34 @@ pub struct RefreshResult {
141141
pub trait Provider {
142142
fn id(&self) -> ConnectionProvider;
143143

144+
fn reqwest_client(&self) -> reqwest::Client {
145+
if let (Ok(proxy_host), Ok(proxy_port), Ok(proxy_user_name), Ok(proxy_user_password)) = (
146+
env::var("PROXY_HOST"),
147+
env::var("PROXY_PORT"),
148+
env::var("PROXY_USER_NAME"),
149+
env::var("PROXY_USER_PASSWORD"),
150+
) {
151+
match reqwest::Proxy::all(format!(
152+
"http://{}:{}@{}:{}",
153+
proxy_user_name, proxy_user_password, proxy_host, proxy_port
154+
)) {
155+
Ok(proxy) => match reqwest::Client::builder().proxy(proxy).build() {
156+
Ok(client) => client,
157+
Err(e) => {
158+
error!(error = ?e, "Failed to create client with proxy");
159+
reqwest::Client::new()
160+
}
161+
},
162+
Err(e) => {
163+
error!(error = ?e, "Failed to create proxy, falling back to no proxy");
164+
reqwest::Client::new()
165+
}
166+
}
167+
} else {
168+
reqwest::Client::new()
169+
}
170+
}
171+
144172
async fn finalize(
145173
&self,
146174
connection: &Connection,

core/src/oauth/providers/confluence.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ impl Provider for ConfluenceConnectionProvider {
5454
"redirect_uri": redirect_uri,
5555
});
5656

57-
let req = reqwest::Client::new()
57+
let req = self
58+
.reqwest_client()
5859
.post("https://auth.atlassian.com/oauth/token")
5960
.header("Content-Type", "application/json")
6061
.json(&body);
@@ -117,7 +118,8 @@ impl Provider for ConfluenceConnectionProvider {
117118
"refresh_token": refresh_token,
118119
});
119120

120-
let req = reqwest::Client::new()
121+
let req = self
122+
.reqwest_client()
121123
.post("https://auth.atlassian.com/oauth/token")
122124
.header("Content-Type", "application/json")
123125
.json(&body);

core/src/oauth/providers/github.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ impl GithubConnectionProvider {
9898
code: &str,
9999
) -> Result<(String, u64, serde_json::Value), ProviderError> {
100100
// https://github.com/octokit/auth-app.js/blob/main/src/get-installation-authentication.ts
101-
let req = reqwest::Client::new()
101+
let req = self
102+
.reqwest_client()
102103
.post(format!(
103104
"https://api.github.com/app/installations/{}/access_tokens",
104105
code

core/src/oauth/providers/gong.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ impl Provider for GongConnectionProvider {
5555
("redirect_uri", &redirect_uri),
5656
];
5757

58-
let req = reqwest::Client::new()
58+
let req = self
59+
.reqwest_client()
5960
.post("https://app.gong.io/oauth2/generate-customer-token")
6061
.header("Content-Type", "application/json")
6162
.header("Authorization", authorization)
@@ -113,7 +114,8 @@ impl Provider for GongConnectionProvider {
113114
("refresh_token", &refresh_token),
114115
];
115116

116-
let req = reqwest::Client::new()
117+
let req = self
118+
.reqwest_client()
117119
.post("https://app.gong.io/oauth2/generate-customer-token")
118120
.header("Content-Type", "application/json")
119121
.header("Authorization", authorization)

core/src/oauth/providers/google_drive.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ impl Provider for GoogleDriveConnectionProvider {
5454
"redirect_uri": redirect_uri,
5555
});
5656

57-
let req = reqwest::Client::new()
57+
let req = self
58+
.reqwest_client()
5859
.post("https://oauth2.googleapis.com/token")
5960
.header("Content-Type", "application/json")
6061
.json(&body);
@@ -124,7 +125,8 @@ impl Provider for GoogleDriveConnectionProvider {
124125
"refresh_token": refresh_token,
125126
});
126127

127-
let req = reqwest::Client::new()
128+
let req = self
129+
.reqwest_client()
128130
.post("https://oauth2.googleapis.com/token")
129131
.header("Content-Type", "application/json")
130132
.json(&body);

core/src/oauth/providers/intercom.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ impl Provider for IntercomConnectionProvider {
4646
"redirect_uri": redirect_uri,
4747
});
4848

49-
let req = reqwest::Client::new()
49+
let req = self
50+
.reqwest_client()
5051
.post("https://api.intercom.io/auth/eagle/token")
5152
.header("Content-Type", "application/json")
5253
.json(&body);

core/src/oauth/providers/microsoft.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ impl Provider for MicrosoftConnectionProvider {
5454
"scope": "User.Read Sites.Read.All Directory.Read.All Files.Read.All Team.ReadBasic.All ChannelSettings.Read.All ChannelMessage.Read.All",
5555
});
5656

57-
let req = reqwest::Client::new()
57+
let req = self
58+
.reqwest_client()
5859
.post("https://login.microsoftonline.com/common/oauth2/v2.0/token")
5960
.header("Content-Type", "application/x-www-form-urlencoded")
6061
.form(&body);
@@ -104,7 +105,8 @@ impl Provider for MicrosoftConnectionProvider {
104105
"scope": "User.Read Sites.Read.All Directory.Read.All Files.Read.All Team.ReadBasic.All ChannelSettings.Read.All ChannelMessage.Read.All",
105106
});
106107

107-
let req = reqwest::Client::new()
108+
let req = self
109+
.reqwest_client()
108110
.post("https://login.microsoftonline.com/common/oauth2/v2.0/token")
109111
.header("Content-Type", "application/x-www-form-urlencoded")
110112
.form(&body);

core/src/oauth/providers/notion.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ impl Provider for NotionConnectionProvider {
5151
"redirect_uri": redirect_uri,
5252
});
5353

54-
let req = reqwest::Client::new()
54+
let req = self
55+
.reqwest_client()
5556
.post("https://api.notion.com/v1/oauth/token")
5657
.header("Accept", "application/json")
5758
.header("Content-Type", "application/json")

core/src/oauth/providers/salesforce.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ impl Provider for SalesforceConnectionProvider {
8787
form_data.insert("redirect_uri", redirect_uri);
8888
form_data.insert("code_verifier", code_verifier);
8989

90-
let req = reqwest::Client::new()
90+
let req = self
91+
.reqwest_client()
9192
.post(format!("{}/services/oauth2/token", instance_url))
9293
.header("Content-Type", "application/x-www-form-urlencoded")
9394
.form(&form_data);
@@ -134,7 +135,8 @@ impl Provider for SalesforceConnectionProvider {
134135
form_data.insert("client_secret", &client_secret);
135136
form_data.insert("refresh_token", &refresh_token);
136137

137-
let req = reqwest::Client::new()
138+
let req = self
139+
.reqwest_client()
138140
.post(format!("{}/services/oauth2/token", instance_url))
139141
.header("Content-Type", "application/x-www-form-urlencoded")
140142
.form(&form_data);

core/src/oauth/providers/slack.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ impl Provider for SlackConnectionProvider {
4949
code: &str,
5050
redirect_uri: &str,
5151
) -> Result<FinalizeResult, ProviderError> {
52-
let req = reqwest::Client::new()
52+
let req = self
53+
.reqwest_client()
5354
.post("https://slack.com/api/oauth.v2.access")
5455
.header("Content-Type", "application/x-www-form-urlencoded")
5556
.header("Authorization", format!("Basic {}", self.basic_auth()))
@@ -98,7 +99,7 @@ impl Provider for SlackConnectionProvider {
9899
// .unseal_refresh_token()?
99100
// .ok_or_else(|| anyhow!("Missing `refresh_token` in Slack connection"))?;
100101

101-
// let req = reqwest::Client::new()
102+
// let req = self.reqwest_client()
102103
// .post("https://slack.com/api/oauth.v2.access")
103104
// .header("Authorization", format!("Basic {}", self.basic_auth()))
104105
// .header("Content-Type", "application/json; charset=utf-8")

0 commit comments

Comments
 (0)