OAuth Web server flow

This commit is contained in:
Wim Velzeboer
2026-09-22 18:05:06 +01:00
parent 8010d33b11
commit 5def68ef61
9 changed files with 205 additions and 144 deletions
+15 -8
View File
@@ -9,11 +9,11 @@
//!
//! The user must have API access enabled to be able to make API calls to Salesforce.
//!
use std::process::Command;
use oauth2::http::header::AUTHORIZATION;
use oauth2::http::{HeaderMap, HeaderValue};
use rustsf_auth::{SalesforceCredentials};
use rustsf_auth::OAuthWebService;
pub const CONNECT_TIMEOUT: u64 = 15;
pub const REQUEST_TIMEOUT: u64 = 30;
@@ -42,7 +42,7 @@ fn open_browser(url: &str) {
}
#[tokio::main]
async fn main(){
async fn main() {
// The client id, urls and scopes.
let login_url = "https://example.my.salesforce.com/";
let client_id = "PlatformCLI"; // The connected app the SFDX CLI is using
@@ -57,7 +57,18 @@ async fn main(){
client_secret,
redirect_url,
scopes,
);
)
// Optionally: add a custom callback response the user will see after authentication
.with_callback_response(
r#"<!doctype html>
<html>
<head><title>Authenticated</title></head>
<body>
<h1>Authentication complete</h1>
<p>You can close this tab.</p>
</body>
</html>"#,
);
let auth_url = web_service.authorization_url().await.unwrap();
// Ask user to authenticate themselves
@@ -65,10 +76,6 @@ async fn main(){
open_browser(&auth_url);
let session = web_service.connect().await.unwrap();
// Constructing the authentication session and connecting to Salesforce
// This will open the default browser and wait for the user to complete the authentication process.
let session = config.connect().await.unwrap();
// Build the headers to include the access token
let mut headers = HeaderMap::new();
let auth_value = format!("Bearer {}", session.access_token().await.unwrap());