Skip to content

Commit 75843e9

Browse files
jagermanmajestrate
authored andcommitted
flake8/black
1 parent d4d176f commit 75843e9

File tree

12 files changed

+54
-72
lines changed

12 files changed

+54
-72
lines changed

contrib/auth-example.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# Example script for demonstrating X-SOGS-* authentication calculation.
22

33
import nacl.bindings as salt
4-
from nacl.public import PrivateKey, PublicKey
54
from nacl.signing import SigningKey
65
from hashlib import blake2b, sha512
76
from base64 import b64encode
8-
from typing import Optional
97

108
# import time
119
# import nacl.utils
@@ -125,10 +123,10 @@ def get_signing_headers(
125123
# X-SOGS-Pubkey: 00bac6e71efd7dfa4a83c98ed24f254ab2c267f9ccdb172a5280a0444ad24e89cc
126124
# X-SOGS-Timestamp: 1642472103
127125
# X-SOGS-Nonce: CdB5nyKVmQGCw6s0Bvv8Ww==
128-
# X-SOGS-Signature: xxLpXHbomAJMB9AtGMyqvBsXrdd2040y+Ol/IKzElWfKJa3EYZRv1GLO6CTLhrDFUwVQe8PPltyGs54Kd7O5Cg==
126+
# X-SOGS-Signature: xxLpXHbomAJMB9AtGMyqvBsXrdd2040y+Ol/IKzElWfKJa3EYZRv1GLO6CTLhrDFUwVQe8PPltyGs54Kd7O5Cg== # noqa E501
129127
#
130128
# Blinded headers:
131129
# X-SOGS-Pubkey: 1598932d4bccbe595a8789d7eb1629cefc483a0eaddc7e20e8fe5c771efafd9af5
132130
# X-SOGS-Timestamp: 1642472103
133131
# X-SOGS-Nonce: CdB5nyKVmQGCw6s0Bvv8Ww==
134-
# X-SOGS-Signature: n4HK33v7gkcz/3pZuWvzmOlY+AbzbpEN1K12dtCc8Gw0m4iP5gUddGKKLEbmoWNhqJeY2S81Lm9uK2DBBN8aCg==
132+
# X-SOGS-Signature: n4HK33v7gkcz/3pZuWvzmOlY+AbzbpEN1K12dtCc8Gw0m4iP5gUddGKKLEbmoWNhqJeY2S81Lm9uK2DBBN8aCg== # noqa E501

contrib/blinding.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from nacl.signing import SigningKey, VerifyKey
2-
from nacl.public import PublicKey
32
import nacl.bindings as salt
43
from nacl.utils import random
54
import nacl.hash
@@ -36,7 +35,8 @@
3635
assert salt.crypto_scalarmult_ed25519_base_noclamp(ka) == kA
3736
assert salt.crypto_core_ed25519_is_valid_point(kA)
3837

39-
##### Signing (e.g. for X-SOGS-*) with a blinded keypair ka/kA
38+
#############################
39+
# Signing (e.g. for X-SOGS-*) with a blinded keypair ka/kA
4040

4141
# This generation is *almost* just bog standard Ed25519 but we have one change: when generating
4242
# r we add kA into the hash r = H(H_rh || kA || M), rather than r = H(H_rh || M), so that there
@@ -56,7 +56,8 @@
5656

5757
assert VerifyKey(kA).verify(message_to_sign, full_sig)
5858

59-
##### Sending a DM
59+
#############################
60+
# Sending a DM
6061

6162
# Our user A above wants to send a SOGS DM to another user B:
6263
s2 = SigningKey.generate()
@@ -72,7 +73,9 @@
7273
assert salt.crypto_scalarmult_ed25519_base_noclamp(kb) == kB
7374
assert salt.crypto_core_ed25519_is_valid_point(kB)
7475

75-
##### Finding friends:
76+
#############################
77+
# Finding friends:
78+
7679
# For example (in reality this would come directly from the known session id):
7780
friend_xpk = salt.crypto_sign_ed25519_pk_to_curve25519(A)
7881

@@ -112,7 +115,8 @@
112115
print("failed; got neither ±A")
113116
continue
114117

115-
##### Encrypting a SOGS DM
118+
#############################
119+
# Encrypting a SOGS DM
116120
msg = 'hello 🎂'
117121

118122
# Step one: calculate a shared secret, sending from A to B. We're going to calculate:
@@ -138,7 +142,8 @@
138142

139143
data = b'\x00' + ciphertext + nonce
140144

141-
##### Decrypting a SOGS DM
145+
#############################
146+
# Decrypting a SOGS DM
142147
# Opening the box on the recipient end.
143148

144149
# I receive alongside the message from sogs (i.e. this is the blinded session id minus the '15')

sogs/crypto.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ def server_encrypt(pk, data):
6969

7070
@functools.lru_cache(maxsize=1024)
7171
def compute_derived_key_bytes(pk_bytes):
72-
""" compute derived key as bytes with no prefix """
72+
"""compute derived key as bytes with no prefix"""
7373
return crypto_scalarmult(server_pubkey_hash_bytes, pk_bytes)
7474

7575

7676
def compute_derived_id(session_id, prefix='15'):
77-
""" compute derived session """
77+
"""compute derived session"""
7878
return prefix + binascii.hexlify(
7979
compute_derived_key_bytes(decode_hex_or_b64(session_id[2:], 32))
8080
).decode('ascii')

sogs/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def add_new_columns(conn):
200200
'whisper': 'INTEGER REFERENCES users(id)',
201201
'whisper_mods': 'BOOLEAN NOT NULL DEFAULT FALSE',
202202
'filtered': 'BOOLEAN NOT NULL DEFAULT FALSE',
203-
},
203+
}
204204
}
205205

