use serde_json::{Value, json};
use windows::Win32::Foundation::HWND;
use windows::Win32::Networking::WindowsWebServices::*;
use windows::Win32::System::Threading::GetCurrentProcessId;
use windows::Win32::UI::Input::KeyboardAndMouse::GetActiveWindow;
use windows::Win32::UI::WindowsAndMessaging::{GetForegroundWindow, GetWindowThreadProcessId};
use windows::core::{HSTRING, PCWSTR};
use super::OnDone;
use crate::crypto::{base64url, base64url_decode};
use crate::{AuthError, Support};
const HRESULT_CANCELLED: i32 = 0x8007_04C7_u32 as i32;
const HRESULT_EXISTS: i32 = 0x8009_000F_u32 as i32;
const HRESULT_NOT_SUPPORTED: i32 = 0x8009_0029_u32 as i32;
const HRESULT_INVALID_PARAMETER: i32 = 0x8009_0027_u32 as i32;
pub fn support() -> Support {
Support::Native
}
pub fn available(on_done: Box<dyn FnOnce(bool) + Send>) {
let available = unsafe { WebAuthNIsUserVerifyingPlatformAuthenticatorAvailable() }
.is_ok_and(windows::core::BOOL::as_bool);
on_done(available);
}
#[derive(Clone, Copy)]
struct Window(isize);
fn owner_window() -> Option<Window> {
unsafe {
let active = GetActiveWindow();
if !active.is_invalid() {
return Some(Window(active.0 as isize));
}
let foreground = GetForegroundWindow();
if foreground.is_invalid() {
return None;
}
let mut pid = 0u32;
GetWindowThreadProcessId(foreground, Some(&raw mut pid));
(pid == GetCurrentProcessId()).then_some(Window(foreground.0 as isize))
}
}
fn on_worker(
name: &'static str,
on_done: OnDone,
work: impl FnOnce() -> Result<Value, AuthError> + Send + 'static,
) {
let spawned = std::thread::Builder::new()
.name(name.into())
.spawn(move || on_done(work()));
if let Err(e) = spawned {
eprintln!("day-part-auth: could not start the passkey thread: {e}");
}
}
pub fn create(options_json: String, on_done: OnDone) {
let Some(window) = owner_window() else {
on_done(Err(AuthError::Platform(
"no window to show the passkey dialog from".into(),
)));
return;
};
on_worker("day-auth-passkey", on_done, move || {
make_credential(window, &options_json)
});
}
pub fn get(options_json: String, on_done: OnDone) {
let Some(window) = owner_window() else {
on_done(Err(AuthError::Platform(
"no window to show the passkey dialog from".into(),
)));
return;
};
on_worker("day-auth-passkey", on_done, move || {
get_assertion(window, &options_json)
});
}
fn parse(options_json: &str) -> Result<Value, AuthError> {
serde_json::from_str(options_json).map_err(|e| AuthError::InvalidRequest(e.to_string()))
}
fn text<'a>(v: &'a Value, path: &[&str]) -> Option<&'a str> {
let mut cur = v;
for key in path {
cur = cur.get(key)?;
}
cur.as_str()
}
fn bytes_field(v: &Value, path: &[&str]) -> Result<Vec<u8>, AuthError> {
let text = text(v, path)
.ok_or_else(|| AuthError::InvalidRequest(format!("missing {}", path.join("."))))?;
base64url_decode(text)
.ok_or_else(|| AuthError::InvalidRequest(format!("{} is not base64url", path.join("."))))
}
fn user_verification(v: Option<&str>) -> u32 {
match v {
Some("required") => WEBAUTHN_USER_VERIFICATION_REQUIREMENT_REQUIRED,
Some("preferred") => WEBAUTHN_USER_VERIFICATION_REQUIREMENT_PREFERRED,
Some("discouraged") => WEBAUTHN_USER_VERIFICATION_REQUIREMENT_DISCOURAGED,
_ => WEBAUTHN_USER_VERIFICATION_REQUIREMENT_ANY,
}
}
fn attachment(v: Option<&str>) -> u32 {
match v {
Some("platform") => WEBAUTHN_AUTHENTICATOR_ATTACHMENT_PLATFORM,
Some("cross-platform") => WEBAUTHN_AUTHENTICATOR_ATTACHMENT_CROSS_PLATFORM,
_ => WEBAUTHN_AUTHENTICATOR_ATTACHMENT_ANY,
}
}
fn timeout(v: &Value) -> u32 {
v.get("timeout")
.and_then(Value::as_u64)
.and_then(|t| u32::try_from(t).ok())
.filter(|t| *t > 0)
.unwrap_or(120_000)
}
fn client_data(kind: &str, options: &Value, rp_id: &str) -> Result<Vec<u8>, AuthError> {
let challenge = text(options, &["challenge"])
.ok_or_else(|| AuthError::InvalidRequest("missing challenge".into()))?;
let origin =
text(options, &["origin"]).map_or_else(|| format!("https://{rp_id}"), str::to_string);
let data = json!({
"type": kind,
"challenge": challenge,
"origin": origin,
"crossOrigin": false,
});
serde_json::to_vec(&data).map_err(|e| AuthError::Platform(e.to_string()))
}
fn credential_list(items: Option<&Vec<Value>>) -> (Vec<Vec<u8>>, Vec<WEBAUTHN_CREDENTIAL>) {
let mut ids: Vec<Vec<u8>> = items.map_or_default(|list| {
list.iter()
.filter_map(|c| {
c.get("id")
.and_then(Value::as_str)
.and_then(base64url_decode)
})
.collect()
});
let creds = ids
.iter_mut()
.map(|id| WEBAUTHN_CREDENTIAL {
dwVersion: WEBAUTHN_CREDENTIAL_CURRENT_VERSION,
cbId: id.len() as u32,
pbId: id.as_mut_ptr(),
pwszCredentialType: WEBAUTHN_CREDENTIAL_TYPE_PUBLIC_KEY,
})
.collect();
(ids, creds)
}
fn platform_error(e: &windows::core::Error) -> AuthError {
match e.code().0 {
HRESULT_CANCELLED => AuthError::Cancelled,
HRESULT_EXISTS => AuthError::Provider {
error: "invalid_state".into(),
description: Some("a passkey for this account already exists on this device".into()),
},
HRESULT_NOT_SUPPORTED => AuthError::Unsupported,
HRESULT_INVALID_PARAMETER => AuthError::InvalidRequest(e.message()),
code => {
let name = unsafe { WebAuthNGetErrorName(e.code()) };
let name = if name.is_null() {
String::new()
} else {
unsafe { name.to_string() }.unwrap_or_default()
};
AuthError::Platform(format!("WebAuthn {name} ({code:#x}): {}", e.message()))
}
}
}
unsafe fn copy(ptr: *mut u8, len: u32) -> Vec<u8> {
if ptr.is_null() || len == 0 {
return Vec::new();
}
unsafe { std::slice::from_raw_parts(ptr, len as usize).to_vec() }
}
fn make_credential(window: Window, options_json: &str) -> Result<Value, AuthError> {
let options = parse(options_json)?;
let rp_id = text(&options, &["rp", "id"]).unwrap_or("").to_string();
let rp_name = HSTRING::from(text(&options, &["rp", "name"]).unwrap_or(""));
let rp_id_w = HSTRING::from(rp_id.as_str());
let mut user_id = bytes_field(&options, &["user", "id"])?;
let user_name = HSTRING::from(text(&options, &["user", "name"]).unwrap_or(""));
let user_display = HSTRING::from(text(&options, &["user", "displayName"]).unwrap_or(""));
let mut params: Vec<WEBAUTHN_COSE_CREDENTIAL_PARAMETER> = options
.get("pubKeyCredParams")
.and_then(Value::as_array)
.map_or_default(|list| {
list.iter()
.filter(|p| p.get("type").and_then(Value::as_str) == Some("public-key"))
.filter_map(|p| p.get("alg").and_then(Value::as_i64))
.filter_map(|alg| i32::try_from(alg).ok())
.map(|alg| WEBAUTHN_COSE_CREDENTIAL_PARAMETER {
dwVersion: WEBAUTHN_COSE_CREDENTIAL_PARAMETER_CURRENT_VERSION,
pwszCredentialType: WEBAUTHN_CREDENTIAL_TYPE_PUBLIC_KEY,
lAlg: alg,
})
.collect()
});
if params.is_empty() {
return Err(AuthError::InvalidRequest(
"no usable pubKeyCredParams".into(),
));
}
let mut client = client_data("webauthn.create", &options, &rp_id)?;
let (_exclude_ids, mut exclude) =
credential_list(options.get("excludeCredentials").and_then(Value::as_array));
let selection = options.get("authenticatorSelection");
let resident = selection
.and_then(|s| s.get("residentKey"))
.and_then(Value::as_str)
.is_some_and(|r| r == "required")
|| selection
.and_then(|s| s.get("requireResidentKey"))
.and_then(Value::as_bool)
.unwrap_or(false);
let conveyance = match text(&options, &["attestation"]) {
Some("direct") => WEBAUTHN_ATTESTATION_CONVEYANCE_PREFERENCE_DIRECT,
Some("none") => WEBAUTHN_ATTESTATION_CONVEYANCE_PREFERENCE_NONE,
_ => WEBAUTHN_ATTESTATION_CONVEYANCE_PREFERENCE_ANY,
};
let rp = WEBAUTHN_RP_ENTITY_INFORMATION {
dwVersion: WEBAUTHN_RP_ENTITY_INFORMATION_CURRENT_VERSION,
pwszId: PCWSTR(rp_id_w.as_ptr()),
pwszName: PCWSTR(rp_name.as_ptr()),
pwszIcon: PCWSTR::null(),
};
let user = WEBAUTHN_USER_ENTITY_INFORMATION {
dwVersion: WEBAUTHN_USER_ENTITY_INFORMATION_CURRENT_VERSION,
cbId: user_id.len() as u32,
pbId: user_id.as_mut_ptr(),
pwszName: PCWSTR(user_name.as_ptr()),
pwszIcon: PCWSTR::null(),
pwszDisplayName: PCWSTR(user_display.as_ptr()),
};
let cose = WEBAUTHN_COSE_CREDENTIAL_PARAMETERS {
cCredentialParameters: params.len() as u32,
pCredentialParameters: params.as_mut_ptr(),
};
let client_data = WEBAUTHN_CLIENT_DATA {
dwVersion: WEBAUTHN_CLIENT_DATA_CURRENT_VERSION,
cbClientDataJSON: client.len() as u32,
pbClientDataJSON: client.as_mut_ptr(),
pwszHashAlgId: WEBAUTHN_HASH_ALGORITHM_SHA_256,
};
let make_options = WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS {
dwVersion: WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS_VERSION_1,
dwTimeoutMilliseconds: timeout(&options),
CredentialList: WEBAUTHN_CREDENTIALS {
cCredentials: exclude.len() as u32,
pCredentials: exclude.as_mut_ptr(),
},
dwAuthenticatorAttachment: attachment(
selection
.and_then(|s| s.get("authenticatorAttachment"))
.and_then(Value::as_str),
),
bRequireResidentKey: resident.into(),
dwUserVerificationRequirement: user_verification(
selection
.and_then(|s| s.get("userVerification"))
.and_then(Value::as_str),
),
dwAttestationConveyancePreference: conveyance,
..Default::default()
};
let attestation = unsafe {
WebAuthNAuthenticatorMakeCredential(
HWND(window.0 as *mut core::ffi::c_void),
&raw const rp,
&raw const user,
&raw const cose,
&raw const client_data,
Some(&raw const make_options),
)
}
.map_err(|e| platform_error(&e))?;
let (credential_id, attestation_object) = unsafe {
let a = &*attestation;
(
copy(a.pbCredentialId, a.cbCredentialId),
copy(a.pbAttestationObject, a.cbAttestationObject),
)
};
unsafe { WebAuthNFreeCredentialAttestation(Some(attestation)) };
let id = base64url(&credential_id);
Ok(json!({
"id": id,
"rawId": id,
"type": "public-key",
"authenticatorAttachment": "platform",
"clientExtensionResults": {},
"response": {
"clientDataJSON": base64url(&client),
"attestationObject": base64url(&attestation_object),
"transports": ["internal"],
}
}))
}
fn get_assertion(window: Window, options_json: &str) -> Result<Value, AuthError> {
let options = parse(options_json)?;
let rp_id = text(&options, &["rpId"]).unwrap_or("").to_string();
if rp_id.is_empty() {
return Err(AuthError::InvalidRequest(
"the request options need rpId".into(),
));
}
let rp_id_w = HSTRING::from(rp_id.as_str());
let mut client = client_data("webauthn.get", &options, &rp_id)?;
let (_allow_ids, mut allow) =
credential_list(options.get("allowCredentials").and_then(Value::as_array));
let client_data = WEBAUTHN_CLIENT_DATA {
dwVersion: WEBAUTHN_CLIENT_DATA_CURRENT_VERSION,
cbClientDataJSON: client.len() as u32,
pbClientDataJSON: client.as_mut_ptr(),
pwszHashAlgId: WEBAUTHN_HASH_ALGORITHM_SHA_256,
};
let get_options = WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS {
dwVersion: WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS_VERSION_1,
dwTimeoutMilliseconds: timeout(&options),
CredentialList: WEBAUTHN_CREDENTIALS {
cCredentials: allow.len() as u32,
pCredentials: allow.as_mut_ptr(),
},
dwAuthenticatorAttachment: WEBAUTHN_AUTHENTICATOR_ATTACHMENT_ANY,
dwUserVerificationRequirement: user_verification(text(&options, &["userVerification"])),
..Default::default()
};
let assertion = unsafe {
WebAuthNAuthenticatorGetAssertion(
HWND(window.0 as *mut core::ffi::c_void),
PCWSTR(rp_id_w.as_ptr()),
&raw const client_data,
Some(&raw const get_options),
)
}
.map_err(|e| platform_error(&e))?;
let (credential_id, authenticator_data, signature, user_handle) = unsafe {
let a = &*assertion;
(
copy(a.Credential.pbId, a.Credential.cbId),
copy(a.pbAuthenticatorData, a.cbAuthenticatorData),
copy(a.pbSignature, a.cbSignature),
copy(a.pbUserId, a.cbUserId),
)
};
unsafe { WebAuthNFreeAssertion(assertion) };
let id = base64url(&credential_id);
Ok(json!({
"id": id,
"rawId": id,
"type": "public-key",
"authenticatorAttachment": "platform",
"clientExtensionResults": {},
"response": {
"clientDataJSON": base64url(&client),
"authenticatorData": base64url(&authenticator_data),
"signature": base64url(&signature),
"userHandle": if user_handle.is_empty() { Value::Null } else { Value::String(base64url(&user_handle)) },
}
}))
}