def post(self, request, format=None):
        # Store the registration information the developer gave us.
        serialized = OAuth2RegistrationSerializer(data=request.data)
        if not serialized.is_valid():
            return Response(
                status=400,
                data=serialized.errors
            )
        else:
            serialized.save()

        # Produce a client ID, client secret, and authorize the application in
        # the OAuth2 backend.
        new_application = ThrottledApplication(
            name=serialized.validated_data['name'],
            skip_authorization=False,
            client_type='Confidential',
            authorization_grant_type='client-credentials',
            verified=False
        )
        new_application.save()
        # Send a verification email.
        verification = OAuth2Verification(
            email=serialized.validated_data['email'],
            code=secrets.token_urlsafe(64),
            associated_application=new_application
        )
        verification.save()
        token = verification.code
        link = request.build_absolute_uri(reverse('verify-email', [token]))
        verification_msg = f"""
To verify your CC Catalog API credentials, click on the following link:

{link}

If you believe you received this message in error, please disregard it.
        """
        try:
            send_mail(
                subject='Verify your API credentials',
                message=verification_msg,
                from_email='*****@*****.**',
                recipient_list=[verification.email],
                fail_silently=False
            )
        except smtplib.SMTPException as e:
            log.error('Failed to send API verification email!')
            log.error(e)
        # Give the user their newly created credentials.
        return Response(
            status=201,
            data={
                'client_id': new_application.client_id,
                'client_secret': new_application.client_secret,
                'name': new_application.name,
                'msg': 'Check your email for a verification link.'
            }
        )
Exemple #2
0
    def post(self, request, format=None):
        # Store the registration information the developer gave us.
        serialized = OAuth2RegistrationSerializer(data=request.data)
        if not serialized.is_valid():
            return Response(status=400, data=serialized.errors)
        else:
            serialized.save()

        # Produce a client ID, client secret, and authorize the application in
        # the OAuth2 backend.
        new_application = ThrottledApplication(
            name=serialized.validated_data['name'],
            skip_authorization=False,
            client_type='Confidential',
            authorization_grant_type='client-credentials')
        new_application.save()
        # Give the user their newly created credentials.
        return Response(status=201,
                        data={
                            'client_id': new_application.client_id,
                            'client_secret': new_application.client_secret,
                            'name': new_application.name
                        })