Skip to content

Commit

Permalink
refactoring SCRAM-SHA-256 Authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
nakagami committed Feb 11, 2024
1 parent 9257c1c commit 552fbfa
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions micropg.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,11 @@ def _process_messages(self, obj):
client_nonce = ''.join(printable[random.getrandbits(6)] for i in range(24))

# send client first message
first_message = 'n,,n=,r=' + client_nonce
client_first_message = 'n,,n=,r=' + client_nonce
self._send_data(b'p', b''.join([
b'SCRAM-SHA-256\x00',
_bint_to_bytes(len(first_message)),
first_message.encode('utf-8')
_bint_to_bytes(len(client_first_message)),
client_first_message.encode('utf-8')
]))

code = ord(self._read(1))
Expand All @@ -446,6 +446,7 @@ def _process_messages(self, obj):
data = self._read(ln)
_bytes_to_bint(data[:4]) == 11 # SCRAM first

# recv server first message
server = {
kv[0]: kv[2:]
for kv in data[4:].decode('utf-8').split(',')
Expand Down Expand Up @@ -483,9 +484,11 @@ def _process_messages(self, obj):
)
if proof[-1:] == b'\n':
proof = proof[:-1]
proof = proof.decode('utf-8')
client_final_message = client_final_message_without_proof + ",p=" + proof
self._send_data(
b'p',
(client_final_message_without_proof + ",p=").encode('utf-8') + proof
client_final_message.encode('utf-8')
)

code = ord(self._read(1))
Expand Down

0 comments on commit 552fbfa

Please sign in to comment.