FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/src/pythonnative/native_modules/biometrics.py at main · andevsr/pythonnative · GitHub
andevsr
pythonnative
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
biometrics.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
157 lines (115 loc) · 4.88 KB
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
biometrics.py
Copy path
File metadata and controls
157 lines (115 loc) · 4.88 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""Biometric authentication (Face ID / Touch ID / fingerprint).
[`Biometrics`][pythonnative.Biometrics] gates an action behind the
device's biometric hardware via ``LAContext`` (iOS) and
``BiometricPrompt`` (Android).
``is_available`` is synchronous; ``authenticate`` is a coroutine that
presents the system prompt and resolves to ``True`` on success or
``False`` on failure / cancellation.
Example:
```python
import pythonnative as pn
async def unlock():
if await pn.Biometrics.authenticate("Unlock your vault"):
show_secrets()
```
"""
from
__future__
import
annotations
import
asyncio
from
typing
import
Any
,
Callable
,
Dict
from
..
import
diagnostics
from
..
runtime
import
resolve_future
from
..
utils
import
IS_ANDROID
,
IS_IOS
_pending
:
Dict
[
int
,
Any
]
=
{}
class
Biometrics
:
"""Biometric authentication interface."""
@
staticmethod
def
is_available
()
->
bool
:
"""Return ``True`` when biometric auth can be attempted."""
if
IS_IOS
:
return
_ios_available
()
if
IS_ANDROID
:
return
_android_available
()
return
False
@
staticmethod
async
def
authenticate
(
reason
:
str
=
"Authenticate"
)
->
bool
:
"""Present the biometric prompt; resolve ``True`` on success."""
loop
=
asyncio
.
get_running_loop
()
future
:
asyncio
.
Future
[
bool
]
=
loop
.
create_future
()
def
_done
(
ok
:
bool
)
->
None
:
resolve_future
(
future
,
ok
)
if
IS_IOS
:
_ios_authenticate
(
reason
,
_done
)
elif
IS_ANDROID
:
_android_authenticate
(
reason
,
_done
)
else
:
_done
(
False
)
return
await
future
# ======================================================================
# iOS: LAContext
# ======================================================================
def
_ios_context
()
->
Any
:
from
rubicon
.
objc
import
ObjCClass
return
ObjCClass
(
"LAContext"
).
alloc
().
init
()
def
_ios_available
()
->
bool
:
try
:
# LAPolicyDeviceOwnerAuthenticationWithBiometrics == 1
return
bool
(
_ios_context
().
canEvaluatePolicy_error_
(
1
,
None
))
except
Exception
:
return
False
def
_ios_authenticate
(
reason
:
str
,
on_done
:
Callable
[[
bool
],
None
])
->
None
:
try
:
from
rubicon
.
objc
import
Block
,
ObjCClass
context
=
_ios_context
()
token
=
id
(
context
)
_pending
[
token
]
=
context
def
_reply
(
success
:
bool
,
error
:
Any
)
->
None
:
del
error
_pending
.
pop
(
token
,
None
)
try
:
on_done
(
bool
(
success
))
except
Exception
:
diagnostics
.
swallowed
(
"biometrics._ios_authenticate._reply"
)
context
.
evaluatePolicy_localizedReason_reply_
(
1
,
reason
,
Block
(
_reply
,
None
,
bool
,
ObjCClass
(
"NSError"
)))
except
Exception
:
on_done
(
False
)
# ======================================================================
# Android: BiometricPrompt
# ======================================================================
def
_android_available
()
->
bool
:
try
:
from
java
import
jclass
from
..
utils
import
get_android_context
BiometricManager
=
jclass
(
"androidx.biometric.BiometricManager"
)
manager
=
BiometricManager
.
from_
(
get_android_context
())
Authenticators
=
jclass
(
"androidx.biometric.BiometricManager$Authenticators"
)
result
=
manager
.
canAuthenticate
(
Authenticators
.
BIOMETRIC_WEAK
)
return
result
==
BiometricManager
.
BIOMETRIC_SUCCESS
except
Exception
:
return
False
def
_android_authenticate
(
reason
:
str
,
on_done
:
Callable
[[
bool
],
None
])
->
None
:
try
:
from
java
import
dynamic_proxy
,
jclass
from
..
utils
import
get_android_context
ctx
=
get_android_context
()
FragmentActivity
=
jclass
(
"androidx.fragment.app.FragmentActivity"
)
if
not
FragmentActivity
.
isInstance
(
ctx
):
on_done
(
False
)
return
BiometricPrompt
=
jclass
(
"androidx.biometric.BiometricPrompt"
)
ContextCompat
=
jclass
(
"androidx.core.content.ContextCompat"
)
executor
=
ContextCompat
.
getMainExecutor
(
ctx
)
class
_Callback
(
dynamic_proxy
(
BiometricPrompt
.
AuthenticationCallback
)):
# type: ignore[misc]
def
onAuthenticationSucceeded
(
self
,
result
:
Any
)
->
None
:
# noqa: N802, ARG002
on_done
(
True
)
def
onAuthenticationError
(
self
,
code
:
int
,
message
:
Any
)
->
None
:
# noqa: N802, ARG002
on_done
(
False
)
def
onAuthenticationFailed
(
self
)
->
None
:
# noqa: N802
# Fired on a non-matching attempt; the prompt stays up,
# so don't resolve yet.
pass
prompt
=
BiometricPrompt
(
ctx
,
executor
,
_Callback
())
Builder
=
jclass
(
"androidx.biometric.BiometricPrompt$PromptInfo$Builder"
)
info
=
Builder
().
setTitle
(
reason
).
setNegativeButtonText
(
"Cancel"
).
build
()
prompt
.
authenticate
(
info
)
except
Exception
:
on_done
(
False
)
Back
|
FazBrowse Home
|
New Git URL