]> git.proxmox.com Git - proxmox-openid-rs.git/commitdiff
new helper verify_authorization_code_simple()
authorDietmar Maurer <dietmar@proxmox.com>
Fri, 6 Aug 2021 11:57:41 +0000 (13:57 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 18 Nov 2021 08:32:20 +0000 (09:32 +0100)
Simply return data as serde_json::Value.

src/lib.rs

index a0548f785349aa0d6bfd9d65d0d4e965620d0886..de2251fc90edbe4c70c39ecc285eea324ad05053 100644 (file)
@@ -2,6 +2,7 @@ use std::path::Path;
 
 use anyhow::{format_err, Error};
 use serde::{Deserialize, Serialize};
+use serde_json::Value;
 
 mod http_client;
 pub use http_client::http_client;
@@ -39,7 +40,7 @@ use openidconnect::{
 
 /// Stores Additional Claims into a serde_json::Value;
 #[derive(Debug, Deserialize, Serialize)]
-pub struct GenericClaims(serde_json::Value);
+pub struct GenericClaims(Value);
 impl AdditionalClaims for GenericClaims {}
 
 pub type GenericUserInfoClaims = UserInfoClaims<GenericClaims, CoreGenderClaim>;
@@ -196,4 +197,29 @@ impl OpenIdAuthenticator {
 
         Ok((id_token_claims.clone(), userinfo_claims))
     }
+
+    /// Like verify_authorization_code(), but returns claims as serde_json::Value
+    pub fn verify_authorization_code_simple(
+        &self,
+        code: &str,
+        private_auth_state: &PrivateAuthState,
+    ) -> Result<Value, Error> {
+
+        let (id_token_claims, userinfo_claims) = self.verify_authorization_code(&code, &private_auth_state)?;
+
+        let mut data = serde_json::to_value(id_token_claims)?;
+
+        let data2 = serde_json::to_value(userinfo_claims)?;
+
+        if let Some(map) = data2.as_object() {
+            for (key, value) in map {
+                if data[key] != Value::Null {
+                    continue; // already set
+                }
+                data[key] = value.clone();
+            }
+        }
+
+        Ok(data)
+    }
 }