Merge pull request 'promotion_sendmail' (#172) from promotion_sendmail into release

Reviewed-on: https://gitea.pangea.org/trustchain-oc1-orchestral/IdHub/pulls/172
This commit is contained in:
cayop 2024-03-07 17:06:56 +00:00
commit 7d31b4e205
3 changed files with 86 additions and 8 deletions

View File

@ -1,7 +1,5 @@
import logging import logging
from urllib.parse import urlparse
from django.conf import settings from django.conf import settings
from django.template import loader from django.template import loader
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives

View File

@ -264,9 +264,20 @@ class OAuth2VPToken(models.Model):
self.authorization = Authorization.objects.filter(code=code).first() self.authorization = Authorization.objects.filter(code=code).first()
@property
def code(self):
if not self.authorization:
return ''
return self.authorization.code
def verifing(self): def verifing(self):
self.result_verify = verify_presentation(self.vp_token) self.result_verify = verify_presentation(self.vp_token)
def get_result_verify(self):
if not self.result_verify:
return {}
return json.loads(self.result_verify)
def get_response_verify(self): def get_response_verify(self):
response = { response = {
"verify": ',', "verify": ',',
@ -307,3 +318,4 @@ class OAuth2VPToken(models.Model):
self.user_info = tk.get( self.user_info = tk.get(
"verifiableCredential", [{}] "verifiableCredential", [{}]
)[-1].get("credentialSubject") )[-1].get("credentialSubject")
return json.dumps(self.user_info, indent=2)

View File

@ -1,6 +1,9 @@
import json import json
import base64 import base64
import logging
from django.template import loader
from django.core.mail import EmailMultiAlternatives
from django.conf import settings from django.conf import settings
from django.views.generic.edit import View, FormView from django.views.generic.edit import View, FormView
from django.http import HttpResponse, Http404, JsonResponse from django.http import HttpResponse, Http404, JsonResponse
@ -10,6 +13,7 @@ from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.contrib import messages from django.contrib import messages
from django.contrib.auth import get_user_model
from oidc4vp.models import Authorization, Organization, OAuth2VPToken from oidc4vp.models import Authorization, Organization, OAuth2VPToken
from idhub.mixins import UserView from idhub.mixins import UserView
@ -18,6 +22,11 @@ from idhub.models import Event
from oidc4vp.forms import AuthorizeForm from oidc4vp.forms import AuthorizeForm
User = get_user_model()
logger = logging.getLogger(__name__)
class AuthorizeView(UserView, FormView): class AuthorizeView(UserView, FormView):
title = _("My wallet") title = _("My wallet")
section = "MyWallet" section = "MyWallet"
@ -101,6 +110,10 @@ class AuthorizeView(UserView, FormView):
@method_decorator(csrf_exempt, name='dispatch') @method_decorator(csrf_exempt, name='dispatch')
class VerifyView(View): class VerifyView(View):
subject_template_name = 'email/verify_subject.txt'
email_template_name = 'email/verify_email.txt'
html_email_template_name = 'email/verify_email.html'
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
org = self.validate(request) org = self.validate(request)
presentation_definition = json.dumps(settings.SUPPORTED_CREDENTIALS) presentation_definition = json.dumps(settings.SUPPORTED_CREDENTIALS)
@ -121,19 +134,23 @@ class VerifyView(View):
org = self.validate(request) org = self.validate(request)
vp_token = OAuth2VPToken( self.vp_token = OAuth2VPToken(
vp_token = vp_tk, vp_token = vp_tk,
organization=org, organization=org,
code=code code=code
) )
if not vp_token.authorization:
if not self.vp_token.authorization:
raise Http404("Page not Found!") raise Http404("Page not Found!")
vp_token.verifing() self.vp_token.verifing()
response = vp_token.get_response_verify() response = self.vp_token.get_response_verify()
vp_token.save() self.vp_token.save()
response["response"] = "Validation Code {}".format(code)
for user in User.objects.filter(is_admin=True):
self.send_email(user)
response["response"] = "Validation Code {}".format(code)
return JsonResponse(response) return JsonResponse(response)
def validate(self, request): def validate(self, request):
@ -152,6 +169,57 @@ class VerifyView(View):
raise Http404("Page not Found!") raise Http404("Page not Found!")
def send_email(self, user):
"""
Send a email when a user is activated.
"""
verification = self.vp_token.get_result_verify()
if not verification:
return
if verification.get('errors') or verification.get('warnings'):
return
email = self.get_email(user)
try:
if settings.ENABLE_EMAIL:
email.send()
return
logger.warning(user.email)
logger.warning(email.body)
except Exception as err:
logger.error(err)
return
def get_context(self):
url_domain = "https://{}/".format(settings.DOMAIN)
context = {
"domain": settings.DOMAIN,
"url_domain": url_domain,
"verification": self.get_verification(),
"code": self.vp_token.code,
}
return context
def get_email(self, user):
context = self.get_context()
subject = loader.render_to_string(self.subject_template_name, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
body = loader.render_to_string(self.email_template_name, context)
from_email = settings.DEFAULT_FROM_EMAIL
to_email = user.email
email_message = EmailMultiAlternatives(
subject, body, from_email, [to_email])
html_email = loader.render_to_string(self.html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
return email_message
def get_verification(self):
return self.vp_token.get_user_info()
class AllowCodeView(View): class AllowCodeView(View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):