Skip to content

Commit

Permalink
Install and configure JWT (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrofeitoza authored and TalysonSoares committed Jul 29, 2024
1 parent ae74a40 commit 06aaac4
Show file tree
Hide file tree
Showing 18 changed files with 388 additions and 143 deletions.
32 changes: 32 additions & 0 deletions app/migrations/Version20240716230312.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20240716230312 extends AbstractMigration
{
public function getDescription(): string
{
return 'Modify column auth_token to table user';
}

public function preUp(Schema $schema): void
{
$this->addSql('ALTER TABLE usr DROP COLUMN auth_token');
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE usr ADD COLUMN auth_token TEXT NULL;');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE usr DROP COLUMN auth_token');
$this->addSql('ALTER TABLE usr ADD auth_token VARCHAR(255) NULL;');
}
}
10 changes: 10 additions & 0 deletions app/src/Application/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public static function getEnvinronment(): string
return $_ENV['APP_ENV'];
}

public static function getApiPrivateKey(): string
{
return file_get_contents(dirname(__DIR__, 3).'/private.key');
}

public static function getApiPublicKey(): string
{
return file_get_contents(dirname(__DIR__, 3).'/pubkey.key');
}

public static function getEnvData(): mixed
{
return dirname(__DIR__, 2).'/.env';
Expand Down
9 changes: 8 additions & 1 deletion app/src/Controller/Api/AuthApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace App\Controller\Api;

use App\Application\Environment;
use App\Exception\InvalidCredentialsException;
use App\Request\AuthRequest;
use App\Service\Interface\UserServiceInterface;
use Firebase\JWT\JWT;
use Symfony\Component\HttpFoundation\JsonResponse;

class AuthApiController extends AbstractApiController
Expand All @@ -33,7 +35,12 @@ public function auth(): JsonResponse
throw new InvalidCredentialsException();
}

$user->setAuthToken('1q2w3e'.substr(microtime(), 0, 8).$user->id);
$payload = [
'email' => $request['email'],
];
$jwt = JWT::encode($payload, Environment::getApiPrivateKey(), 'RS256');

$user->setAuthToken($jwt);
$this->userService->save($user);

