-
Notifications
You must be signed in to change notification settings - Fork 5
Support risk intelligence #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rokostik
wants to merge
16
commits into
main
Choose a base branch
from
rok/support-risk-intelligence
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8991a9a
add risk intelligence on siteverify
rokostik 8ce377e
add risk intelligence /retrieve
rokostik a8e0149
bump required python version
rokostik 391e8e8
fix actions
rokostik dd8a434
use importlib.metadata instead of deprecated pkg_resources
rokostik ed4f707
bump version
rokostik e5d64ba
format
rokostik 3e5a438
fix tests
rokostik 51798e5
support new response format
rokostik d85eb4a
support optional sitekey
rokostik a821119
save raw risk intelligence response
rokostik d7556e0
always use latest version in examples and add comment
rokostik e878e50
Bump pydantic version
greenberga 665b67c
Fix deprecation warnings from pydantic bump
greenberga a719528
Ensure response.success==False if there is an error
greenberga 708c65a
require >= for requests
rokostik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,8 @@ | ||
| fastapi==0.101.1 | ||
| uvicorn==0.26.0 | ||
| jinja2==3.1.3 | ||
| python-multipart==0.0.6 | ||
|
|
||
| # To use the example outside of this repository, replace the line below with the latest | ||
| # `friendly-captcha-client==x.y.z` version. | ||
| -e ../.. | ||
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,37 @@ | ||
| # Friendly Captcha Python Risk Intelligence Example | ||
|
|
||
| This example demonstrates server-side risk intelligence retrieval with `retrieve_risk_intelligence`. | ||
|
|
||
| ### Requirements | ||
|
|
||
| - Python 3.10+ | ||
| - Your Friendly Captcha API key and sitekey. | ||
|
|
||
| ### Start the application | ||
|
|
||
| - Set up a virtual environment (recommended): | ||
|
|
||
| ```bash | ||
| python -m venv venv | ||
| source venv/bin/activate # On Windows, use `venv\Scripts\activate` | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| - Set environment variables and start the application | ||
|
|
||
| > NOTE: `FRC_API_ENDPOINT` and `FRC_AGENT_ENDPOINT` are optional. If not set, default values are used. You can also use `global` or `eu` as shorthands. | ||
|
|
||
| ```bash | ||
| FRC_APIKEY=<your API key> \ | ||
| FRC_SITEKEY=<your sitekey> \ | ||
| FRC_API_ENDPOINT=<api endpoint> \ | ||
| FRC_AGENT_ENDPOINT=<agent endpoint> \ | ||
| uvicorn main:app --reload --port 8000 | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Navigate to http://localhost:8000/ in your browser. | ||
| The token generation starts automatically. Submit the form to retrieve the risk intelligence data server-side. | ||
| Tokens are cached in the browser for the duration of their validity period so refreshing the page does not regenerate the token. | ||
| You can regenerate the token by clicking the "Regenerate Token" button. |
This file contains hidden or 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,111 @@ | ||
| import json | ||
| import os | ||
|
|
||
| from fastapi import FastAPI, Form, Request | ||
| from fastapi.templating import Jinja2Templates | ||
|
|
||
| from friendly_captcha_client.client import ( | ||
| FriendlyCaptchaClient, | ||
| RiskIntelligenceRetrieveResult, | ||
| ) | ||
|
|
||
| app = FastAPI() | ||
| templates = Jinja2Templates(directory="./templates/") | ||
|
|
||
| FRC_SITEKEY = os.getenv("FRC_SITEKEY") | ||
| FRC_APIKEY = os.getenv("FRC_APIKEY") | ||
|
|
||
| # Optional: "global", "eu", or a full API base endpoint like "https://eu.frcapi.com". | ||
| FRC_API_ENDPOINT = os.getenv("FRC_API_ENDPOINT") | ||
| # Optional: SDK/agent endpoint used in the browser widget. | ||
| FRC_AGENT_ENDPOINT = os.getenv("FRC_AGENT_ENDPOINT") | ||
|
|
||
| if not FRC_SITEKEY or not FRC_APIKEY: | ||
| print( | ||
| "Please set FRC_SITEKEY and FRC_APIKEY before running this example to your Friendly Captcha sitekey and API key respectively." | ||
| ) | ||
| exit(1) | ||
|
|
||
| frc_client = FriendlyCaptchaClient( | ||
| api_key=FRC_APIKEY, | ||
| sitekey=FRC_SITEKEY, | ||
| api_endpoint=FRC_API_ENDPOINT, | ||
| strict=False, | ||
| ) | ||
|
|
||
|
|
||
| def _render_template(request: Request, **values): | ||
| return templates.TemplateResponse("demo.html", {"request": request, **values}) | ||
|
|
||
|
|
||
| def _base_template_data(): | ||
| return { | ||
| "message": "", | ||
| "sitekey": FRC_SITEKEY, | ||
| "agent_endpoint": FRC_AGENT_ENDPOINT or "", | ||
| "risk_token": "", | ||
| "token_timestamp": "", | ||
| "token_expires_at": "", | ||
| "token_num_uses": "", | ||
| "risk_intelligence_raw": "", | ||
| } | ||
|
|
||
|
|
||
| @app.get("/") | ||
| def read_root(request: Request): | ||
| return _render_template(request, **_base_template_data()) | ||
|
|
||
|
|
||
| @app.post("/") | ||
| def post_form( | ||
| request: Request, | ||
| frc_risk_intelligence_token: str = Form("", alias="frc-risk-intelligence-token"), | ||
| ): | ||
| data = _base_template_data() | ||
|
|
||
| risk_token = (frc_risk_intelligence_token or "").strip() | ||
| if not risk_token: | ||
| data["message"] = "No risk intelligence token found." | ||
| return _render_template(request, **data) | ||
|
|
||
| data["risk_token"] = risk_token | ||
|
|
||
| result: RiskIntelligenceRetrieveResult = frc_client.retrieve_risk_intelligence( | ||
| risk_token | ||
| ) | ||
| if not result.was_able_to_retrieve: | ||
| data["message"] = "Risk intelligence retrieval failed: {}".format(result.error) | ||
| return _render_template(request, **data) | ||
|
|
||
| if not result.is_valid: | ||
| data["message"] = "Risk intelligence token is invalid: {}".format(result.error) | ||
| return _render_template(request, **data) | ||
|
|
||
| # This should never happen since /retrieve should always return | ||
| # risk intelligence data if the token is valid and request is successful. | ||
| # But it's good practice to handle it just in case. | ||
| if result.data is None: | ||
| data["message"] = ( | ||
| "Risk intelligence retrieve succeeded, but no data was returned." | ||
| ) | ||
| return _render_template(request, **data) | ||
| if result.data.risk_intelligence is None: | ||
| data["message"] = ( | ||
| "Token was valid, but risk intelligence data was not returned." | ||
| ) | ||
| data["token_timestamp"] = result.data.token.timestamp | ||
| data["token_expires_at"] = result.data.token.expires_at | ||
| data["token_num_uses"] = result.data.token.num_uses | ||
| data["token_origin"] = result.data.token.origin | ||
| return _render_template(request, **data) | ||
|
|
||
| data["message"] = "Retrieved risk intelligence data successfully." | ||
| data["token_timestamp"] = result.data.token.timestamp | ||
| data["token_expires_at"] = result.data.token.expires_at | ||
| data["token_num_uses"] = result.data.token.num_uses | ||
| data["token_origin"] = result.data.token.origin | ||
| data["risk_intelligence_raw"] = json.dumps( | ||
| result.data.risk_intelligence.model_dump(by_alias=True), | ||
| indent=2, | ||
| ) | ||
| return _render_template(request, **data) |
This file contains hidden or 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,8 @@ | ||
| fastapi==0.101.1 | ||
| uvicorn==0.26.0 | ||
| jinja2==3.1.3 | ||
| python-multipart==0.0.6 | ||
|
|
||
| # To use the example outside of this repository, replace the line below with the latest | ||
| # `friendly-captcha-client==x.y.z` version. | ||
| -e ../.. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this still necessary? Or is it referencing this SDK (the Python SDK)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's a reference to the SDK, because we have it fixed to a release and this hasn't been released yet. We could actually just leave it as
-e ../..to always have an up to date SDK in the example without us needing to bump the version but that means that it's not copy-pastable outside of the project for anyone wanting to implement it in their project.What do you think? We could just add a comment explaining that they need to add
friendly-captcha-client==x.y.z.and remove the-e ../..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eh, I think it's fine to leave it as
-e ../..and have a comment that says if you use this outside of this project, you should replace this line withfriendly-captcha-client==x.y.z