expose AuthInfo struct to external code

also get the auth URL
This commit is contained in:
Grant Limberg 2021-10-29 16:04:19 -07:00
parent ebc4c898ff
commit 1c7a5439d5
No known key found for this signature in database
GPG Key ID: 2BA62CCABBB4095A

View File

@ -13,7 +13,7 @@ use openidconnect::{ClientId, CsrfToken, IssuerUrl, Nonce, PkceCodeChallenge, Re
use url::Url; use url::Url;
use std::ffi::CStr; use std::ffi::{CStr, CString};
use std::os::raw::c_char; use std::os::raw::c_char;
pub struct ZeroIDC { 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())); return Box::new(move || Nonce::new(nonce.to_string()));
} }
struct authres { pub struct AuthInfo {
url: Url, url: Url,
csrf_token: CsrfToken, csrf_token: CsrfToken,
nonce: Nonce, 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 (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256();
let r = (*self.inner.lock().unwrap()).oidc_client.as_ref().map(|c| { let r = (*self.inner.lock().unwrap()).oidc_client.as_ref().map(|c| {
@ -134,7 +134,7 @@ impl ZeroIDC {
.set_pkce_challenge(pkce_challenge) .set_pkce_challenge(pkce_challenge)
.url(); .url();
return authres { return AuthInfo {
url: auth_url, url: auth_url,
csrf_token, csrf_token,
nonce, nonce,
@ -214,3 +214,59 @@ pub extern "C" fn zeroidc_stop(ptr: *mut ZeroIDC) {
}; };
idc.stop(); 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();
}