206206
added = False

sogs/hashing.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def blake2b(
3939
"""
4040

4141
return _multipart_hash(
42-
nacl.hashlib.blake2b(digest_size=digest_size, key=key, salt=salt, person=person),
43-
data)
42+
nacl.hashlib.blake2b(digest_size=digest_size, key=key, salt=salt, person=person), data
43+
)
4444

4545

4646
def sha512(data):
@@ -53,6 +53,4 @@ def sha512(data):
5353
5454
Returns a bytes of length 64.
5555
"""
56-
return _multipart_hash(
57-
hashlib.sha512(),
58-
data)
56+
return _multipart_hash(hashlib.sha512(), data)

sogs/model/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def system_user(self):
250250

251251
@property
252252
def derived_key(self):
253-
""" get the derived key for this user """
253+
"""get the derived key for this user"""
254254
if self.session_id[0:2] == '15':
255255
return self.session_id
256256
return crypto.compute_derived_id(self.session_id)

sogs/routes/auth.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from ..hashing import blake2b
66

77
from flask import request, abort, Response, g
8-
import string
98
import time
109
from nacl.signing import VerifyKey
1110
from nacl.exceptions import BadSignatureError
@@ -219,7 +218,6 @@ def handle_http_auth():
219218
# TODO: if "blinding required" config option is set then reject the request here
220219
session_id = '05' + pk.to_curve25519_public_key().encode().hex()
221220

222-
223221
try:
224222
nonce = utils.decode_hex_or_b64(nonce, 16)
225223
except Exception:
@@ -261,7 +259,6 @@ def handle_http_auth():
261259
except sqlalchemy.exc.IntegrityError:
262260
abort_with_reason(http.TOO_EARLY, "Invalid authentication: X-SOGS-Nonce cannot be reused")
263261

264-
265262
# Signature validation
266263

267264
# Signature should be on:

tests/auth.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from nacl.signing import VerifyKey, SigningKey
1+
from nacl.signing import SigningKey
22
from nacl.public import PublicKey
33
from typing import Optional
44
import time
@@ -37,14 +37,15 @@ def x_sogs_raw(
3737

3838
if blinded:
3939
a = s.to_curve25519_private_key().encode()
40-
k = salt.crypto_core_ed25519_scalar_reduce(blake2b(sogs.crypto.server_pubkey_bytes, digest_size=64))
40+
k = salt.crypto_core_ed25519_scalar_reduce(
41+
blake2b(sogs.crypto.server_pubkey_bytes, digest_size=64)
42+
)
4143
ka = salt.crypto_core_ed25519_scalar_mul(k, a)
4244
kA = salt.crypto_scalarmult_ed25519_base_noclamp(ka)
4345
pubkey = '15' + kA.hex()
4446
else:
4547
pubkey = '00' + s.verify_key.encode().hex()
4648

47-
4849
to_sign = [B.encode(), n, str(ts).encode(), method.encode(), full_path.encode()]
4950
if body:
5051
to_sign.append(blake2b(body, digest_size=64))
@@ -54,7 +55,9 @@ def x_sogs_raw(
5455
r = salt.crypto_core_ed25519_scalar_reduce(sha512([H_rh, kA, *to_sign]))
5556
sig_R = salt.crypto_scalarmult_ed25519_base_noclamp(r)
5657
HRAM = salt.crypto_core_ed25519_scalar_reduce(sha512([sig_R, kA, *to_sign]))
57-
sig_s = salt.crypto_core_ed25519_scalar_add(r, salt.crypto_core_ed25519_scalar_mul(HRAM, ka))
58+
sig_s = salt.crypto_core_ed25519_scalar_add(
59+
r, salt.crypto_core_ed25519_scalar_mul(HRAM, ka)
60+
)
5861
sig = sig_R + sig_s
5962

6063
else:
@@ -64,7 +67,7 @@ def x_sogs_raw(
6467
'X-SOGS-Pubkey': pubkey,
6568
'X-SOGS-Nonce': sogs.utils.encode_base64(n) if b64_nonce else n.hex(),
6669
'X-SOGS-Timestamp': str(ts),
67-
'X-SOGS-Signature': sogs.utils.encode_base64(sig)
70+
'X-SOGS-Signature': sogs.utils.encode_base64(sig),
6871
}
6972

7073
return h, n, ts, sig

tests/test_auth.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,13 @@ def test_auth_basic(client, db):
7575

7676
# Barely good timestamp
7777
r = client.get(
78-
"/auth_test/whoami",
79-
headers=x_sogs(a, B, 'GET', '/auth_test/whoami', timestamp_off=86399),
78+
"/auth_test/whoami", headers=x_sogs(a, B, 'GET', '/auth_test/whoami', timestamp_off=86399)
8079
)
8180
assert r.status_code == 200
8281
assert r.json == {"user": {"uid": 1, "session_id": session_id}}
8382

8483
r = client.get(
85-
"/auth_test/whoami",
86-
headers=x_sogs(a, B, 'GET', '/auth_test/whoami', timestamp_off=-86399),
84+
"/auth_test/whoami", headers=x_sogs(a, B, 'GET', '/auth_test/whoami', timestamp_off=-86399)
8785
)
8886
assert r.status_code == 200
8987
assert r.json == {"user": {"uid": 1, "session_id": session_id}}
@@ -275,15 +273,13 @@ def test_auth_malformed(client, db):
275273

276274
# Bad timestamps
277275
r = client.get(
278-
"/auth_test/whoami",
279-
headers=x_sogs(a, B, 'GET', '/auth_test/whoami', timestamp_off=86401),
276+
"/auth_test/whoami", headers=x_sogs(a, B, 'GET', '/auth_test/whoami', timestamp_off=86401)
280277
)
281278
assert r.status_code == 425
282279
assert r.data == b'Invalid authentication: X-SOGS-Timestamp is too far from current time'
283280

284281
r = client.get(
285-
"/auth_test/whoami",
286-
headers=x_sogs(a, B, 'GET', '/auth_test/whoami', timestamp_off=-86401),
282+
"/auth_test/whoami", headers=x_sogs(a, B, 'GET', '/auth_test/whoami', timestamp_off=-86401)
287283
)
288284
assert r.status_code == 425
289285
assert r.data == b'Invalid authentication: X-SOGS-Timestamp is too far from current time'
@@ -332,10 +328,7 @@ def test_auth_batch(client, db):
332328
{
333329
'code': 200,
334330
'headers': {'content-type': 'application/json'},
335-
'body': {
336-
'user': {'uid': 1, 'session_id': session_id},
337-
'body': ['hi', 'world'],
338-
},
331+
'body': {'user': {'uid': 1, 'session_id': session_id}, 'body': ['hi', 'world']},
339332
},
340333
{
341334
'code': 200,

tests/test_dm.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from request import sogs_get, sogs_post
2-
from sogs import crypto, config
2+
from sogs import config
33
from sogs.hashing import blake2b
44
from sogs.utils import encode_base64
55
from sogs.model.user import SystemUser
@@ -31,23 +31,30 @@ def make_post(message, sender, to):
3131
plaintext = message + sender.ed_key.verify_key.encode()
3232
nonce = random(24)
3333
ciphertext = salt.crypto_aead_xchacha20poly1305_ietf_encrypt(
34-
plaintext, aad=None, nonce=nonce, key=key)
34+
plaintext, aad=None, nonce=nonce, key=key
35+
)
3536
data = b'\x00' + ciphertext + nonce
3637
return {'message': encode_base64(data)}
3738

3839

3940
def test_dm_send_from_banned_user(client, blind_user, blind_user2):
4041
blind_user2.ban(banned_by=SystemUser())
4142
r = sogs_post(
42-
client, f'/inbox/{blind_user.session_id}', make_post(b'beep', sender=blind_user2, to=blind_user), blind_user2
43+
client,
44+
f'/inbox/{blind_user.session_id}',
45+
make_post(b'beep', sender=blind_user2, to=blind_user),
46+
blind_user2,
4347
)
4448
assert r.status_code == 403
4549

4650

4751
def test_dm_send_to_banned_user(client, blind_user, blind_user2):
4852
blind_user2.ban(banned_by=SystemUser())
4953
r = sogs_post(
50-
client, f'/inbox/{blind_user2.session_id}', make_post(b'beep', sender=blind_user, to=blind_user2), blind_user
54+
client,
55+
f'/inbox/{blind_user2.session_id}',
56+
make_post(b'beep', sender=blind_user, to=blind_user2),
57+
blind_user,
5158
)
5259
assert r.status_code == 404
5360

@@ -60,11 +67,6 @@ def test_dm_send(client, blind_user, blind_user2):
6067
assert r.status_code == 200
6168
assert len(r.json) == 1
6269
data = r.json[0]
63-
now = time.time()
6470
assert -1 < data.pop('posted_at') - time.time() < 1
65-
assert -1 < data.pop('expires_at') - config.DM_EXPIRY_DAYS*86400 - time.time() < 1
66-
assert data == {
67-
'id': 1,
68-
'message': post['message'],
69-
'sender': blind_user.session_id,
70-
}
71+
assert -1 < data.pop('expires_at') - config.DM_EXPIRY_DAYS * 86400 - time.time() < 1
72+
assert data == {'id': 1, 'message': post['message'], 'sender': blind_user.session_id}

0 commit comments

Comments
 (0)