[email] Use NaCL instead of PyJWT for encoding
This commit is contained in:
parent
09758fa8e8
commit
b8ce2a4843
|
@ -4,10 +4,11 @@
|
||||||
|
|
||||||
from kibicara.platforms.email.model import EmailRecipients, Email
|
from kibicara.platforms.email.model import EmailRecipients, Email
|
||||||
from kibicara.model import Hood
|
from kibicara.model import Hood
|
||||||
from kibicara.platformapi import Censor, Spawner, Message
|
from kibicara.platformapi import Censor, Spawner
|
||||||
from kibicara.email import send_email
|
from kibicara.email import send_email
|
||||||
from kibicara.config import config
|
from kibicara.config import config
|
||||||
import jwt
|
from nacl.encoding import URLSafeBase64Encoder
|
||||||
|
from nacl.secret import SecretBox
|
||||||
|
|
||||||
|
|
||||||
class EmailBot(Censor):
|
class EmailBot(Censor):
|
||||||
|
@ -25,13 +26,15 @@ class EmailBot(Censor):
|
||||||
'email': recipient.email,
|
'email': recipient.email,
|
||||||
'hood': self.model.hood,
|
'hood': self.model.hood,
|
||||||
}
|
}
|
||||||
token = jwt.encode(json, self.model.secret).decode('ascii')
|
secretbox = SecretBox(Email.secret)
|
||||||
|
token = secretbox.encrypt(json, encoder=URLSafeBase64Encoder)
|
||||||
|
asciitoken = token.decode('ascii')
|
||||||
unsubscribe_link = (
|
unsubscribe_link = (
|
||||||
config['root_url']
|
config['root_url']
|
||||||
+ 'api/'
|
+ 'api/'
|
||||||
+ self.model.id
|
+ self.model.id
|
||||||
+ '/email/unsubscribe/'
|
+ '/email/unsubscribe/'
|
||||||
+ token
|
+ asciitoken
|
||||||
)
|
)
|
||||||
message.text += (
|
message.text += (
|
||||||
"\n\n--\nIf you want to stop receiving these mails, "
|
"\n\n--\nIf you want to stop receiving these mails, "
|
||||||
|
|
|
@ -13,7 +13,8 @@ from kibicara.webapi.hoods import get_hood
|
||||||
from ormantic.exceptions import NoMatch
|
from ormantic.exceptions import NoMatch
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from sqlite3 import IntegrityError
|
from sqlite3 import IntegrityError
|
||||||
import jwt
|
from nacl.encoding import URLSafeBase64Encoder
|
||||||
|
from nacl.secret import SecretBox
|
||||||
from os import urandom
|
from os import urandom
|
||||||
|
|
||||||
|
|
||||||
|
@ -70,9 +71,11 @@ async def email_delete(hood=Depends(get_hood)):
|
||||||
|
|
||||||
@hood_router.post('/recipient/')
|
@hood_router.post('/recipient/')
|
||||||
async def email_recipient_create(recipient: Recipient, hood=Depends(get_hood)):
|
async def email_recipient_create(recipient: Recipient, hood=Depends(get_hood)):
|
||||||
token = jwt.encode({'email': recipient.email}, Email.secret).decode('ascii')
|
secretbox = SecretBox(Email.secret)
|
||||||
|
token = secretbox.encrypt({'email': recipient.email,}, encoder=URLSafeBase64Encoder)
|
||||||
|
asciitoken = token.decode('ascii')
|
||||||
confirm_link = (
|
confirm_link = (
|
||||||
config['root_url'] + "api/" + hood.id + "/email/recipient/confirm/" + token
|
config['root_url'] + "api/" + hood.id + "/email/recipient/confirm/" + asciitoken
|
||||||
)
|
)
|
||||||
send_email(
|
send_email(
|
||||||
recipient.email,
|
recipient.email,
|
||||||
|
@ -85,7 +88,8 @@ async def email_recipient_create(recipient: Recipient, hood=Depends(get_hood)):
|
||||||
|
|
||||||
@hood_router.post('/recipient/confirm/{token}')
|
@hood_router.post('/recipient/confirm/{token}')
|
||||||
async def email_recipient_confirm(token, hood=Depends(get_hood)):
|
async def email_recipient_confirm(token, hood=Depends(get_hood)):
|
||||||
json = jwt.decode(token, Email.secret)
|
secretbox = SecretBox(Email.secret)
|
||||||
|
json = secretbox.decrypt(token.encode('ascii'), encoder=URLSafeBase64Encoder)
|
||||||
try:
|
try:
|
||||||
await EmailRecipients.objects.create(hood=hood.id, email=json['email'])
|
await EmailRecipients.objects.create(hood=hood.id, email=json['email'])
|
||||||
return status.HTTP_201_CREATED
|
return status.HTTP_201_CREATED
|
||||||
|
@ -95,7 +99,8 @@ async def email_recipient_confirm(token, hood=Depends(get_hood)):
|
||||||
|
|
||||||
@hood_router.get('/unsubscribe/{token}', status_code=status.HTTP_200_OK)
|
@hood_router.get('/unsubscribe/{token}', status_code=status.HTTP_200_OK)
|
||||||
async def email_recipient_unsubscribe(token, hood=Depends(get_hood)):
|
async def email_recipient_unsubscribe(token, hood=Depends(get_hood)):
|
||||||
json = jwt.decode(token)
|
secretbox = SecretBox(Email.secret)
|
||||||
|
json = secretbox.decrypt(token.encode('ascii'), encoder=URLSafeBase64Encoder)
|
||||||
if hood.id is not json['hood']:
|
if hood.id is not json['hood']:
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
await EmailRecipients.objects.delete_many(hood=json['hood'], email=json['email'])
|
await EmailRecipients.objects.delete_many(hood=json['hood'], email=json['email'])
|
||||||
|
|
Loading…
Reference in a new issue