return new JsonResponse([
Expand Down
31 changes: 31 additions & 0 deletions app/src/Enum/SocialMediaEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Enum;

enum SocialMediaEnum: string
{
case FACEBOOK = 'facebook.com';
case INSTAGRAM = 'instagram.com';
case LINKEDIN = 'linkedin.com';
case PINTEREST = 'pinterest.com';
case SPOTIFY = 'spotify.com';
case X = 'x.com';
case VIMEO = 'vimeo.com';
case YOUTUBE = 'youtube.com';
case TIKTOK = 'tiktok.com';

public function getParsingRegex(): string
{
return match ($this) {
self::FACEBOOK, self::INSTAGRAM, self::PINTEREST, self::VIMEO => "/(?:{$this->value}\/(?:profile\.php\?id=)?|^)([\w\d\.]+)$/i",

self::X, self::YOUTUBE, self::TIKTOK => "/(?:{$this->value}\/|^)(@?[\w\d\.]+)$/i",

self::LINKEDIN => "/(?:{$this->value}\/in\/|^)([\w\d-]+)$/i",

self::SPOTIFY => "/(?:open\.)?spotify\.com\/(?:intl-\w+\/)?(?:(user|artist)\/([\w\d]+))|^(user|artist)\/([\w\d]+)$/i",
};
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"doctrine/annotations": "^2.0",
"doctrine/dbal": "^3.6",
"doctrine/orm": "2.16.*",
"firebase/php-jwt": "^6.4",
"firebase/php-jwt": "^6.10",
"michelf/php-markdown": "1.*",
"monolog/monolog": "^3.4",
"mustache/mustache": "^2.11",
Expand Down
20 changes: 10 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 45 additions & 20 deletions src/conf/agent-types.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use MapasCulturais\Utils;
use App\Enum\SocialMediaEnum;
use MapasCulturais\Utils;

/**
* See https://github.com/Respect/Validation to know how to write validations
Expand Down Expand Up @@ -433,48 +434,60 @@
'type' => "socialMedia",
'label' => \MapasCulturais\i::__('Facebook'),
'serialize' =>function($value){
return Utils::parseSocialMediaUser('facebook.com', $value);
return Utils::parseSocialMediaUser(SocialMediaEnum::FACEBOOK, $value);
},
'validations' => array(
"v::oneOf(v::urlDomain('facebook.com'), v::regex('/^@?([\w\d\.]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL válida ou o nome ou id do usuário.")
),
'placeholder' => "nomedousuario ou iddousuario",
'placeholder' => "Url ou id do usuario",
'available_for_opportunities' => true
),
'twitter' => array(
'type' => "socialMedia",
'label' => \MapasCulturais\i::__('Twitter'),
'label' => \MapasCulturais\i::__('X'),
'serialize' =>function($value){
return Utils::parseSocialMediaUser('twitter.com', $value);
return Utils::parseSocialMediaUser(SocialMediaEnum::X, $value);
},
'validations' => array(
"v::oneOf(v::urlDomain('twitter.com'), v::regex('/^@?([\w\d\.]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL ou usuário válido.")
"v::oneOf(v::urlDomain('x.com'), v::regex('/^@?([\w\d\.]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL ou usuário válido.")
),
'placeholder' => "nomedousuario",
'placeholder' => "Url ou id do usuario",
'available_for_opportunities' => true
),
'x' => array(
'type' => "socialMedia",
'label' => \MapasCulturais\i::__('X'),
'serialize' =>function($value){
return Utils::parseSocialMediaUser(SocialMediaEnum::X, $value);
},
'validations' => array(
"v::oneOf(v::urlDomain('x.com'), v::regex('/^@?([\w\d\.]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL ou usuário válido.")
),
'placeholder' => "Url ou id do usuario",
'available_for_opportunities' => true
),
'instagram' => array(
'type' => "socialMedia",
'label' => \MapasCulturais\i::__('Instagram'),
'available_for_opportunities' => true,
'serialize' =>function($value){
return Utils::parseSocialMediaUser('instagram.com', $value);
return Utils::parseSocialMediaUser(SocialMediaEnum::INSTAGRAM, $value);
},
'validations' => array(
"v::oneOf(v::urlDomain('instagram.com'), v::regex('/^@?([\w\d\.]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL ou usuário válido.")
),
'placeholder' => "nomedousuario",
'placeholder' => "Url ou id do usuario",
),
'linkedin' => array(
'type' => "socialMedia",
'label' => \MapasCulturais\i::__('Linkedin'),
'serialize' =>function($value){
return Utils::parseSocialMediaUser('linkedin.com', $value);
return Utils::parseSocialMediaUser(SocialMediaEnum::LINKEDIN, $value);
},
'validations' => array(
"v::oneOf(v::urlDomain('linkedin.com'), v::regex('/^@?([\w\d\.]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL ou usuário válido.")
),
'placeholder' => "nomedousuario",
'placeholder' => "Url ou id do usuario",
'available_for_opportunities' => true
),
'vimeo' => array(
Expand All @@ -484,21 +497,21 @@
"v::oneOf(v::urlDomain('vimeo.com'), v::regex('/^@?([\w\d\.]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL ou usuário válido.")
),
'serialize' =>function($value){
return Utils::parseSocialMediaUser('vimeo.com', $value);
return Utils::parseSocialMediaUser(SocialMediaEnum::VIMEO, $value);
},
'placeholder' => "nomedousuario",
'placeholder' => "Url ou id do usuario",
'available_for_opportunities' => true
),
'spotify' => array(
'type' => "socialMedia",
'label' => \MapasCulturais\i::__('Spotify'),
'validations' => array(
"v::oneOf(v::urlDomain('spotify.com'), v::regex('/^@?([\w\d\.]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL ou usuário válido.")
"v::oneOf(v::urlDomain('spotify.com'), v::regex('/(?:open\.)?spotify\.com\/(?:intl-\w+\/)?(?:(user|artist)\/([\w\d]+))|^(user|artist)\/([\w\d]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL ou usuário válido.")
),
'serialize' =>function($value){
return Utils::parseSocialMediaUser('spotify.com', $value);
return Utils::parseSocialMediaUser(SocialMediaEnum::SPOTIFY, $value);
},
'placeholder' => "nomedousuario",
'placeholder' => "Url ou user/id ou artist/id",
'available_for_opportunities' => true
),
'youtube' => array(
Expand All @@ -508,9 +521,9 @@
"v::oneOf(v::urlDomain('youtube.com'), v::regex('/^@?([\w\d\.]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL ou usuário válido.")
),
'serialize' =>function($value){
return Utils::parseSocialMediaUser('youtube.com', $value);
return Utils::parseSocialMediaUser(SocialMediaEnum::YOUTUBE, $value);
},
'placeholder' => "iddocanal",
'placeholder' => "Url ou id do canal",
'available_for_opportunities' => true
),
'pinterest' => array(
Expand All @@ -520,9 +533,21 @@
"v::oneOf(v::urlDomain('pinterest.com'), v::regex('/^@?([\w\d\.]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL ou usuário válido.")
),
'serialize' =>function($value){
return Utils::parseSocialMediaUser('pinterest.com', $value);
return Utils::parseSocialMediaUser(SocialMediaEnum::PINTEREST, $value);
},
'placeholder' => "Url ou id do usuario",
'available_for_opportunities' => true
),
'tiktok' => array(
'type' => "socialMedia",
'label' => \MapasCulturais\i::__('TikTok'),
'validations' => array(
"v::oneOf(v::urlDomain('tiktok.com'), v::regex('/^@?([\w\d\.]+)$/i'))" => \MapasCulturais\i::__("O valor deve ser uma URL ou usuário válido.")
),
'serialize' =>function($value){
return Utils::parseSocialMediaUser(SocialMediaEnum::TIKTOK, $value);
},
'placeholder' => "nomedousuario",
'placeholder' => "Url ou id do usuario",
'available_for_opportunities' => true
),
// DADOS BANCÁRIOS
Expand Down
Loading

0 comments on commit 06aaac4

Please sign in to comment.