Просмотр исходного кода

fix: use the client id from mint configuration (#1343)

* fix: use the client id from mint configuration
Leonardo Escuer 2 месяцев назад
Родитель
Сommit
2889084254

+ 14 - 9
crates/cdk-cli/src/sub_commands/cat_device_login.rs

@@ -16,10 +16,6 @@ use crate::token_storage;
 pub struct CatDeviceLoginSubCommand {
 pub struct CatDeviceLoginSubCommand {
     /// Mint url
     /// Mint url
     mint_url: MintUrl,
     mint_url: MintUrl,
-    /// Client ID for OIDC authentication
-    #[arg(default_value = "cashu-client")]
-    #[arg(long)]
-    client_id: String,
 }
 }
 
 
 pub async fn cat_device_login(
 pub async fn cat_device_login(
@@ -39,8 +35,7 @@ pub async fn cat_device_login(
         .await?
         .await?
         .ok_or(anyhow!("Mint info not found"))?;
         .ok_or(anyhow!("Mint info not found"))?;
 
 
-    let (access_token, refresh_token) =
-        get_device_code_token(&mint_info, &sub_command_args.client_id).await;
+    let (access_token, refresh_token) = get_device_code_token(&mint_info).await;
 
 
     // Save tokens to file in work directory
     // Save tokens to file in work directory
     if let Err(e) =
     if let Err(e) =
@@ -60,7 +55,7 @@ pub async fn cat_device_login(
     Ok(())
     Ok(())
 }
 }
 
 
-async fn get_device_code_token(mint_info: &MintInfo, client_id: &str) -> (String, String) {
+async fn get_device_code_token(mint_info: &MintInfo) -> (String, String) {
     let openid_discovery = mint_info
     let openid_discovery = mint_info
         .nuts
         .nuts
         .nut21
         .nut21
@@ -68,6 +63,13 @@ async fn get_device_code_token(mint_info: &MintInfo, client_id: &str) -> (String
         .expect("Nut21 defined")
         .expect("Nut21 defined")
         .openid_discovery;
         .openid_discovery;
 
 
+    let client_id = mint_info
+        .nuts
+        .nut21
+        .clone()
+        .expect("Nut21 defined")
+        .client_id;
+
     let oidc_client = OidcClient::new(openid_discovery, None);
     let oidc_client = OidcClient::new(openid_discovery, None);
 
 
     // Get the OIDC configuration
     // Get the OIDC configuration
@@ -83,7 +85,10 @@ async fn get_device_code_token(mint_info: &MintInfo, client_id: &str) -> (String
     let client = reqwest::Client::new();
     let client = reqwest::Client::new();
     let device_code_response = client
     let device_code_response = client
         .post(device_auth_url)
         .post(device_auth_url)
-        .form(&[("client_id", client_id)])
+        .form(&[
+            ("client_id", client_id.clone().as_str()),
+            ("scope", "openid offline_access"),
+        ])
         .send()
         .send()
         .await
         .await
         .expect("Failed to send device code request");
         .expect("Failed to send device code request");
@@ -129,7 +134,7 @@ async fn get_device_code_token(mint_info: &MintInfo, client_id: &str) -> (String
             .form(&[
             .form(&[
                 ("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
                 ("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
                 ("device_code", device_code),
                 ("device_code", device_code),
-                ("client_id", client_id),
+                ("client_id", client_id.clone().as_str()),
             ])
             ])
             .send()
             .send()
             .await
             .await

+ 10 - 12
crates/cdk-cli/src/sub_commands/cat_login.rs

@@ -18,10 +18,6 @@ pub struct CatLoginSubCommand {
     username: String,
     username: String,
     /// Password
     /// Password
     password: String,
     password: String,
-    /// Client ID for OIDC authentication
-    #[arg(default_value = "cashu-client")]
-    #[arg(long)]
-    client_id: String,
 }
 }
 
 
 pub async fn cat_login(
 pub async fn cat_login(
@@ -43,7 +39,6 @@ pub async fn cat_login(
 
 
     let (access_token, refresh_token) = get_access_token(
     let (access_token, refresh_token) = get_access_token(
         &mint_info,
         &mint_info,
-        &sub_command_args.client_id,
         &sub_command_args.username,
         &sub_command_args.username,
         &sub_command_args.password,
         &sub_command_args.password,
     )
     )
@@ -66,12 +61,7 @@ pub async fn cat_login(
     Ok(())
     Ok(())
 }
 }
 
 
-async fn get_access_token(
-    mint_info: &MintInfo,
-    client_id: &str,
-    user: &str,
-    password: &str,
-) -> (String, String) {
+async fn get_access_token(mint_info: &MintInfo, user: &str, password: &str) -> (String, String) {
     let openid_discovery = mint_info
     let openid_discovery = mint_info
         .nuts
         .nuts
         .nut21
         .nut21
@@ -79,6 +69,13 @@ async fn get_access_token(
         .expect("Nut21 defined")
         .expect("Nut21 defined")
         .openid_discovery;
         .openid_discovery;
 
 
+    let client_id = mint_info
+        .nuts
+        .nut21
+        .clone()
+        .expect("Nut21 defined")
+        .client_id;
+
     let oidc_client = OidcClient::new(openid_discovery, None);
     let oidc_client = OidcClient::new(openid_discovery, None);
 
 
     // Get the token endpoint from the OIDC configuration
     // Get the token endpoint from the OIDC configuration
@@ -91,7 +88,8 @@ async fn get_access_token(
     // Create the request parameters
     // Create the request parameters
     let params = [
     let params = [
         ("grant_type", "password"),
         ("grant_type", "password"),
-        ("client_id", client_id),
+        ("client_id", &client_id),
+        ("scope", "openid offline_access"),
         ("username", user),
         ("username", user),
         ("password", password),
         ("password", password),
     ];
     ];

+ 6 - 1
crates/cdk/examples/auth_wallet.rs

@@ -105,11 +105,16 @@ async fn get_access_token(mint_info: &MintInfo) -> String {
         .await
         .await
         .expect("Failed to get OIDC config")
         .expect("Failed to get OIDC config")
         .token_endpoint;
         .token_endpoint;
+    let client_id = oidc_client
+        .get_oidc_config()
+        .await
+        .expect("Failed to get OIDC config")
+        .token_endpoint;
 
 
     // Create the request parameters
     // Create the request parameters
     let params = [
     let params = [
         ("grant_type", "password"),
         ("grant_type", "password"),
-        ("client_id", "cashu-client"),
+        ("client_id", &client_id),
         ("username", TEST_USERNAME),
         ("username", TEST_USERNAME),
         ("password", TEST_PASSWORD),
         ("password", TEST_PASSWORD),
     ];
     ];