Skip to content

Commit 0acb4c0

Browse files
authored
Merge pull request #14 from IABTechLab/ian-UID2-1270-align-python-with-java
Ian UI d2 1270 align python with java
2 parents f643a1a + 7ab1087 commit 0acb4c0

16 files changed

+699
-265
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test:
66
shell:
77
docker run -it -w $(PWD) -v $(PWD):$(PWD) -u `id -u`:`id -g` $(DOCKERIMAGE) /bin/bash
88

9-
examples: example_client example_auto_refresh
9+
examples: example_client example_auto_refresh example_sharing
1010

1111
example_client:
1212
docker run -w $(PWD) -v $(PWD):$(PWD) -u `id -u`:`id -g` -e PYTHONPATH=$(PWD) $(DOCKERIMAGE) python3 examples/sample_client.py "$(BASE_URL)" "$(AUTH_KEY)" "$(SECRET_KEY)" "$(AD_TOKEN)"
@@ -17,6 +17,10 @@ example_auto_refresh:
1717
example_encryption:
1818
docker run -w $(PWD) -v $(PWD):$(PWD) -u `id -u`:`id -g` -e PYTHONPATH=$(PWD) $(DOCKERIMAGE) python3 examples/sample_encryption.py "$(BASE_URL)" "$(AUTH_KEY)" "$(SECRET_KEY)" "$(AD_TOKEN)" "Hello World!"
1919

20+
example_sharing:
21+
docker run -w $(PWD) -v $(PWD):$(PWD) -u `id -u`:`id -g` -e PYTHONPATH=$(PWD) $(DOCKERIMAGE) python3 examples/sample_sharing.py "$(BASE_URL)" "$(AUTH_KEY)" "$(SECRET_KEY)" "$(RAW_UID)"
22+
23+
2024
docker:
2125
docker build -t $(DOCKERIMAGE) -f Dockerfile.dev .
2226

README.md

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,72 @@
22

