Skip to content

Commit 5f438de

Browse files
authored
feat(auth): use stable key for transient auth to avoid drift (#1002)
1 parent b406e05 commit 5f438de

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

python/cocoindex/auth_registry.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,6 @@
1111

1212
T = TypeVar("T")
1313

14-
# Global atomic counter for generating unique auth entry keys
15-
_counter_lock = threading.Lock()
16-
_auth_key_counter = 0
17-
18-
19-
def _generate_auth_key() -> str:
20-
"""Generate a unique auth entry key using a global atomic counter."""
21-
global _auth_key_counter # pylint: disable=global-statement
22-
with _counter_lock:
23-
_auth_key_counter += 1
24-
return f"__auth_{_auth_key_counter}"
25-
2614

2715
@dataclass
2816
class TransientAuthEntryReference(Generic[T]):
@@ -37,7 +25,8 @@ class AuthEntryReference(TransientAuthEntryReference[T]):
3725

3826
def add_transient_auth_entry(value: T) -> TransientAuthEntryReference[T]:
3927
"""Add an auth entry to the registry. Returns its reference."""
40-
return add_auth_entry(_generate_auth_key(), value)
28+
key = _engine.add_transient_auth_entry(dump_engine_object(value))
29+
return TransientAuthEntryReference(key)
4130

4231

4332
def add_auth_entry(key: str, value: T) -> AuthEntryReference[T]:

src/py/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,13 @@ fn add_auth_entry(key: String, value: Pythonized<serde_json::Value>) -> PyResult
559559
Ok(())
560560
}
561561

562+
#[pyfunction]
563+
fn add_transient_auth_entry(value: Pythonized<serde_json::Value>) -> PyResult<String> {
564+
get_auth_registry()
565+
.add_transient(value.into_inner())
566+
.into_py_result()
567+
}
568+
562569
#[pyfunction]
563570
fn get_app_namespace(py: Python<'_>) -> PyResult<String> {
564571
let app_namespace = py
@@ -599,6 +606,7 @@ fn cocoindex_engine(m: &Bound<'_, PyModule>) -> PyResult<()> {
599606
m.add_function(wrap_pyfunction!(make_drop_bundle, m)?)?;
600607
m.add_function(wrap_pyfunction!(remove_flow_context, m)?)?;
601608
m.add_function(wrap_pyfunction!(add_auth_entry, m)?)?;
609+
m.add_function(wrap_pyfunction!(add_transient_auth_entry, m)?)?;
602610
m.add_function(wrap_pyfunction!(get_app_namespace, m)?)?;
603611

604612
m.add_class::<builder::flow_builder::FlowBuilder>()?;

src/setup/auth_registry.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ impl AuthRegistry {
3232
Ok(())
3333
}
3434

35+
pub fn add_transient(&self, value: serde_json::Value) -> Result<String> {
36+
let key = format!(
37+
"__transient_{}",
38+
utils::fingerprint::Fingerprinter::default()
39+
.with("cocoindex_auth")? // salt
40+
.with(&value)?
41+
.into_fingerprint()
42+
.to_base64()
43+
);
44+
self.entries
45+
.write()
46+
.unwrap()
47+
.entry(key.clone())
48+
.or_insert(value);
49+
Ok(key)
50+
}
51+
3552
pub fn get<T: DeserializeOwned>(&self, entry_ref: &spec::AuthEntryReference<T>) -> Result<T> {
3653
let entries = self.entries.read().unwrap();
3754
match entries.get(&entry_ref.key) {

0 commit comments

Comments
 (0)