Typo; easier QR code png saving

This commit is contained in:
DarkCat09 2022-09-29 19:17:38 +04:00
parent 805addf18a
commit b0fccc34cd
2 changed files with 23 additions and 12 deletions

View file

@ -205,26 +205,19 @@ and a plain secret code.
- Enter this code into your 2FA application
**or** save the QR into a file:
```python
import base64
url = response.get('secret', '')
encoded = url.removeprefix('data:image/png;base64,')
png = base64.b64decode(encoded)
with open('test.png', 'wb') as f:
f.write(png)
>>> qr = response.get('qrcode', '')
>>> at.save_qr(qr, 'test.png')
```
- Confirm:
```python
at.enable_2fa(123456)
>>> at.enable_2fa(123456)
```
Where 123456 is an OTP code from the app.
### Disable 2FA
It's pretty easy:
```python
at.disable_2fa(123456)
>>> at.disable_2fa(123456)
```
And, of course, pass a real OTP code as an argument.

View file

@ -6,6 +6,8 @@ import re
import hashlib
import logging
import base64
from typing import List, Dict, Optional
import lxml.html
@ -398,7 +400,23 @@ class Client:
'GET', sendtoken=True
).json()
def enbale_2fa(self, code: int) -> None:
def save_qr(self, qrcode: str, filename: str) -> None:
"""Writes a 2FA QR code into a png-file
Args:
qrcode (str): Base64 encoded png image from `qrcode_2fa()`
filename (str): Where the QR code image must be saved.
Existing file will be rewritten.
"""
data = qrcode.removeprefix('data:image/png;base64,')
png = base64.b64decode(data)
with open(filename, 'wb') as f:
f.write(png)
def enable_2fa(self, code: int) -> None:
"""Enables Two-Factor Authentication