Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit df59fad

Browse files
committed
Update php
1 parent 6dd17d8 commit df59fad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+5208
-2328
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
/dist
55
/tmp
66
/out-tsc
7-
7+
/vendor
8+
/server/logs
89
# dependencies
910
/node_modules
1011

@@ -41,3 +42,4 @@ testem.log
4142
#System Files
4243
.DS_Store
4344
Thumbs.db
45+
!.gitkeep

app/Acme/Helpers/Mailer.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Acme\Helpers;
6+
7+
use App\Exceptions\GenericException;
8+
use Swift_Mailer;
9+
use Swift_Message;
10+
use Swift_SmtpTransport;
11+
12+
class Mailer
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $host;
18+
/**
19+
* @var string
20+
*/
21+
private $port;
22+
/**
23+
* @var string
24+
*/
25+
private $encryption;
26+
/**
27+
* @var string
28+
*/
29+
private $username;
30+
/**
31+
* @var string
32+
*/
33+
private $password;
34+
/**
35+
* @var string
36+
*/
37+
private $name;
38+
39+
/**
40+
* Mailer constructor.
41+
*
42+
* @param string $host
43+
* @param string $port
44+
* @param string $encryption
45+
* @param string $username
46+
* @param string $password
47+
* @param string $name
48+
*/
49+
public function __construct(string $host, string $port, string $encryption, string $username, string $password, string $name)
50+
{
51+
$this->host = $host;
52+
$this->port = $port;
53+
$this->encryption = $encryption;
54+
$this->username = $username;
55+
$this->password = $password;
56+
$this->name = $name;
57+
}
58+
59+
/**
60+
* Return a Mailer from an array
61+
*
62+
* @param array $smtp
63+
*
64+
* @return Mailer
65+
*/
66+
public static function fromArray(array $smtp): self
67+
{
68+
return new self(
69+
$smtp['host'],
70+
$smtp['port'],
71+
$smtp['encryption'],
72+
$smtp['username'],
73+
$smtp['password'],
74+
$smtp['name']
75+
);
76+
}
77+
78+
/**
79+
* Send an email
80+
*
81+
* @param $subject
82+
* @param array $to
83+
* @param array $cc
84+
* @param $body
85+
*
86+
* @return void
87+
*
88+
* @throws \App\Exceptions\GenericException
89+
*/
90+
public function send($subject, array $to, array $cc, $body)
91+
{
92+
$transport = new Swift_SmtpTransport($this->host, $this->port, $this->encryption);
93+
$transport->setUsername($this->username)
94+
->setPassword($this->password);
95+
96+
$mailer = new Swift_Mailer($transport);
97+
98+
$message = new Swift_Message($subject);
99+
$message->setFrom([$this->username => $this->name])
100+
->setTo($to)
101+
->setCc($cc)
102+
->setBody($body, 'text/html');
103+
104+
if ($mailer->send($message) === 0) {
105+
throw new GenericException('Email not sent', '', 500);
106+
}
107+
}
108+
}

app/Acme/Helpers/Str.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Acme\Helpers;
4+
5+
class Str
6+
{
7+
/**
8+
* Return a random string by a specific length
9+
*
10+
* @param int $length
11+
*
12+
* @return string
13+
*/
14+
public static function random(int $length): string
15+
{
16+
return substr(md5((string)mt_rand()), 0, $length);
17+
}
18+
}

app/Acme/JWT/JWT.php

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Acme\JWT;
6+
7+
use App\Exceptions\JWTException;
8+
use BadMethodCallException;
9+
use InvalidArgumentException;
10+
use Lcobucci\JWT\Builder;
11+
use Lcobucci\JWT\Parser;
12+
use Lcobucci\JWT\Signer\Hmac\Sha256;
13+
use Lcobucci\JWT\ValidationData;
14+
use RuntimeException;
15+
use Slim\Http\Request;
16+
17+
class JWT
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $token;
23+
/**
24+
* @var Sha256
25+
*/
26+
private $sha256;
27+
/**
28+
* @var Parser
29+
*/
30+
private $parser;
31+
/**
32+
* @var Builder
33+
*/
34+
private $builder;
35+
/**
36+
* @var ValidationData
37+
*/
38+
private $validator;
39+
/**
40+
* @var array
41+
*/
42+
private $config = [];
43+
44+
/**
45+
* JWT constructor.
46+
*
47+
* @param Request $request
48+
* @param Builder $builder
49+
* @param Sha256 $sha256
50+
* @param Parser $parser
51+
* @param ValidationData $validator
52+
* @param array $config
53+
*/
54+
public function __construct(Request $request, Builder $builder, Sha256 $sha256, Parser $parser, ValidationData $validator, array $config)
55+
{
56+
$this->config = $config;
57+
$this->sha256 = $sha256;
58+
$this->parser = $parser;
59+
$this->builder = $builder;
60+
$this->token = $request->getHeaderLine($this->config['header-param']);
61+
$this->validator = $validator;
62+
}
63+
64+
/**
65+
* Return an encoded jwt
66+
*
67+
* @param array $data
68+
*
69+
* @return string
70+
*/
71+
public function encode(array $data): string
72+
{
73+
$token = $this->builder->setIssuer($this->config['issuer'])
74+
->setAudience($this->config['audience'])
75+
->setId($this->config['id'], true)
76+
->setIssuedAt(time())
77+
->setNotBefore(time())
78+
->setExpiration(time() + 3600);
79+
foreach ($data as $key => $value) {
80+
$token->set($key, $value);
81+
}
82+
$token->sign($this->sha256, $this->config['sign']);
83+
84+
return $token->getToken()->__toString();
85+
}
86+
87+
/**
88+
* Decode the token and return its content
89+
*
90+
* @return array
91+
*
92+
* @throws JWTException
93+
*/
94+
public function decode(): array
95+
{
96+
try {
97+
$token = $this->parser->parse($this->token);
98+
$this->validator->setIssuer($this->config['issuer']);
99+
$this->validator->setAudience($this->config['audience']);
100+
$this->validator->setId($this->config['id']);
101+
if (!$token->validate($this->validator)) {
102+
throw new JWTException('Invalid Token', 'Error validating the token');
103+
}
104+
if (!$token->verify($this->sha256, $this->config['sign'])) {
105+
throw new JWTException('Invalid Token', 'The token is empty or not encrypted correctly');
106+
}
107+
108+
return $token->getClaims();
109+
} catch (InvalidArgumentException $e) {
110+
throw new JWTException('Invalid Token', $e->getMessage(), $e->getCode());
111+
} catch (BadMethodCallException $e) {
112+
throw new JWTException('Parsing Error', $e->getMessage(), $e->getCode());
113+
} catch (RuntimeException $e) {
114+
throw new JWTException('Parsing Error', $e->getMessage(), $e->getCode());
115+
}
116+
}
117+
}

