Skip to content

Commit 38e6b05

Browse files
committed
Added punny code encoding (#16)
For email fields on - Users - OTP _ Registration Request Signed-off-by: smarcet@gmail.com <smarcet@gmail.com> Change-Id: I5f35ab0cbb38f27712babe3385aa9172cf5b0a4e Signed-off-by: smarcet@gmail.com <smarcet@gmail.com> Added log info Signed-off-by: smarcet@gmail.com <smarcet@gmail.com> Change-Id: I6f8db9443f388731249d1627032b4efa7e8b9cf3 Fix on punny code Signed-off-by: smarcet@gmail.com <smarcet@gmail.com> Change-Id: Ia7ea361e4a75a54650ea15d36990c4f208ef8cec
1 parent b3cd722 commit 38e6b05

23 files changed

+438
-81
lines changed

app/Http/Controllers/Auth/EmailVerificationController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* limitations under the License.
1313
**/
1414
use App\Http\Controllers\Controller;
15+
use App\libs\Utils\EmailUtils;
1516
use App\Services\Auth\IUserService;
1617
use Illuminate\Support\Facades\Log;
1718
use Illuminate\Support\Facades\Redirect;
@@ -46,7 +47,7 @@ public function showVerificationForm(LaravelRequest $request)
4647
$params = ['email' => ''];
4748
if($request->has("email")){
4849
$email = trim($request->get("email"));
49-
if (filter_var($email, FILTER_VALIDATE_EMAIL) !== FALSE) {
50+
if (EmailUtils::isValidEmail($email)) {
5051
$params['email'] = $email;
5152
}
5253
}

app/Http/Controllers/Auth/ForgotPasswordController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
**/
1414

1515
use App\Http\Controllers\Controller;
16+
use App\libs\Utils\EmailUtils;
1617
use App\Services\Auth\IUserService;
1718
use Illuminate\Support\Facades\Auth;
1819
use Illuminate\Support\Facades\Log;
@@ -68,7 +69,7 @@ public function showLinkRequestForm(LaravelRequest $request)
6869

6970
if($request->has("email")){
7071
$email = trim($request->get("email"));
71-
if (filter_var($email, FILTER_VALIDATE_EMAIL) !== FALSE) {
72+
if (EmailUtils::isValidEmail($email)) {
7273
$params['email'] = $email;
7374
}
7475
}
Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace App\Http\Middleware;
22
/**
3-
* Copyright 2015 OpenStack Foundation
3+
* Copyright 2022 OpenStack Foundation
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
@@ -11,18 +11,15 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14-
1514
use Closure;
16-
use Log;
17-
15+
use Illuminate\Support\Facades\Log;
1816
/**
1917
* Class ETagsMiddleware
2018
* @package App\Http\Middleware
2119
*/
2220
final class ETagsMiddleware
2321
{
2422

25-
2623
/**
2724
* Handle an incoming request.
2825
* @param \Illuminate\Http\Request $request
@@ -31,21 +28,41 @@ final class ETagsMiddleware
3128
*/
3229
public function handle($request, Closure $next)
3330
{
31+
// Handle request
32+
$method = $request->getMethod();
33+
34+
// Support using HEAD method for checking If-None-Match
35+
if ($request->isMethod('HEAD')) {
36+
$request->setMethod('GET');
37+
}
38+
//Handle response
3439
$response = $next($request);
40+
3541
if ($response->getStatusCode() === 200 && $request->getMethod() === 'GET')
3642
{
37-
$etag = md5($response->getContent());
43+
$etag = md5($response->getContent());
3844
$requestETag = str_replace('"', '', $request->getETags());
3945
$requestETag = str_replace('-gzip', '', $requestETag);
46+
if($requestETag && is_array($requestETag))
47+
Log::debug(sprintf("ETagsMiddleware::handle requestEtag %s calculated etag %s", $requestETag[0], $etag));
4048

41-
if ($requestETag && $requestETag[0] == $etag)
42-
{
43-
Log::debug('ETAG 304');
49+
// Strip W/ if weak comparison algorithm can be used
50+
$requestETag = array_map([$this, 'stripWeakTags'], $requestETag);
51+
52+
if (in_array($etag, $requestETag)) {
4453
$response->setNotModified();
4554
}
55+
4656
$response->setEtag($etag);
4757
}
4858

59+
$request->setMethod($method);
60+
4961
return $response;
5062
}
63+
64+
private function stripWeakTags($etag)
65+
{
66+
return str_replace('W/', '', $etag);
67+
}
5168
}

app/Http/Utils/Filters/Filter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
15+
use App\libs\Utils\PunnyCodeHelper;
1416
use Doctrine\Common\Collections\Criteria;
1517
use Doctrine\ORM\QueryBuilder;
1618
use Illuminate\Support\Facades\Validator;
@@ -349,7 +351,7 @@ public function apply2Query(QueryBuilder $query, array $mappings)
349351
* @param string $original_format
350352
* @return mixed
351353
*/
352-
private function convertValue($value, $original_format)
354+
private function convertValue(string $value, string $original_format)
353355
{
354356
switch ($original_format) {
355357
case 'datetime_epoch':
@@ -360,8 +362,10 @@ private function convertValue($value, $original_format)
360362
return intval($value);
361363
break;
362364
case 'json_string':
363-
return sprintf("%s",$value);
365+
return sprintf("%s", $value);
364366
break;
367+
case 'json_email':
368+
return PunnyCodeHelper::encodeEmail($value);
365369
default:
366370
return $value;
367371
break;

app/Http/Utils/ParseMultiPartFormDataInputStream.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,13 @@ private function decide($string)
158158
private function file($string)
159159
{
160160
preg_match('/name=\"([^\"]*)\".*stream[\n|\r]+([^\n\r].*)?$/s', $string, $match);
161-
return [
162-
$match[1] => ($match[2] !== NULL ? $match[2] : '')
163-
];
161+
if(count($match) >=2 ) {
162+
return [
163+
$match[1] => ($match[2] !== NULL ? $match[2] : '')
164+
];
165+
}
166+
Log::warning(sprintf( "ParseMultiPartFormDataInputStream::file %s", $string));
167+
return [];
164168
}
165169

166170
/**

app/ModelSerializers/AbstractSerializer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use models\utils\IEntity;
1515
use OAuth2\IResourceServerContext;
1616
use Utils\JsonUtils;
17+
use Illuminate\Support\Facades\Log;
1718
/**
1819
* Class AbstractSerializer
1920
* @package App\ModelSerializers
@@ -143,8 +144,11 @@ public function serialize($expand = null, array $fields = [], array $relations =
143144
if(count($fields) > 0 && !in_array($mapping[0], $fields)) continue;
144145
$value = null;
145146
foreach($method_prefix as $prefix){
147+
Log::debug(sprintf("AbstractSerializer::serialize prefix %s attribute %s", $prefix, $attribute));
146148
if(method_exists($this->object, $prefix.$attribute)){
147-
$value = call_user_func([$this->object, $prefix.$attribute ]);
149+
$value = call_user_func([$this->object, $prefix.$attribute]);
150+
if(is_string($value))
151+
Log::debug(sprintf("AbstractSerializer::serialize prefix %s attribute %s value %s", $prefix, $attribute, $value));
148152
break;
149153
}
150154
}

app/Models/OAuth2/OAuth2OTP.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
15+
use App\libs\Utils\PunnyCodeHelper;
1416
use App\Models\Utils\BaseEntity;
1517
use Doctrine\ORM\Mapping AS ORM;
1618
use DateTime;
@@ -192,15 +194,15 @@ public function setScope(?string $scope): void
192194
*/
193195
public function getEmail(): ?string
194196
{
195-
return $this->email;
197+
return PunnyCodeHelper::decodeEmail($this->email);
196198
}
197199

198200
/**
199201
* @param string $email
200202
*/
201203
public function setEmail(?string $email): void
202204
{
203-
$this->email = !empty($email) ? strtolower(trim($email)):null;
205+
$this->email = PunnyCodeHelper::encodeEmail($email);
204206
}
205207

206208
/**
@@ -361,7 +363,7 @@ public function isValid():bool{
361363
}
362364

363365
public function getUserName():?string{
364-
return $this->connection == OAuth2Protocol::OAuth2PasswordlessEmail ? $this->email : $this->phone_number;
366+
return $this->connection == OAuth2Protocol::OAuth2PasswordlessEmail ? $this->getEmail() : $this->phone_number;
365367
}
366368

367369
/**
@@ -402,7 +404,7 @@ public function generateValue(): string
402404
public static function fromRequest(OAuth2AccessTokenRequestPasswordless $request, int $length):OAuth2OTP{
403405
$instance = new self($length);
404406
$instance->connection = $request->getConnection();
405-
$instance->email = $request->getEmail();
407+
$instance->setEmail($request->getEmail());
406408
$instance->phone_number = $request->getPhoneNumber();
407409
$instance->scope = $request->getScopes();
408410
$instance->setValue($request->getOTP());
@@ -420,7 +422,7 @@ public static function fromParams(string $user_name, string $connection, string
420422
$instance = new self(strlen($value));
421423
$instance->connection = $connection;
422424
if($connection == OAuth2Protocol::OAuth2PasswordlessConnectionEmail)
423-
$instance->email = $user_name;
425+
$instance->setEmail($user_name);
424426
if($connection == OAuth2Protocol::OAuth2PasswordlessConnectionEmail)
425427
$instance->phone_number = $user_name;
426428
$instance->setValue($value);

app/Repositories/DoctrineOAuth2OTPRepository.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* limitations under the License.
1313
**/
1414
use App\libs\OAuth2\Repositories\IOAuth2OTPRepository;
15+
use App\libs\Utils\PunnyCodeHelper;
1516
use Models\OAuth2\Client;
1617
use Models\OAuth2\OAuth2OTP;
1718
/**
@@ -46,6 +47,8 @@ public function getLatestByConnectionAndUserNameNotRedeemed
4647
?Client $client
4748
):?OAuth2OTP
4849
{
50+
$user_name = PunnyCodeHelper::encodeEmail($user_name);
51+
4952
$query = $this->getEntityManager()
5053
->createQueryBuilder()
5154
->select("e")
@@ -78,6 +81,8 @@ public function getByUserNameNotRedeemed
7881
?Client $client = null
7982
)
8083
{
84+
$user_name = PunnyCodeHelper::encodeEmail($user_name);
85+
8186
$query = $this->getEntityManager()
8287
->createQueryBuilder()
8388
->select("e")

app/Repositories/DoctrineUserRegistrationRequestRepository.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
**/
1414
use App\libs\Auth\Models\UserRegistrationRequest;
1515
use App\libs\Auth\Repositories\IUserRegistrationRequestRepository;
16+
use App\libs\Utils\PunnyCodeHelper;
1617
use utils\DoctrineCaseFilterMapping;
1718
use utils\DoctrineSwitchFilterMapping;
1819
/**
1920
* Class DoctrineUserRegistrationRequestRepository
2021
* @package App\Repositories
2122
*/
2223
final class DoctrineUserRegistrationRequestRepository
23-
extends ModelDoctrineRepository implements IUserRegistrationRequestRepository
24+
extends ModelDoctrineRepository
25+
implements IUserRegistrationRequestRepository
2426
{
2527

2628
/**
@@ -81,7 +83,7 @@ public function getByHash(string $hash): ?UserRegistrationRequest
8183
public function getByEmail(string $email): ?UserRegistrationRequest
8284
{
8385
return $this->findOneBy([
84-
'email' => strtolower(trim($email))
86+
'email' => PunnyCodeHelper::encodeEmail($email)
8587
]);
8688
}
8789
}

app/Repositories/DoctrineUserRepository.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
15+
use App\libs\Utils\PunnyCodeHelper;
1416
use Auth\Repositories\IUserRepository;
1517
use Auth\User;
1618
use utils\DoctrineFilterMapping;
@@ -49,8 +51,8 @@ protected function getFilterMappings()
4951
'last_name' => 'e.last_name:json_string',
5052
'full_name' => new DoctrineFilterMapping("concat(e.first_name, ' ', e.last_name) :operator :value"),
5153
'github_user' => 'e.github_user:json_string',
52-
'email' => ['e.email:json_string', 'e.second_email:json_string', 'e.third_email:json_string'],
53-
'primary_email' => 'e.email:json_string',
54+
'email' => ['e.email:json_email', 'e.second_email:json_email', 'e.third_email:json_email'],
55+
'primary_email' => 'e.email:json_email',
5456
'active' => 'e.active:json_boolean',
5557
'group_id' => new DoctrineJoinFilterMapping('e.groups', "g", "g.id :operator :value")
5658
];
@@ -87,12 +89,14 @@ public function getByToken(string $token): ?User
8789
*/
8890
public function getByEmailOrName(string $term): ?User
8991
{
92+
$term = PunnyCodeHelper::encodeEmail($term);
93+
9094
return $this->getEntityManager()
9195
->createQueryBuilder()
9296
->select("e")
9397
->from($this->getBaseEntity(), "e")
9498
->Where("e.email = (:term)")
95-
->setParameter("term", trim($term))
99+
->setParameter("term", $term)
96100
->getQuery()
97101
->getOneOrNullResult();
98102
}

0 commit comments

Comments
 (0)