Skip to content

Commit

Permalink
Renaming the bundle to avoid collision with Auth0 plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo Giovanetti committed Nov 18, 2020
1 parent 25aa185 commit 7bee10f
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 60 deletions.
Binary file removed Assets/img/auth0.png
Binary file not shown.
25 changes: 14 additions & 11 deletions Config/config.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
<?php
return [
'name' => 'Auth0',
'description' => 'Integración con funcionalidades de Auth0.',
'name' => 'Auth0 Password Reset',
'description' => 'Providing a Password Reset URL Token to send to users. Requires a custom field "auth0userid" for a contact.',
'author' => 'Leo Giovanetti',
'version' => '1.0.0',
'services' => [
'events' => [
'plugin.auth0.configbundle.subscriber' => [
'class' => 'MauticPlugin\MauticAuth0Bundle\EventListener\ConfigSubscriber'
'mautic.auth0.configbundle.subscriber' => [
'class' => 'MauticPlugin\MauticAuth0PasswordResetBundle\EventListener\ConfigSubscriber'
],
'plugin.auth0.emailbundle.subscriber' => [
'class' => 'MauticPlugin\MauticAuth0Bundle\EventListener\EmailSubscriber'
'mautic.auth0.emailbundle.subscriber' => [
'class' => 'MauticPlugin\MauticAuth0PasswordResetBundle\EventListener\EmailSubscriber',
'arguments' => ['mautic.helper.core_parameters']
]
],
'forms' => array(
'plugin.auth0.form' => array(
'class' => 'MauticPlugin\MauticAuth0Bundle\Form\Type\Auth0Type',
'alias' => 'auth0_config'
'plugin.auth0_password_reset.form' => array(
'class' => 'MauticPlugin\MauticAuth0PasswordResetBundle\Form\Type\Auth0Type',
'alias' => 'auth0_password_reset_config'
)
),
],
'parameters' => array(
'auth0_management_api_token' => '',
'auth0_domain_url' => ''
'auth0_domain_url' => '',
'auth0_result_url' => '',
'auth0_client_id' => '',
'auth0_client_secret' => ''
)
];
24 changes: 16 additions & 8 deletions EventListener/ConfigSubscriber.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MauticPlugin\MauticAuth0Bundle\EventListener;
namespace MauticPlugin\MauticAuth0PasswordResetBundle\EventListener;

use Mautic\ConfigBundle\Event\ConfigEvent;
use Mautic\CoreBundle\EventListener\CommonSubscriber;
Expand Down Expand Up @@ -31,9 +31,9 @@ public function onConfigGenerate(ConfigBuilderEvent $event)
{
$event->addForm(
array(
'formAlias' => 'auth0_config',
'formTheme' => 'MauticAuth0Bundle:FormTheme\Config',
'parameters' => $event->getParametersFromConfig('MauticAuth0Bundle')
'formAlias' => 'auth0_password_reset_config',
'formTheme' => 'MauticAuth0PasswordResetBundle:FormTheme\Config',
'parameters' => $event->getParametersFromConfig('MauticAuth0PasswordResetBundle')
)
);
}
Expand All @@ -47,12 +47,20 @@ public function onConfigSave(ConfigEvent $event)
$values = $event->getConfig();

// Manipulate the values
if (!empty($values['auth0_config']['auth0_management_api_token'])) {
$values['auth0_config']['auth0_management_api_token'] = htmlspecialchars($values['auth0_config']['auth0_management_api_token']);
if (!empty($values['auth0_password_reset_config']['auth0_domain_url'])) {
$values['auth0_password_reset_config']['auth0_domain_url'] = htmlspecialchars($values['auth0_password_reset_config']['auth0_domain_url']);
}

if (!empty($values['auth0_config']['auth0_domain_url'])) {
$values['auth0_config']['auth0_domain_url'] = htmlspecialchars($values['auth0_config']['auth0_domain_url']);
if (!empty($values['auth0_password_reset_config']['auth0_result_url'])) {
$values['auth0_password_reset_config']['auth0_result_url'] = htmlspecialchars($values['auth0_password_reset_config']['auth0_result_url']);
}

if (!empty($values['auth0_password_reset_config']['auth0_client_id'])) {
$values['auth0_password_reset_config']['auth0_client_id'] = htmlspecialchars($values['auth0_password_reset_config']['auth0_client_id']);
}

if (!empty($values['auth0_password_reset_config']['auth0_client_secret'])) {
$values['auth0_password_reset_config']['auth0_client_secret'] = htmlspecialchars($values['auth0_password_reset_config']['auth0_client_secret']);
}

// Set updated values
Expand Down
124 changes: 114 additions & 10 deletions EventListener/EmailSubscriber.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
<?php

namespace MauticPlugin\MauticAuth0Bundle\EventListener;
namespace MauticPlugin\MauticAuth0PasswordResetBundle\EventListener;

use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\EmailBundle\EmailEvents;
use Mautic\EmailBundle\Event\EmailBuilderEvent;
use Mautic\EmailBundle\Event\EmailSendEvent;

use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Http\Client\HttpClient;
use Mautic\CoreBundle\Helper\CoreParametersHelper;

/**
* Class EmailSubscriber
*/
class EmailSubscriber extends CommonSubscriber
{
/**
* @var CoreParametersHelper
*/
protected $coreParametersHelper;
protected $auth0ManagementApiToken;

public function __construct(CoreParametersHelper $coreParametersHelper)
{
$this->coreParametersHelper = $coreParametersHelper;
}

/**
* @return array
*/
Expand All @@ -40,7 +48,7 @@ public function onEmailBuild(EmailBuilderEvent $event)
{
// register tokens
$tokens = [
'{password_reset_ticket_url}' => $this->translator->trans('plugin.auth0.password_reset_ticket_url.token'),
'{password_reset_ticket_url}' => $this->translator->trans('plugin.auth0_password_reset.password_reset_ticket_url.token'),
];

if ($event->tokensRequested($tokens)) {
Expand Down Expand Up @@ -74,6 +82,55 @@ public function decodeTokensSend(EmailSendEvent $event)
$this->decodeTokens($event);
}

public function getAuth0Token()
{
$curl = curl_init();

$auth0DomainUrl = $this->coreParametersHelper->getParameter('auth0_domain_url');
$auth0ClientId = $this->coreParametersHelper->getParameter('auth0_client_id');
$auth0ClientSecret = $this->coreParametersHelper->getParameter('auth0_client_secret');

$payload = [
"grant_type" => 'client_credentials',
"client_id" => $auth0ClientId,
"client_secret" => $auth0ClientSecret,
"audience" => 'https://'.$auth0DomainUrl.'/api/v2/'
];

if (!empty($auth0DomainUrl) && !empty($auth0ClientId) && !empty($auth0ClientSecret)) {
curl_setopt_array($curl, [
CURLOPT_URL => "https://".$auth0DomainUrl."/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

if ($err) {
throw new \Exception('Request to Auth0 Management API failed: ' . $err);
} else {
$data = json_decode($response, true);
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200) {
curl_close($curl);
return $data["access_token"];
} else {
throw new \Exception('Request to Auth0 Management API failed: ' . json_encode($data));
}
}
} else {
throw new \Exception('Empty required value: auth0DomainUrl="' . $auth0DomainUrl . '" / auth0ClientId="' . $auth0ClientId . '" / auth0ClientSecret="' . $auth0ClientSecret);
}
}

/**
* Search and replace tokens with content.
*
Expand All @@ -85,8 +142,55 @@ public function decodeTokens(EmailSendEvent $event)
{
$tokens = [];
$lead = $event->getLead();
$content = "THE_URL";
$tokens['{password_reset_ticket_url}'] = $content;
$event->addTokens($tokens);
$lead_auth0_user_id = $lead["auth0userid"];
if(empty($this->auth0ManagementApiToken)) {
$this->auth0ManagementApiToken = $this->getAuth0Token();
}
$auth0DomainUrl = $this->coreParametersHelper->getParameter('auth0_domain_url');
$auth0ResultUrl = $this->coreParametersHelper->getParameter('auth0_result_url');

if (!empty($lead_auth0_user_id) && !empty($this->auth0ManagementApiToken) && !empty($auth0DomainUrl) && !empty($auth0ResultUrl)) {
$curl = curl_init();
$url = "https://" . $auth0DomainUrl . "/api/v2/tickets/password-change";
$payload = [
"result_url" => $auth0ResultUrl,
"user_id" => $lead_auth0_user_id,
"ttl_sec" => 0,
"mark_email_as_verified" => false,
"includeEmailInRedirect" => false
];

curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer " . $this->auth0ManagementApiToken,
"content-type: application/json"
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
throw new \Exception('Request to Auth0 Management API failed: ' . $err);
} else {
$data = json_decode($response, true);
$this->logger->debug("OK Auth0 EmailSubscriber: " . $response);
$content = $data["ticket"];
$tokens['{password_reset_ticket_url}'] = $content;
$event->addTokens($tokens);
}
} else {
throw new \Exception('Empty required value: lead_auth0_user_id="' . $lead_auth0_user_id . '" / auth0ManagementApiToken="' . $this->auth0ManagementApiToken . '" / auth0DomainUrl="' . $auth0DomainUrl . '" / auth0ResultUrl="' . $auth0ResultUrl . '"');
}
}
}
42 changes: 29 additions & 13 deletions Form/Type/Auth0Type.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php

namespace MauticPlugin\MauticAuth0Bundle\Form\Type;
namespace MauticPlugin\MauticAuth0PasswordResetBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
*
Expand All @@ -20,35 +18,53 @@ class Auth0Type extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'auth0_management_api_token',
'textarea',
'auth0_domain_url',
'text',
array(
'label' => 'plugin.auth0.config.auth0_management_api_token',
'data' => $options['data']['auth0_management_api_token'],
'label' => 'plugin.auth0_password_reset.config.auth0_domain_url',
'data' => $options['data']['auth0_domain_url'],
'attr' => array(
'tooltip' => 'plugin.auth0.config.auth0_management_api_token_tooltip',
'tooltip' => 'plugin.auth0_password_reset.config.auth0_domain_url_tooltip',
)
)
);

$builder->add(
'auth0_domain_url',
'auth0_result_url',
'text',
array(
'label' => 'plugin.auth0.config.auth0_domain_url',
'data' => $options['data']['auth0_domain_url'],
'label' => 'plugin.auth0_password_reset.config.auth0_result_url',
'data' => $options['data']['auth0_result_url'],
'attr' => array(
'tooltip' => 'plugin.auth0.config.auth0_domain_url_tooltip',
'tooltip' => 'plugin.auth0_password_reset.config.auth0_result_url_tooltip',
)
)
);

$builder->add(
'auth0_client_id',
'text',
array(
'label' => 'plugin.auth0_password_reset.config.auth0_client_id',
'data' => $options['data']['auth0_client_id'],
)
);

$builder->add(
'auth0_client_secret',
'text',
array(
'label' => 'plugin.auth0_password_reset.config.auth0_client_secret',
'data' => $options['data']['auth0_client_secret'],
)
);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'auth0_config';
return 'auth0_password_reset_config';
}
}
8 changes: 0 additions & 8 deletions MauticAuth0Bundle.php

This file was deleted.

8 changes: 8 additions & 0 deletions MauticAuth0PasswordResetBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace MauticPlugin\MauticAuth0PasswordResetBundle;

use Mautic\PluginBundle\Bundle\PluginBundleBase;

class MauticAuth0PasswordResetBundle extends PluginBundleBase
{
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# MauticAuth0Bundle
# MauticAuth0PasswordResetBundle
16 changes: 9 additions & 7 deletions Translations/en_US/messages.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
plugin.auth0.password_reset_ticket_url.token = "Password Reset Ticket URL"
plugin.auth0_password_reset.password_reset_ticket_url.token = "Password Reset Ticket URL"

mautic.config.tab.auth0_config = "Auth0 Settings"
mautic.config.tab.auth0_password_reset_config = "Auth0 Password Reset Settings"

plugin.auth0.config.auth0_management_api_token = "Auth0 Management API Token"
plugin.auth0.config.auth0_management_api_token_tooltip = "Please read the official docs on how to get it."
plugin.auth0_password_reset.config.auth0_domain_url = "Auth0 Domain URL"
plugin.auth0_password_reset.config.auth0_domain_url_tooltip = "Your Auth0 domain URL (e.g. acme.auth0.com)"

plugin.auth0.config.auth0_domain_url = "Auth0 Domain URL"
plugin.auth0.config.auth0_domain_url_tooltip = "Your Auth0 domain URL (e.g. acme.auth0.com)"
plugin.auth0_password_reset.config.auth0_result_url = "Auth0 Password Ticket Result URL"
plugin.auth0_password_reset.config.auth0_result_url_tooltip = "URL the user will be redirected to once ticket is used."

plugin.auth0.config.auth0_management_api_token.error = "Error generating Password Reset Ticket URL"
plugin.auth0_password_reset.config.auth0_client_id = "Auth0 Client ID"

plugin.auth0_password_reset.config.auth0_client_secret = "Auth0 Client Secret"
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

?>

<div class="panel panel-primary">
<div class="panel panel-warning">
<div class="panel-heading">
<h3 class="panel-title"><?php echo $view['translator']->trans('mautic.config.tab.auth0_config'); ?></h3>
<h3 class="panel-title"><?php echo $view['translator']->trans('plugin.auth0_password_reset.config.tab.auth0_config'); ?></h3>
</div>
<div class="panel-body">
<?php foreach ($form->children as $f): ?>
Expand Down

0 comments on commit 7bee10f

Please sign in to comment.