- Notifications
You must be signed in to change notification settings - Fork0
An Appoov token integration example for Python 3 with Flask. Article:https://blog.approov.io/approov-integration-in-a-python-flask-api
approov/quickstart-python-flask-token-check
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Approov is an API security solution used to verify that requests received by your backend services originate from trusted versions of your mobile apps.
This repo implements the Approov server-side request verification code with the Python Flask framework in a simple Hello API server, which performs the verification check before allowing valid traffic to be processed by the API endpoint.
Originally this repo was just to show the Approov token integration example on a Python 3 Flask API as described in the article:Approov Integration in a Python Flask API, that you can still find at/servers/shapes-api.
The quickstart was tested with the following Operating Systems:
- Ubuntu 20.04
- MacOS Big Sur
- Windows 10 WSL2 - Ubuntu 20.04
First, setup theApproov CLI.
Now, register the API domain for which Approov will issues tokens:
approov api -add api.example.com
NOTE: By default a symmetric key (HS256) is used to sign the Approov token on a valid attestation of the mobile app for each API domain it's added with the Approov CLI, so that all APIs will share the same secret and the backend needs to take care to keep this secret secure.
A more secure alternative is to use asymmetric keys (RS256 or others) that allows for a different keyset to be used on each API domain and for the Approov token to be verified with a public key that can only verify, but not sign, Approov tokens.
To implement the asymmetric key you need to change from using the symmetric HS256 algorithm to an asymmetric algorithm, for example RS256, that requires you to firstadd a new key, and then specify it whenadding each API domain. Please visitManaging Key Sets on the Approov documentation for more details.
Next, enable your Approovadmin
role with:
eval`approov role admin`
For the Windows powershell:
set APPROOV_ROLE=admin:___YOUR_APPROOV_ACCOUNT_NAME_HERE___
Now, get your Approov Secret with theApproov CLI:
approov secret -get base64
Next, add theApproov secret to your project.env
file:
APPROOV_BASE64_SECRET=approov_base64_secret_here
Now, add to yourrequirements.txt
file theJWT dependency:
PyJWT==1.7.1# update the version to the latest one
Next, you need to install the dependencies:
pip3 install -r requirements.txt
Now, add this code to your project, just before your first API endpoint:
fromflaskimportFlask,jsonify,request,abort,g,make_response# @link https://github.com/jpadilla/pyjwt/importjwtimportbase64importhashlib# @link https://github.com/theskumar/python-dotenvfromdotenvimportload_dotenv,find_dotenvload_dotenv(find_dotenv(),override=True)fromosimportgetenvapi=Flask(__name__)# Token secret value obtained with the Approov CLI tool:# - approov secret -getapproov_base64_secret=getenv('APPROOV_BASE64_SECRET')ifapproov_base64_secret==None:raiseValueError("Missing the value for environment variable: APPROOV_BASE64_SECRET")APPROOV_SECRET=base64.b64decode(approov_base64_secret)@api.before_requestdef_verifyApproovToken():approov_token=request.headers.get("Approov-Token")# If we didn't find a token, then reject the request.ifapproov_tokenisNoneorapproov_token=="":# You may want to add some logging here.returnabort(make_response({},401))try:# Decode the Approov token explicitly with the HS256 algorithm to# avoid the algorithm None attack.g.approov_token_claims=jwt.decode(approov_token,APPROOV_SECRET,algorithms=['HS256'])# When doesn't occur an exception we have a valid Aproov Tokenexceptjwt.ExpiredSignatureErrorase:# You may want to add some logging here.returnabort(make_response({},401))exceptjwt.InvalidTokenErrorase:# You may want to add some logging here.returnabort(make_response({},401))except:returnabort(make_response({},401))#@api.route("/")#def hello():# return jsonify({"message": "Hello World"})
NOTE: When the Approov token validation fails we return a
401
with an empty body, because we don't want to give clues to an attacker about the reason the request failed, and you can go even further by returning a400
.
Using thebefore_request
decorator approach will ensure that all endpoints in your API will be protected by Approov.
Not enough details in the bare bones quickstart? No worries, check thedetailed quickstarts that contain a more comprehensive set of instructions, including how to test the Approov integration.
In order to correctly check for the expiration times of the Approov tokens is very important that the backend server is synchronizing automatically the system clock over the network with an authoritative time source. In Linux this is usually done with a NTP server.
If you find any issue while following our instructions then just report ithere, with the steps to reproduce it, and we will sort it out and/or guide you to the correct path.
If you wish to explore the Approov solution in more depth, then why not try one of the following links as a jumping off point:
About
An Appoov token integration example for Python 3 with Flask. Article:https://blog.approov.io/approov-integration-in-a-python-flask-api
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.