Skip to content

Commit d19b916

Browse files
committed
Moved the quickstart to the simpler format.
Signed-off-by: Exadra37 <exadra37@gmail.com>
1 parent c85f75a commit d19b916

File tree

10 files changed

+423
-68
lines changed

10 files changed

+423
-68
lines changed

EXAMPLES.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ or get a bash shell inside the container:
9191
sudo docker-compose run --rm --service-ports approov-token-binding-check zsh
9292
```
9393

94-
## Support
94+
## Issues
9595

96-
If you find any issue while following this quickstart then just open an issue on this repo with the steps to reproduce it and we will help you to solve them.
96+
If you find any issue while following our instructions then just report it [here](https://github.com/approov/quickstart-python-django-token-check/issues), with the steps to reproduce it, and we will sort it out and/or guide you to the correct path.
97+
98+
99+
## Useful Links
100+
101+
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:
102+
103+
* [Approov Free Trial](https://approov.io/signup)(no credit card needed)
104+
* [Approov Get Started](https://approov.io/product/demo)
105+
* [Approov QuickStarts](https://approov.io/docs/latest/approov-integration-examples/)
106+
* [Approov Docs](https://approov.io/docs)
107+
* [Approov Blog](https://approov.io/blog/)
108+
* [Approov Resources](https://approov.io/resource/)
109+
* [Approov Customer Stories](https://approov.io/customer)
110+
* [Approov Support](https://approov.zendesk.com/hc/en-gb/requests/new)
111+
* [About Us](https://approov.io/company)
112+
* [Contact Us](https://approov.io/contact)

OVERVIEW.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Approov Overview
2+
3+
[Approov](https://approov.io) is an API security solution used to verify that requests received by your backend services originate from trusted versions of your mobile apps.
4+
5+
6+
## Why?
7+
8+
You can learn more about Approov, the motives for adopting it, and more detail on how it works by following this [link](https://approov.io/product). In brief, Approov:
9+
10+
* Ensures that accesses to your API come from official versions of your apps; it blocks accesses from republished, modified, or tampered versions
11+
* Protects the sensitive data behind your API; it prevents direct API abuse from bots or scripts scraping data and other malicious activity
12+
* Secures the communication channel between your app and your API with [Approov Dynamic Certificate Pinning](https://approov.io/docs/latest/approov-usage-documentation/#approov-dynamic-pinning). This has all the benefits of traditional pinning but without the drawbacks
13+
* Removes the need for an API key in the mobile app
14+
* Provides DoS protection against targeted attacks that aim to exhaust the API server resources to prevent real users from reaching the service or to at least degrade the user experience.
15+
16+
17+
## How it works?
18+
19+
This is a brief overview of how the Approov cloud service and the backend server fit together from a backend perspective. For a complete overview of how the mobile app and backend fit together with the Approov cloud service and the Approov SDK we recommend to read the [Approov overview](https://approov.io/product) page on our website.
20+
21+
### Approov Cloud Service
22+
23+
The Approov cloud service attests that a device is running a legitimate and tamper-free version of your mobile app.
24+
25+
* If the integrity check passes then a valid token is returned to the mobile app
26+
* If the integrity check fails then a legitimate looking token will be returned
27+
28+
In either case, the app, unaware of the token's validity, adds it to every request it makes to the Approov protected API(s).
29+
30+
### The Backend Server
31+
32+
The backend server ensures that the token supplied in the `Approov-Token` header is present and valid. The validation is done by using a shared secret known only to the Approov cloud service and the backend server.
33+
34+
The request is handled such that:
35+
36+
* If the Approov Token is valid, the request is allowed to be processed by the API endpoint
37+
* If the Approov Token is invalid, an HTTP 401 Unauthorized response is returned
38+
39+
You can choose to log JWT verification failures, but we left it out on purpose so that you can have the choice of how you prefer to do it and decide the right amount of information you want to log.
40+
41+
42+
## Useful Links
43+
44+
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:
45+
46+
* [Approov Free Trial](https://approov.io/signup)(no credit card needed)
47+
* [Approov Get Started](https://approov.io/product/demo)
48+
* [Approov QuickStarts](https://approov.io/docs/latest/approov-integration-examples/)
49+
* [Approov Docs](https://approov.io/docs)
50+
* [Approov Blog](https://approov.io/blog/)
51+
* [Approov Resources](https://approov.io/resource/)
52+
* [Approov Customer Stories](https://approov.io/customer)
53+
* [Approov Support](https://approov.zendesk.com/hc/en-gb/requests/new)
54+
* [About Us](https://approov.io/company)
55+
* [Contact Us](https://approov.io/contact)

QUICKSTARTS.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Approov Integration Quickstarts
2+
3+
[Approov](https://approov.io) is an API security solution used to verify that requests received by your backend services originate from trusted versions of your mobile apps.
4+
5+
6+
## The Quickstarts
7+
8+
The quickstart code for the Approov backend server is split into two implementations. The first gets you up and running with basic token checking. The second uses a more advanced Approov feature, _token binding_. Token binding may be used to link the Approov token with other properties of the request, such as user authentication (more details can be found [here](https://approov.io/docs/latest/approov-usage-documentation/#token-binding)).
9+
* [Approov token check quickstart](/docs/APPROOV_TOKEN_QUICKSTART.md)
10+
* [Approov token check with token binding quickstart](/docs/APPROOV_TOKEN_BINDING_QUICKSTART.md)
11+
12+
Both the quickstarts are built from the unprotected example server defined [here](/src/unprotected-server/hello), thus you can use Git to see the code differences between them.
13+
14+
Code difference between the Approov token check quickstart and the original unprotected server:
15+
16+
```
17+
git diff --no-index src/unprotected-server/hello src/approov-protected-server/token-check/hello
18+
```
19+
20+
You can do the same for the Approov token binding quickstart:
21+
22+
```
23+
git diff --no-index src/unprotected-server/hello src/approov-protected-server/token-binding-check/hello
24+
```
25+
26+
Or you can compare the code difference between the two quickstarts:
27+
28+
```
29+
git diff --no-index src/approov-protected-server/token-check/hello src/approov-protected-server/token-binding-check/hello
30+
```
31+
32+
## Issues
33+
34+
If you find any issue while following our instructions then just report it [here](https://github.com/approov/quickstart-python-django-token-check/issues), with the steps to reproduce it, and we will sort it out and/or guide you to the correct path.
35+
36+
37+
## Useful Links
38+
39+
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:
40+
41+
* [Approov Free Trial](https://approov.io/signup)(no credit card needed)
42+
* [Approov Get Started](https://approov.io/product/demo)
43+
* [Approov QuickStarts](https://approov.io/docs/latest/approov-integration-examples/)
44+
* [Approov Docs](https://approov.io/docs)
45+
* [Approov Blog](https://approov.io/blog/)
46+
* [Approov Resources](https://approov.io/resource/)
47+
* [Approov Customer Stories](https://approov.io/customer)
48+
* [Approov Support](https://approov.zendesk.com/hc/en-gb/requests/new)
49+
* [About Us](https://approov.io/company)
50+
* [Contact Us](https://approov.io/contact)

README.md

Lines changed: 86 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,109 +7,139 @@ This repo implements the Approov server-side request verification code in Python
77
This is an Approov integration quickstart example for the Python Django framework. If you are looking for another Python integration you can check our list of [quickstarts](https://approov.io/docs/latest/approov-integration-examples/backend-api/), and if you don't find what you are looking for, then please let us know [here](https://approov.io/contact). Meanwhile, you can always use the framework agnostic [quickstart example](https://github.com/approov/quickstart-python-token-check) for Python, and you may find that's easily adaptable to your framework of choice.
88

99

10-
## TOC - Table of Contents
10+
## Approov Integration Quickstart
1111

12-
* [Why?](#why)
13-
* [How it Works?](#how-it-works)
14-
* [Quickstarts](#approov-integration-quickstarts)
15-
* [Examples](#approov-integration-examples)
16-
* [Useful Links](#useful-links)
12+
The quickstart was tested with the following Operating Systems:
1713

14+
* Ubuntu 20.04
15+
* MacOS Big Sur
16+
* Windows 10 WSL2 - Ubuntu 20.04
1817

19-
## Why?
18+
First, setup the [Appoov CLI](https://approov.io/docs/latest/approov-installation/index.html#initializing-the-approov-cli).
2019

21-
You can learn more about Approov, the motives for adopting it, and more detail on how it works by following this [link](https://approov.io/product). In brief, Approov:
20+
Now, register the API domain for which Approov will issues tokens:
2221

23-
* Ensures that accesses to your API come from official versions of your apps; it blocks accesses from republished, modified, or tampered versions
24-
* Protects the sensitive data behind your API; it prevents direct API abuse from bots or scripts scraping data and other malicious activity
25-
* Secures the communication channel between your app and your API with [Approov Dynamic Certificate Pinning](https://approov.io/docs/latest/approov-usage-documentation/#approov-dynamic-pinning). This has all the benefits of traditional pinning but without the drawbacks
26-
* Removes the need for an API key in the mobile app
27-
* Provides DoS protection against targeted attacks that aim to exhaust the API server resources to prevent real users from reaching the service or to at least degrade the user experience.
22+
```bash
23+
approov api -add api.example.com
24+
```
2825

29-
[TOC](#toc---table-of-contents)
26+
Next, enable your Approov `admin` role with:
3027

28+
```bash
29+
eval `approov role admin`
30+
```
3131

32-
## How it works?
32+
Now, get your Approov Secret with the [Appoov CLI](https://approov.io/docs/latest/approov-installation/index.html#initializing-the-approov-cli):
3333

34-
This is a brief overview of how the Approov cloud service and the Python Django API server fit together from a backend perspective. For a complete overview of how the mobile app and backend fit together with the Approov cloud service and the Approov SDK we recommend to read the [Approov overview](https://approov.io/product) page on our website.
34+
```bash
35+
approov secret -get base64
36+
```
3537

36-
### Approov Cloud Service
38+
Next, add the [Approov secret](https://approov.io/docs/latest/approov-usage-documentation/#account-secret-key-export) to your project `.env` file:
3739

38-
The Approov cloud service attests that a device is running a legitimate and tamper-free version of your mobile app.
40+
```env
41+
APPROOV_BASE64_SECRET=approov_base64_secret_here
42+
```
3943

40-
* If the integrity check passes then a valid token is returned to the mobile app
41-
* If the integrity check fails then a legitimate looking token will be returned
44+
Now, add to your `requirements.txt` file the [JWT dependency](https://github.com/jpadilla/pyjwt/):
4245

43-
In either case, the app, unaware of the token's validity, adds it to every request it makes to the Approov protected API(s).
46+
```bash
47+
PyJWT==1.7.1 # update the version to the latest one
48+
```
4449

45-
### Python Django API Server
50+
Next, you need to install the dependencies:
4651

47-
The Python Django API server ensures that the token supplied in the `Approov-Token` header is present and valid. The validation is done by using a shared secret known only to the Approov cloud service and the Python Django API server.
52+
```bash
53+
pip3 install -r requirements.txt
54+
```
4855

49-
The request is handled such that:
56+
Now, add the [approov_middleware.py](/src/approov-protected-server/token-check/hello/approov_middleware.py) class to your project:
5057

51-
* If the Approov Token is valid, the request is allowed to be processed by the API endpoint
52-
* If the Approov Token is invalid, an HTTP 401 Unauthorized response is returned
58+
```python
59+
from django.http import JsonResponse
60+
from os import getenv
61+
from dotenv import load_dotenv, find_dotenv
62+
import base64
63+
import jwt # https://github.com/jpadilla/pyjwt/
5364