app/Controllers/AuthController.php

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controllers;
6+
7+
use App\Acme\Helpers\Mailer;
8+
use App\Acme\JWT\JWT;
9+
use App\Exceptions\GenericException;
10+
use App\Responses\ApiResponse;
11+
use App\Services\AuthService;
12+
use App\Transformers\Users\UserTransformer;
13+
use App\Validators\Auth\LoginAuthValidator;
14+
use App\Validators\Auth\RecoveryAuthValidator;
15+
use App\Validators\Auth\ResetAuthValidator;
16+
use Illuminate\Database\Connection;
17+
use Slim\Http\Request;
18+
use Slim\Http\Response;
19+
20+
class AuthController
21+
{
22+
/**
23+
* @var JWT
24+
*/
25+
private $jwt;
26+
/**
27+
* @var ApiResponse
28+
*/
29+
private $apiResponse;
30+
/**
31+
* @var AuthService
32+
*/
33+
private $authService;
34+
35+
public function __construct(ApiResponse $apiResponse, AuthService $authService, JWT $jwt)
36+
{
37+
$this->jwt = $jwt;
38+
$this->apiResponse = $apiResponse;
39+
$this->authService = $authService;
40+
}
41+
42+
/**
43+
* Return the logged user
44+
*
45+
* @param UserTransformer $userTransformer
46+
*
47+
* @return Response
48+
*/
49+
public function logged(UserTransformer $userTransformer): Response
50+
{
51+
$user = $this->jwt->decode();
52+
53+
$data = $userTransformer->item($user);
54+
55+
return $this->apiResponse->success($data);
56+
}
57+
58+
/**
59+
* Authenticate the user and return a JWT
60+
*
61+
* @param Request $request
62+
* @param LoginAuthValidator $validator
63+
*
64+
* @return Response
65+
*/
66+
public function login(Request $request, LoginAuthValidator $validator): Response
67+
{
68+
if (!$validator->validate()) {
69+
return $this->apiResponse->errorValidation($validator->errors());
70+
}
71+
72+
$user = $this->authService->login($request->getParams());
73+
74+
return $this->apiResponse->success($this->jwt->encode($user));
75+
}
76+
77+
/**
78+
* Recover password
79+
*
80+
* @param Request $request
81+
* @param RecoveryAuthValidator $validator
82+
* @param Connection $db
83+
* @param Mailer $mailer
84+
*
85+
* @return Response
86+
*
87+
* @throws GenericException
88+
*/
89+
public function recovery(Request $request, RecoveryAuthValidator $validator, Connection $db, Mailer $mailer): Response
90+
{
91+
if (!$validator->validate()) {
92+
return $this->apiResponse->errorValidation($validator->errors());
93+
}
94+
95+
try {
96+
$db->beginTransaction();
97+
$this->authService->recovery($request->getParam('email'), $mailer);
98+
$db->commit();
99+
} catch (GenericException $e) {
100+
$db->rollBack();
101+
throw $e;
102+
}
103+
104+
return $this->apiResponse->success(['title' => 'Email sent']);
105+
}
106+
107+
/**
108+
* Reset user password
109+
*
110+
* @param Request $request
111+
* @param ResetAuthValidator $validator
112+
*
113+
* @return Response
114+
*/
115+
public function reset(Request $request, ResetAuthValidator $validator): Response
116+
{
117+
if (!$validator->validate()) {
118+
return $this->apiResponse->errorValidation($validator->errors());
119+
}
120+
121+
$this->authService->reset($request->getParams());
122+
123+
return $this->apiResponse->success(['title' => 'Password updated']);
124+
}
125+
}

0 commit comments

Comments
 (0)