33
The UID 2 Project is subject to Tech Lab IPR’s Policy and is managed by the IAB Tech Lab Addressability Working Group and Privacy & Rearc Commit Group. Please review [the governance rules](https://github.com/IABTechLab/uid2-core/blob/master/Software%20Development%20and%20Release%20Procedures.md).
44

5-
This SDK simplifies integration with UID2 for those using Python.
5+
This document includes:
6+
* [Who Is this SDK for?](#who-is-this-sdk-for)
7+
* [Requirements](#requirements)
8+
* [Install](#install)
9+
* [Usage for DSPs](#usage-for-dsps)
10+
* [Usage for UID2 Sharers](#usage-for-uid2-sharers)
11+
* [Development](#development)
12+
* [Example Usage](#example-usage)
613

7-
## Dependencies
14+
## Who Is this SDK for?
15+
16+
This SDK simplifies integration with UID2 for DSPs and UID Sharers, as described in [UID2 Integration Guides](https://unifiedid.com/docs/category/integration-guides).
17+
18+
## Requirements
819

920
This SDK supports Python 3.6 and above.
1021

11-
## Quick Start
22+
## Install
1223

13-
Connect to the UID2 service, refresh the encryption keys, and then use the keys to decrypt an advertising token, to arrive at the corresponding advertising ID:
24+
The SDK can be installed using pip.
25+
```
26+
pip install uid2-client
27+
```
1428

29+
## Usage for DSPs
30+
31+
Connect to the UID2 service, refresh the encryption keys, and then use the keys to decrypt an advertising token, to arrive at the corresponding advertising ID:
32+
For examples of usage for DSPs, see [sample_client.py](examples/sample_client.py) and [sample_auto_refresh.py](examples/sample_auto_refresh.py)
1533
```
16-
from uid2_client import Uid2Client, decrypt
34+
from uid2_client import Uid2ClientFactory
1735
18-
client = Uid2Client('https://prod.uidapi.com', 'my-auth-token', 'my-secret-key')
19-
keys = client.refresh_keys()
36+
# for UID2 (for EUID use EuidClientFactory)
37+
client = Uid2ClientFactory.create('https://prod.uidapi.com', 'my-auth-token', 'my-secret-key')
38+
client.refresh_keys()
2039
advertising_token = 'AgAAAANRdREk+IWqqnQkZ2rZdK0TgSUP/owLryysSkUGZJT+Gy551L1WJMAZA/G2B1UMDQ20WAqwwTu6o9TexWyux0lg0HHIbmJjN6IYwo+42KC8ugaR+PX0y18qQ+3yzkxmJ/ee//4IGu/1Yq4AmO4ArXN6CeszPTxByTkysVqyQVNY2A=='
21-
decrypted_token = decrypt(advertising_token, keys)
40+
decrypted_token = client.decrypt(advertising_token)
2241
print(decrypted_token.uid2)
2342
```
2443

25-
Additional examples are in the [examples] directory:
26-
* [sample_auto_refresh.py](examples/sample_auto_refresh.py)
27-
* [sample_client.py](examples/sample_client.py)
28-
* Includes an example to encrypt a raw UID2 into an advertising token for UID2 sharing.
44+
## Usage for Sharers
45+
46+
A UID2 sharer is a participant that wants to share UID2s or EUIDs with another participant. Raw UID2s must be encrypted into UID2 tokens before sending them to another participant.
47+
For examples of usage, see [sample_sharing.py](examples/sample_sharing.py) and [sample_auto_refresh.py](examples/sample_auto_refresh.py)
48+
49+
```
50+
from uid2_client import Uid2ClientFactory
51+
52+
# for UID2 (for EUID use EuidClientFactory)
53+
client = Uid2ClientFactory.create('https://prod.uidapi.com', 'my-auth-token', 'my-secret-key')
54+
client.refresh_keys()
55+
```
56+
Senders:
57+
58+
1. Call the following:
59+
```
60+
encrypted = client.encrypt(raw_uid)
61+
```
62+
2. If encryption was successful, send the token `encrypted.uid2` to the receiver.
63+
64+
Receivers:
65+
66+
1. Call the following:
67+
```
68+
decrypted = client.decrypt(uid_token)
69+
```
70+
2. If decryption was successful, use the token `decrypted.uid2`.
2971

3072
## Development
3173

@@ -53,13 +95,14 @@ Get access to an interactive shell within the Python 3.6 Docker image:
5395
make shell
5496
```
5597

56-
## Example Usage
98+
### Example Usage
5799

58100
To run all the example applications:
59101

60102
```
61103
make examples BASE_URL=https://prod.uidapi.com AUTH_KEY=my-auth-key SECRET_KEY=my-secret-key \
62-
AD_TOKEN=AgAAAANRdREk+IWqqnQkZ2rZdK0TgSUP/owLryysSkUGZJT+Gy551L1WJMAZA/G2B1UMDQ20WAqwwTu6o9TexWyux0lg0HHIbmJjN6IYwo+42KC8ugaR+PX0y18qQ+3yzkxmJ/ee//4IGu/1Yq4AmO4ArXN6CeszPTxByTkysVqyQVNY2A==
104+
AD_TOKEN=AgAAAANRdREk+IWqqnQkZ2rZdK0TgSUP/owLryysSkUGZJT+Gy551L1WJMAZA/G2B1UMDQ20WAqwwTu6o9TexWyux0lg0HHIbmJjN6IYwo+42KC8ugaR+PX0y18qQ+3yzkxmJ/ee//4IGu/1Yq4AmO4ArXN6CeszPTxByTkysVqyQVNY2A== \
105+
RAW_UID=JCqmlLXpbbu/jTdpB2a1cNAVs8O72eMXPaQzC9Ic9mE=
63106
```
64107

65108
Alternatively, you can run specific examples:

examples/sample_auto_refresh.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import time
44

55
from uid2_client import EncryptionKeysAutoRefresher
6-
from uid2_client import Uid2Client
7-
from uid2_client import decrypt
6+
from uid2_client import EuidClientFactory
7+
from uid2_client import Uid2ClientFactory
88

99

1010
def _usage():
@@ -20,13 +20,16 @@ def _usage():
2020
secret_key = sys.argv[3]
2121
ad_token = sys.argv[4]
2222

23-
client = Uid2Client(base_url, auth_key, secret_key)
23+
# for EUID use:
24+
# client = EuidClientFactory.create(base_url, auth_key, secret_key)
25+
# for UID2 use:
26+
client = Uid2ClientFactory.create(base_url, auth_key, secret_key)
2427
with EncryptionKeysAutoRefresher(client, dt.timedelta(seconds=4), dt.timedelta(seconds=7)) as refresher:
2528
for i in range(0, 20):
2629
refresh_result = refresher.current_result()
2730
if refresh_result.ready:
2831
print('Keys are ready, last refreshed (UTC):', refresh_result.last_success_time, flush=True)
29-
result = decrypt(ad_token, refresh_result.keys)
32+
result = client.decrypt(ad_token)
3033
print('UID2 =', result.uid2, flush=True)
3134
else:
3235
print('Keys are not ready yet, last error:', refresh_result.last_error[1], flush=True)

examples/sample_client.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import sys
22

3-
from uid2_client import Uid2Client
4-
from uid2_client import decrypt
5-
from uid2_client import encrypt
6-
from uid2_client.identity_scope import IdentityScope
3+
from uid2_client.euid_client_factory import EuidClientFactory
4+
from uid2_client.uid2_client_factory import Uid2ClientFactory
5+
76

87
# this sample client decrypts an advertising token into a raw UID2
9-
# and then encrypts it into a new advertising token
10-
# to demonstrate decryption (for DSPs and sharers) and encryption (sharers only).
8+
# to demonstrate decryption for DSPs
119

1210
def _usage():
1311
print('Usage: python3 sample_client.py <base_url> <auth_key> <secret_key> <ad_token>', file=sys.stderr)
@@ -22,17 +20,14 @@ def _usage():
2220
secret_key = sys.argv[3]
2321
ad_token = sys.argv[4]
2422

25-
client = Uid2Client(base_url, auth_key, secret_key)
26-
keys = client.refresh_keys()
27-
decrypt_result = decrypt(ad_token, keys)
23+
# for EUID use:
24+
# client = EuidClientFactory.create(base_url, auth_key, secret_key)
25+
# for UID2 use:
26+
client = Uid2ClientFactory.create(base_url, auth_key, secret_key)
27+
client.refresh_keys()
28+
decrypt_result = client.decrypt(ad_token)
2829

2930
print('UID2 =', decrypt_result.uid2)
3031
print('Established =', decrypt_result.established)
3132
print('Site ID =', decrypt_result.site_id)
3233
print('Site Key Site ID =', decrypt_result.site_key_site_id)
33-
34-
# Not required for DSPs, but for those using UID2 sharing functionality this shows how to encrypt a raw UID2 into
35-
# a new advertising token.
36-
# IdentityScope could be UID2 or EUID
37-
new_ad_token = encrypt(decrypt_result.uid2, IdentityScope.UID2, keys)
38-
print('New Ad Token =', new_ad_token)

examples/sample_sharing.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
3+
from uid2_client import EuidClientFactory
4+
from uid2_client import Uid2ClientFactory
5+
6+
7+
# this sample client encrypts and decrypts a uid2 to a sharing token
8+
# to demonstrate encryption and decryption for sharers
9+
10+
def _usage():
11+
print('Usage: python3 sample_encryption.py <base_url> <auth_key> <secret_key> <raw_uid>', file=sys.stderr)
12+
sys.exit(1)
13+
14+
15+
if len(sys.argv) <= 4:
16+
_usage()
17+
18+
base_url = sys.argv[1]
19+
auth_key = sys.argv[2]
20+
secret_key = sys.argv[3]
21+
raw_uid = sys.argv[4]
22+
23+
# for EUID use:
24+
# client = EuidClientFactory.create(base_url, auth_key, secret_key)
25+
# for UID2 use:
26+
client = Uid2ClientFactory.create(base_url, auth_key, secret_key)
27+
client.refresh_keys()
28+
new_ad_token = client.encrypt(raw_uid)
29+
30+
print('New Ad Token =', new_ad_token)
31+
32+
decrypt_result = client.decrypt(new_ad_token)
33+
34+
print('Decrypted UID2 =', decrypt_result.uid2)
35+
print('Established =', decrypt_result.established)
36+
print('Site ID =', decrypt_result.site_id)
37+
print('Site Key Site ID =', decrypt_result.site_key_site_id)

0 commit comments

Comments
 (0)