reflex-google-recaptcha-v2 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,75 @@
1
+ Metadata-Version: 2.1
2
+ Name: reflex-google-recaptcha-v2
3
+ Version: 0.0.1
4
+ Summary: Reflex custom component google-recaptcha-v2
5
+ Author-email: Masen Furer <m_github@0x26.net>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/masenf/reflex-google-recaptcha-v2
8
+ Keywords: reflex,reflex-custom-components
9
+ Classifier: Development Status :: 4 - Beta
10
+ Requires-Python: >=3.8
11
+ Description-Content-Type: text/markdown
12
+ Requires-Dist: reflex>=0.4.2
13
+ Provides-Extra: dev
14
+ Requires-Dist: build; extra == "dev"
15
+ Requires-Dist: twine; extra == "dev"
16
+
17
+ # google-recaptcha-v2
18
+
19
+ A Reflex custom component google-recaptcha-v2.
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ pip install reflex-google-recaptcha-v2
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Set your site key and secret key in your environment variables.
30
+
31
+ ```bash
32
+ export RECAPTCHA_SITE_KEY="your-site-key"
33
+ export RECAPTCHA_SECRET_KEY="your-secret-key"
34
+ ```
35
+
36
+ Alternatively, you can set the keys via python functions as seen in the demo app.
37
+
38
+ ### Place the Recaptcha component
39
+
40
+ ```python
41
+ import reflex as rx
42
+
43
+ from reflex_google_recaptcha_v2 import google_recaptcha_v2
44
+
45
+ def index():
46
+ return rx.vstack(
47
+ ...,
48
+ google_recaptcha_v2(),
49
+ )
50
+ ```
51
+
52
+ ### Verify the Recaptcha response
53
+
54
+ Before taking actions on the backend, you should verify that the user
55
+ browser has passed validation of the token.
56
+
57
+ This value will be set for the specific tab that has completed validation and
58
+ persists for the lifetime of the Reflex session (until the tab is closed).
59
+
60
+ ```python
61
+ import reflex as rx
62
+
63
+ from reflex_google_recaptcha_v2 import GoogleRecaptchaV2State
64
+
65
+
66
+ class MyState(rx.State):
67
+ form_error: str
68
+
69
+ async def handle_submit(self, form_data):
70
+ ...
71
+ recaptcha_state = await self.get_state(GoogleRecaptchaV2State)
72
+ if not recaptcha_state.token_is_valid:
73
+ self.form_error = "Invalid recaptcha. Are you a robot?"
74
+ return
75
+ ```
@@ -0,0 +1,59 @@
1
+ # google-recaptcha-v2
2
+
3
+ A Reflex custom component google-recaptcha-v2.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install reflex-google-recaptcha-v2
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Set your site key and secret key in your environment variables.
14
+
15
+ ```bash
16
+ export RECAPTCHA_SITE_KEY="your-site-key"
17
+ export RECAPTCHA_SECRET_KEY="your-secret-key"
18
+ ```
19
+
20
+ Alternatively, you can set the keys via python functions as seen in the demo app.
21
+
22
+ ### Place the Recaptcha component
23
+
24
+ ```python
25
+ import reflex as rx
26
+
27
+ from reflex_google_recaptcha_v2 import google_recaptcha_v2
28
+
29
+ def index():
30
+ return rx.vstack(
31
+ ...,
32
+ google_recaptcha_v2(),
33
+ )
34
+ ```
35
+
36
+ ### Verify the Recaptcha response
37
+
38
+ Before taking actions on the backend, you should verify that the user
39
+ browser has passed validation of the token.
40
+
41
+ This value will be set for the specific tab that has completed validation and
42
+ persists for the lifetime of the Reflex session (until the tab is closed).
43
+
44
+ ```python
45
+ import reflex as rx
46
+
47
+ from reflex_google_recaptcha_v2 import GoogleRecaptchaV2State
48
+
49
+
50
+ class MyState(rx.State):
51
+ form_error: str
52
+
53
+ async def handle_submit(self, form_data):
54
+ ...
55
+ recaptcha_state = await self.get_state(GoogleRecaptchaV2State)
56
+ if not recaptcha_state.token_is_valid:
57
+ self.form_error = "Invalid recaptcha. Are you a robot?"
58
+ return
59
+ ```
@@ -0,0 +1 @@
1
+ from .google_recaptcha_v2 import *
@@ -0,0 +1,164 @@
1
+ """Reflex custom component GoogleRecaptchaV2."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ from typing import Any, Dict
7
+
8
+ import httpx
9
+
10
+ import reflex as rx
11
+
12
+
13
+ VERIFY_ENDPOINT = "https://www.google.com/recaptcha/api/siteverify"
14
+ SITE_KEY = os.environ.get("RECAPTCHA_SITE_KEY")
15
+ SECRET_KEY = os.environ.get("RECAPTCHA_SECRET_KEY")
16
+
17
+
18
+ def set_site_key(site_key: str):
19
+ """Set the site key."""
20
+ global SITE_KEY
21
+ SITE_KEY = site_key
22
+
23
+
24
+ def set_secret_key(secret_key: str):
25
+ """Set the secret key."""
26
+ global SECRET_KEY
27
+ SECRET_KEY = secret_key
28
+
29
+
30
+ class GoogleRecaptchaV2State(rx.State):
31
+ recaptcha_verify_response_json: str = rx.LocalStorage(sync=True)
32
+ _is_valid: bool = False
33
+
34
+ def verify_captcha(self, token: str):
35
+ """Validate the captcha token."""
36
+ payload = {
37
+ "secret": SECRET_KEY,
38
+ "response": token,
39
+ "remoteip": getattr(
40
+ self.router.headers, "x_forwarded_for", self.router.session.client_ip
41
+ ),
42
+ }
43
+ resp = httpx.post(VERIFY_ENDPOINT, data=payload)
44
+ resp.raise_for_status()
45
+ try:
46
+ self._is_valid = resp.json().get("success", False)
47
+ except ValueError:
48
+ pass
49
+
50
+ @rx.cached_var
51
+ def token_is_valid(self) -> bool:
52
+ """Check if the token is valid."""
53
+ return self._is_valid
54
+
55
+
56
+ class GoogleRecaptchaV2(rx.NoSSRComponent):
57
+ """GoogleRecaptchaV2 component.
58
+
59
+ Event Triggers:
60
+ - async_script_on_load: optional callback when the google recaptcha script has been loaded
61
+ - on_change: The function to be called when the user successfully completes the captcha
62
+ - on_errored: optional callback when the challenge errored, most likely due to network issues.
63
+ - on_expired: optional callback when the challenge is expired and has to be redone by user. By default it will call the onChange with null to signify expired callback.
64
+ """
65
+
66
+ # The React library to wrap.
67
+ library = "react-google-recaptcha"
68
+
69
+ # The React component tag.
70
+ tag = "ReCAPTCHA"
71
+
72
+ is_default = True
73
+
74
+ # Positions ReCAPTCHA badge. Only for invisible ReCAPTCHA. bottomright, bottomleft or inline.
75
+ badge: rx.Var[str]
76
+
77
+ # Set the hl parameter, which allows the captcha to be used from different languages, see reCAPTCHA hl
78
+ hl: rx.Var[str]
79
+
80
+ # For plugin owners to not interfere with existing reCAPTCHA installations on a page. If true, this reCAPTCHA instance will be part of a separate ID space. (default: false)
81
+ isolated: rx.Var[bool]
82
+
83
+ # The API client key (required).
84
+ sitekey: rx.Var[str]
85
+
86
+ # The size of the widget - compact, normal, or invisible (default: normal).
87
+ size: rx.Var[str]
88
+
89
+ # Set the stoken parameter, which allows the captcha to be used from different domains, see reCAPTCHA secure-token
90
+ stoken: rx.Var[str]
91
+
92
+ # The tabindex on the element (default: 0).
93
+ tabindex: rx.Var[int]
94
+
95
+ # The type of initial captcha - image or audio (defaults: image).
96
+ type: rx.Var[str]
97
+
98
+ # The theme of the widget - light or dark (defaults: light).
99
+ theme: rx.Var[str]
100
+
101
+ @classmethod
102
+ def create(cls, **props) -> "GoogleRecaptchaV2":
103
+ if props.get("size") == "invisible":
104
+ props.setdefault("id", rx.vars.get_unique_variable_name())
105
+ raise NotImplementedError("Invisible mode is not currently working.")
106
+ props.setdefault("sitekey", SITE_KEY)
107
+ props.setdefault("on_change", GoogleRecaptchaV2State.verify_captcha)
108
+ return super().create(**props)
109
+
110
+ def get_event_triggers(self) -> Dict[str, Any]:
111
+ return {
112
+ **super().get_event_triggers(),
113
+ "async_script_on_load": lambda e0: [e0],
114
+ "on_change": lambda e0: [e0],
115
+ "on_errored": lambda e0: [e0],
116
+ "on_expired": lambda e0: [e0],
117
+ }
118
+
119
+ def api(self) -> "GoogleRecaptchaV2API" | None:
120
+ raise NotImplementedError("Invisible mode is not currently working.")
121
+ ref = self.get_ref()
122
+ if ref:
123
+ return GoogleRecaptchaV2API(ref_name=self.get_ref())
124
+ raise ValueError("Be sure to set an id on the component to use the API.")
125
+
126
+
127
+ google_recaptcha_v2 = GoogleRecaptchaV2.create
128
+
129
+
130
+ class GoogleRecaptchaV2API(rx.Base):
131
+ """The API for triggering execute() in invisible mode.
132
+
133
+ Ref API:
134
+ getValue() returns the value of the captcha field
135
+ getWidgetId() returns the recaptcha widget Id
136
+ reset() forces reset. See the JavaScript API doc
137
+ execute() programmatically invoke the challenge
138
+ need to call when using "invisible" reCAPTCHA - example below
139
+
140
+ (TODO: Not currently working with Reflex.)
141
+ """
142
+
143
+ ref_name: str
144
+
145
+ def _get_api_spec(self, fn_name) -> rx.Var[rx.EventChain]:
146
+ return rx.Var.create(
147
+ f"{rx.Var.create(self.ref_name).as_ref()}?.current?.{fn_name}",
148
+ _var_is_local=False,
149
+ _var_is_string=False,
150
+ )._replace(
151
+ _var_type=rx.EventChain,
152
+ )
153
+
154
+ def get_value(self) -> rx.Var[rx.EventChain]:
155
+ return self._get_api_spec("get_value")
156
+
157
+ def get_widget_id(self) -> rx.Var[rx.EventChain]:
158
+ return self._get_api_spec("get_widget_id")
159
+
160
+ def reset(self) -> rx.Var[rx.EventChain]:
161
+ return self._get_api_spec("reset")
162
+
163
+ def execute(self) -> rx.Var[rx.EventChain]:
164
+ return self._get_api_spec("execute")
@@ -0,0 +1,75 @@
1
+ Metadata-Version: 2.1
2
+ Name: reflex-google-recaptcha-v2
3
+ Version: 0.0.1
4
+ Summary: Reflex custom component google-recaptcha-v2
5
+ Author-email: Masen Furer <m_github@0x26.net>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/masenf/reflex-google-recaptcha-v2
8
+ Keywords: reflex,reflex-custom-components
9
+ Classifier: Development Status :: 4 - Beta
10
+ Requires-Python: >=3.8
11
+ Description-Content-Type: text/markdown
12
+ Requires-Dist: reflex>=0.4.2
13
+ Provides-Extra: dev
14
+ Requires-Dist: build; extra == "dev"
15
+ Requires-Dist: twine; extra == "dev"
16
+
17
+ # google-recaptcha-v2
18
+
19
+ A Reflex custom component google-recaptcha-v2.
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ pip install reflex-google-recaptcha-v2
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Set your site key and secret key in your environment variables.
30
+
31
+ ```bash
32
+ export RECAPTCHA_SITE_KEY="your-site-key"
33
+ export RECAPTCHA_SECRET_KEY="your-secret-key"
34
+ ```
35
+
36
+ Alternatively, you can set the keys via python functions as seen in the demo app.
37
+
38
+ ### Place the Recaptcha component
39
+
40
+ ```python
41
+ import reflex as rx
42
+
43
+ from reflex_google_recaptcha_v2 import google_recaptcha_v2
44
+
45
+ def index():
46
+ return rx.vstack(
47
+ ...,
48
+ google_recaptcha_v2(),
49
+ )
50
+ ```
51
+
52
+ ### Verify the Recaptcha response
53
+
54
+ Before taking actions on the backend, you should verify that the user
55
+ browser has passed validation of the token.
56
+
57
+ This value will be set for the specific tab that has completed validation and
58
+ persists for the lifetime of the Reflex session (until the tab is closed).
59
+
60
+ ```python
61
+ import reflex as rx
62
+
63
+ from reflex_google_recaptcha_v2 import GoogleRecaptchaV2State
64
+
65
+
66
+ class MyState(rx.State):
67
+ form_error: str
68
+
69
+ async def handle_submit(self, form_data):
70
+ ...
71
+ recaptcha_state = await self.get_state(GoogleRecaptchaV2State)
72
+ if not recaptcha_state.token_is_valid:
73
+ self.form_error = "Invalid recaptcha. Are you a robot?"
74
+ return
75
+ ```
@@ -0,0 +1,9 @@
1
+ README.md
2
+ pyproject.toml
3
+ custom_components/reflex_google_recaptcha_v2/__init__.py
4
+ custom_components/reflex_google_recaptcha_v2/google_recaptcha_v2.py
5
+ custom_components/reflex_google_recaptcha_v2.egg-info/PKG-INFO
6
+ custom_components/reflex_google_recaptcha_v2.egg-info/SOURCES.txt
7
+ custom_components/reflex_google_recaptcha_v2.egg-info/dependency_links.txt
8
+ custom_components/reflex_google_recaptcha_v2.egg-info/requires.txt
9
+ custom_components/reflex_google_recaptcha_v2.egg-info/top_level.txt
@@ -0,0 +1,35 @@
1
+ [build-system]
2
+ requires = [
3
+ "setuptools",
4
+ "wheel",
5
+ ]
6
+ build-backend = "setuptools.build_meta"
7
+
8
+ [project]
9
+ name = "reflex-google-recaptcha-v2"
10
+ version = "0.0.1"
11
+ description = "Reflex custom component google-recaptcha-v2"
12
+ readme = "README.md"
13
+ license = { text = "Apache-2.0" }
14
+ requires-python = ">=3.8"
15
+ authors = [{ name = "Masen Furer", email = "m_github@0x26.net" }]
16
+ keywords = [
17
+ "reflex",
18
+ "reflex-custom-components"]
19
+
20
+ dependencies = [
21
+ "reflex>=0.4.2"
22
+ ]
23
+
24
+ classifiers = [
25
+ "Development Status :: 4 - Beta",
26
+ ]
27
+
28
+ [project.urls]
29
+ Homepage = "https://github.com/masenf/reflex-google-recaptcha-v2"
30
+
31
+ [project.optional-dependencies]
32
+ dev = ["build", "twine"]
33
+
34
+ [tool.setuptools.packages.find]
35
+ where = ["custom_components"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+