瀏覽代碼

expose AuthInfo struct to external code

also get the auth URL
Grant Limberg 3 年之前
父節點
當前提交
1c7a5439d5
共有 1 個文件被更改,包括 60 次插入4 次删除
  1. 60 4
      zeroidc/src/lib.rs

+ 60 - 4
zeroidc/src/lib.rs

@@ -13,7 +13,7 @@ use openidconnect::{ClientId, CsrfToken, IssuerUrl, Nonce, PkceCodeChallenge, Re
 
 use url::Url;
 
-use std::ffi::CStr;
+use std::ffi::{CStr, CString};
 use std::os::raw::c_char;
 
 pub struct ZeroIDC {
@@ -35,7 +35,7 @@ fn nonce_func(nonce: String) -> Box<dyn Fn() -> Nonce> {
     return Box::new(move || Nonce::new(nonce.to_string()));
 }
 
-struct authres {
+pub struct AuthInfo {
     url: Url,
     csrf_token: CsrfToken,
     nonce: Nonce,
@@ -118,7 +118,7 @@ impl ZeroIDC {
         }
     }
 
-    fn get_auth_url(&mut self, csrf_token: String, nonce: String) -> Option<authres> {
+    fn get_auth_info(&mut self, csrf_token: String, nonce: String) -> Option<AuthInfo> {
         let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256();
 
         let r = (*self.inner.lock().unwrap()).oidc_client.as_ref().map(|c| {
@@ -134,7 +134,7 @@ impl ZeroIDC {
                 .set_pkce_challenge(pkce_challenge)
                 .url();
 
-            return authres {
+            return AuthInfo {
                 url: auth_url,
                 csrf_token,
                 nonce,
@@ -214,3 +214,59 @@ pub extern "C" fn zeroidc_stop(ptr: *mut ZeroIDC) {
     };
     idc.stop();
 }
+
+#[no_mangle]
+pub extern "C" fn zeroidc_get_auth_info(
+    ptr: *mut ZeroIDC,
+    csrf_token: *const c_char,
+    nonce: *const c_char,
+) -> *mut AuthInfo {
+    let idc = unsafe {
+        assert!(!ptr.is_null());
+        &mut *ptr
+    };
+
+    if csrf_token.is_null() {
+        println!("csrf_token is null");
+        return std::ptr::null_mut();
+    }
+
+    if nonce.is_null() {
+        println!("nonce is null");
+        return std::ptr::null_mut();
+    }
+
+    let csrf_token = unsafe { CStr::from_ptr(csrf_token) }
+        .to_str()
+        .unwrap()
+        .to_string();
+    let nonce = unsafe { CStr::from_ptr(nonce) }
+        .to_str()
+        .unwrap()
+        .to_string();
+
+    match idc.get_auth_info(csrf_token, nonce) {
+        Some(a) => Box::into_raw(Box::new(a)),
+        None => std::ptr::null_mut(),
+    }
+}
+
+#[no_mangle]
+pub extern "C" fn zeroidc_auth_info_delete(ptr: *mut AuthInfo) {
+    if ptr.is_null() {
+        return;
+    }
+    unsafe {
+        Box::from_raw(ptr);
+    }
+}
+
+#[no_mangle]
+pub extern "C" fn zeroidc_get_auth_url(ptr: *mut AuthInfo) -> *const c_char {
+    let ai = unsafe {
+        assert!(!ptr.is_null());
+        &mut *ptr
+    };
+    let s = CString::new(ai.url.to_string()).unwrap();
+    return s.as_ptr();
+}