This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FFI for libmsal and BrokerClientApplication
Signed-off-by: David Mulder <[email protected]>
- Loading branch information
Showing
7 changed files
with
1,779 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Unix Azure Entra ID implementation | ||
Copyright (C) David Mulder <[email protected]> 2024 | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
let profile = env::var("PROFILE").unwrap(); | ||
let out_path = match profile.as_str() { | ||
"release" => PathBuf::from("target/release"), | ||
_ => PathBuf::from("target/debug"), | ||
}; | ||
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
|
||
cbindgen::generate(crate_dir) | ||
.expect("Couldn't write bindings!") | ||
.write_to_file(out_path.join("include/msal.h")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
language = "C" | ||
include_guard = "MSAL_H" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Unix Azure Entra ID implementation | ||
Copyright (C) David Mulder <[email protected]> 2024 | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
use crate::MSAL_ERROR; | ||
use std::ffi::{CStr, CString}; | ||
use std::os::raw::{c_char, c_int}; | ||
use std::ptr; | ||
use std::slice; | ||
use tracing::error; | ||
|
||
pub(crate) fn wrap_c_char(input: *const c_char) -> Option<String> { | ||
if input.is_null() { | ||
return None; | ||
} | ||
|
||
let c_str = unsafe { CStr::from_ptr(input) }; | ||
match c_str.to_str() { | ||
Ok(output) => Some(output.to_string()), | ||
Err(_) => None, | ||
} | ||
} | ||
|
||
pub(crate) fn wrap_string(input: &str) -> *mut c_char { | ||
match CString::new(input.to_string()) { | ||
Ok(msg) => msg.into_raw(), | ||
Err(e) => { | ||
error!("{:?}", e); | ||
ptr::null_mut() | ||
} | ||
} | ||
} | ||
|
||
macro_rules! free_object { | ||
($input:ident) => {{ | ||
if !$input.is_null() { | ||
unsafe { | ||
let _ = Box::from_raw($input); | ||
} | ||
} | ||
}}; | ||
} | ||
|
||
macro_rules! run_async { | ||
($client:ident, $func:ident $(, $arg:expr)* $(,)?) => {{ | ||
match runtime::Runtime::new() { | ||
Ok(rt) => rt.block_on(async { | ||
match $client.$func($($arg),*).await { | ||
Ok(resp) => Ok(resp), | ||
Err(e) => { | ||
error!("{:?}", e); | ||
Err(MSAL_ERROR::from(e)) | ||
} | ||
} | ||
}), | ||
Err(e) => { | ||
error!("{:?}", e); | ||
Err(MSAL_ERROR::NO_MEMORY) | ||
} | ||
} | ||
}} | ||
} | ||
|
||
pub(crate) fn str_array_to_vec( | ||
arr: *const *const c_char, | ||
len: c_int, | ||
) -> Result<Vec<String>, MSAL_ERROR> { | ||
let slice = unsafe { slice::from_raw_parts(arr, len as usize) }; | ||
let mut array = Vec::new(); | ||
for &item in slice { | ||
if item.is_null() { | ||
error!("Invalid input {}!", stringify!($arr)); | ||
return Err(MSAL_ERROR::INVALID_POINTER); | ||
} | ||
let c_item = unsafe { CStr::from_ptr(item) }; | ||
let str_item = match c_item.to_str() { | ||
Ok(str_item) => str_item, | ||
Err(e) => { | ||
error!("{:?}", e); | ||
return Err(MSAL_ERROR::INVALID_POINTER); | ||
} | ||
}; | ||
array.push(str_item.to_string()); | ||
} | ||
Ok(array) | ||
} | ||
|
||
macro_rules! str_vec_ref { | ||
($items:ident) => { | ||
$items.iter().map(|i| i.as_str()).collect() | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.