54-
You can choose to log JWT verification failures, but we left it out on purpose so that you can have the choice of how you prefer to do it and decide the right amount of information you want to log.
65+
# @link https://django.readthedocs.io/en/stable/topics/http/middleware.html
66+
class ApproovMiddleware:
67+
def __init__(self, get_response):
68+
self.get_response = get_response
5569

56-
>#### System Clock
57-
>
58-
>In order to correctly check for the expiration times of the Approov tokens is very important that the Python Django API 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.
70+
load_dotenv(find_dotenv(), override=True)
5971

60-
[TOC](#toc---table-of-contents)
72+
# Token secret value obtained with the Approov CLI tool:
73+
# - approov secret -get
74+
approov_base64_secret = getenv('APPROOV_BASE64_SECRET')
6175

76+
if approov_base64_secret == None:
77+
raise ValueError("Missing the value for environment variable: APPROOV_BASE64_SECRET")
6278

63-
## Approov Integration Quickstarts
79+
self.APPROOV_SECRET = base64.b64decode(approov_base64_secret)
6480

65-
The quickstart code for the Approov Python Django API server is split into two implementations. The first gets you up and running with basic token checking. The second uses a more advanced Approov feature, _token binding_. Token binding may be used to link the Approov token with other properties of the request, such as user authentication (more details can be found [here](https://approov.io/docs/latest/approov-usage-documentation/#token-binding)).
66-
* [Approov token check quickstart](/docs/APPROOV_TOKEN_QUICKSTART.md)
67-
* [Approov token check with token binding quickstart](/docs/APPROOV_TOKEN_BINDING_QUICKSTART.md)
81+
def __call__(self, request):
82+
approov_token_claims = self.verifyApproovToken(request)
6883

69-
Both the quickstarts are built from the unprotected example server defined in this Django [project](/src/unprotected-server/hello).
84+
if approov_token_claims == None:
85+
return JsonResponse({}, status = 401)
7086

71-
You can use Git to see the code differences between the two quickstarts:
87+
return self.get_response(request)
7288

73-
```
74-
git diff --no-index src/approov-protected-server/token-check/hello/approov_middleware.py src/approov-protected-server/token-binding-check/hello/approov_middleware.py
75-
```
76-
77-
[TOC](#toc---table-of-contents)
89+
# @link https://approov.io/docs/latest/approov-usage-documentation/#backend-integration
90+
def verifyApproovToken(self, request):
91+
approov_token = request.headers.get("Approov-Token")
7892

93+
# If we didn't find a token, then reject the request.
94+
if approov_token == None:
95+
# You may want to add some logging here.
96+
return None
7997

80-
## Approov Integration Examples
98+
try:
99+
# Decode the Approov token explicitly with the HS256 algorithm to avoid
100+
# the algorithm None attack.
101+
approov_token_claims = jwt.decode(approov_token, self.APPROOV_SECRET, algorithms=['HS256'])
102+
return approov_token_claims
103+
except jwt.ExpiredSignatureError as e:
104+
# You may want to add some logging here.
105+
return None
106+
except jwt.InvalidTokenError as e:
107+
# You may want to add some logging here.
108+
return None
81109

82-
The code examples for the Approov quickstarts are extracted from this simple [Approov integration examples](/src/approov-protected-server), that you can run from your computer to play around with the Approov integration and gain a better understanding of how simple and easy it is to integrate Approov in a Python Django API server.
110+
```
83111

84-
### Testing with Postman
112+
Finally, to activate the [Approov Middleware](/src/approov-protected-server/token-check/hello/approov_middleware.py) you just need to include it in the middleware list of your [Django settings](/src/approov-protected-server/token-check/hello/settings.py) as the first one in the list:
85113

86-
A ready-to-use Postman collection can be found [here](https://raw.githubusercontent.com/approov/postman-collections/master/quickstarts/hello-world/hello-world.postman_collection.json). It contains a comprehensive set of example requests to send to the Python Django API server for testing. The collection contains requests with valid and invalid Approov tokens, and with and without token binding.
114+
```python
115+
MIDDLEWARE = [
116+
'YOUR_APP_NAME.approov_middleware.ApproovMiddleware',
117+
'django.middleware.security.SecurityMiddleware',
118+
# lines omitted
119+
]
120+
```
87121

88-
### Testing with Curl
122+
> **NOTE:** The Approov middleware is included as the first one in the list because you don't want to waste your server resources in processing requests that don't have a valid Approov token. This approach will help your server to handle more load under a Denial of Service(DoS) attack.
89123
90-
An alternative to the Postman collection is to use cURL to make the API requests. Check some examples [here](https://github.com/approov/postman-collections/blob/master/quickstarts/hello-world/hello-world.postman_curl_requests_examples.md).
124+
Not enough details in the bare bones quickstart? No worries, check the [detailed quickstarts](QUICKSTARTS.md) that contain a more comprehensive set of instructions, including how to test the Approov integration.
91125

92-
### The Dummy Secret
93126

94-
The valid Approov tokens in the Postman collection and cURL requests examples were signed with a dummy secret that was generated with `openssl rand -base64 64 | tr -d '\n'; echo`, therefore not a production secret retrieved with `approov secret -get base64`, thus in order to use it you need to set the `APPROOV_BASE64_SECRET`, in the `.env` file for each [Approov integration example](/src/approov-protected-server), to the following value: `h+CX0tOzdAAR9l15bWAqvq7w9olk66daIH+Xk+IAHhVVHszjDzeGobzNnqyRze3lw/WVyWrc2gZfh3XXfBOmww==`.
127+
## Issues
95128

96-
[TOC](#toc---table-of-contents)
129+
If you find any issue while following our instructions then just report it [here](https://github.com/approov/quickstart-python-django-token-check/issues), with the steps to reproduce it, and we will sort it out and/or guide you to the correct path.
97130

98131

99132
## Useful Links
100133

101134
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:
102135

103136
* [Approov Free Trial](https://approov.io/signup)(no credit card needed)
137+
* [Approov Get Started](https://approov.io/product/demo)
104138
* [Approov QuickStarts](https://approov.io/docs/latest/approov-integration-examples/)
105-
* [Approov Live Demo](https://approov.io/product/demo)
106139
* [Approov Docs](https://approov.io/docs)
107-
* [Approov Blog](https://blog.approov.io)
140+
* [Approov Blog](https://approov.io/blog/)
108141
* [Approov Resources](https://approov.io/resource/)
109142
* [Approov Customer Stories](https://approov.io/customer)
110143
* [Approov Support](https://approov.zendesk.com/hc/en-gb/requests/new)
111144
* [About Us](https://approov.io/company)
112145
* [Contact Us](https://approov.io/contact)
113-
114-
115-
[TOC](#toc---table-of-contents)

0 commit comments

Comments
 (0)