Skip to content

Commit 71c6eaa

Browse files
jeremjeremad
authored andcommitted
feat(identity): check app ID/secret matching
When generating an identity, we now check that the app secret and the app ID matches, it avoids very hard to debug behavior.
1 parent 2dc1a7d commit 71c6eaa

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

tankersdk_identity/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,25 @@
66
import tankersdk_identity.crypto
77

88

9+
APP_SECRET_SIZE = 64
10+
APP_PUBLIC_KEY_SIZE = 32
11+
AUTHOR_SIZE = 32
12+
APP_CREATION_NATURE = 1
13+
14+
915
def _hash_user_id(app_id, user_id):
1016
user_id_buff = user_id.encode()
1117
to_hash = user_id_buff + app_id
1218
return tankersdk_identity.crypto.generichash(to_hash, size=BLOCK_HASH_SIZE)
1319

1420

21+
def _generate_app_id(app_secret):
22+
public_key = app_secret[APP_SECRET_SIZE - APP_PUBLIC_KEY_SIZE:APP_SECRET_SIZE]
23+
# A bit convoluted, to make it work with python 2.7
24+
to_hash = bytes(bytearray([APP_CREATION_NATURE] + [0] * AUTHOR_SIZE)) + public_key
25+
return tankersdk_identity.crypto.generichash(to_hash, size=BLOCK_HASH_SIZE)
26+
27+
1528
def _generate_preshare_keys():
1629
enc_pub_key, enc_priv_key = tankersdk_identity.crypto.box_keypair()
1730
encryption_keys = {
@@ -60,6 +73,10 @@ def create_identity(app_id, app_secret, user_id):
6073
secret_buf = base64.b64decode(app_secret)
6174
hashed_user_id = _hash_user_id(app_id_buf, user_id)
6275

76+
generated_app_id = _generate_app_id(secret_buf)
77+
if generated_app_id != app_id_buf:
78+
raise ValueError("App secret and app ID mismatch")
79+
6380
e_public_key, e_secret_key = tankersdk_identity.crypto.sign_keypair()
6481
to_sign = e_public_key + hashed_user_id
6582
delegation_signature = tankersdk_identity.crypto.sign_detached(to_sign, secret_buf)

tankersdk_identity/test/test_identity.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,14 @@ def test_get_public_from_bad_identity():
120120
fake_id = base64.b64encode(json.dumps({"target": "stuffs"}).encode()).decode()
121121
with pytest.raises(ValueError):
122122
tankersdk_identity.get_public_identity(fake_id)
123+
124+
125+
def test_mistmatch_app_id_and_secret(test_app):
126+
user_id = "guido@tanker.io"
127+
mismatching_app_id = "rB0/yEJWCUVYRtDZLtXaJqtneXQOsCSKrtmWw+V+ysc="
128+
with pytest.raises(ValueError):
129+
tankersdk_identity.create_identity(
130+
mismatching_app_id,
131+
test_app["secret"],
132+
user_id
133+
)

0 commit comments

Comments
 (0)