Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth & CSRF #16

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Auth & CSRF #16

wants to merge 10 commits into from

Conversation

tymoteuszjozwiak
Copy link
Contributor

Zacząłem tworzyć logikę uwierzytelniania, dodałem funkcje walidacji tokenów anty-CSRF, zrobiłem porządek w plikach i wypisałem wszystkie endpointy w readme

Copy link
Member

@pniedzwiedzinski pniedzwiedzinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kilka małych rzeczy do poprawki, ale ważnych

} else {
$items = array();
while ($row = $result->fetch_assoc()) {
$items[$row['id']]['name'] = $row['name'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ja bym zrobił po prostu Array a id byłoby atrybutem obiektu w arrayu
Bo trochę łatwiej na froncie po tym iterować

Ale to taki szczegół i w sumie może być jak jest

Comment on lines +14 to +20
$item = array();
$row = $result->fetch_assoc();
$item['name'] = $row['name'];
$item['description'] = $row['description'];
$item['image_url'] = $row['image_url'];
$item['order_type'] = $row['order_type'];
$item['contact_info'] = $row['contact_info'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$item = $result->fetch_assoc();

Tak to nie zadziała? :P

Comment on lines +80 to +89
case 0:
$perms = "Użytkownik";
break;
case 1:
$perms = "Redaktor serwisu";
break;
case 2:
$perms = "Administrator";
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
switch ($code){
case 0:
$perms = "Użytkownik";
break;
case 1:
$perms = "Redaktor serwisu";
break;
case 2:
$perms = "Administrator";
break;
$p = ["Użytkownik", "Redaktor serwisu", "Administrator"];
$perms = $p[$code]

Comment on lines +58 to +73
if ($token_age <= $config['site']['csrf_time']){
if ($token == $_SESSION['csrf']) {
unset($_SESSION['csrf']);
unset($_SESSION['csrf_time']);
return true;
} else {
unset($_SESSION['csrf']);
unset($_SESSION['csrf_time']);
return false;
}
} else {
unset($_SESSION['csrf']);
unset($_SESSION['csrf_time']);
return false;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To jest bardzo pogmatwane i można uprościć, na przykład tak:

Suggested change
$token_age = $timestamp - $_SESSION['csrf_time'];
if ($token_age <= $config['site']['csrf_time']){
if ($token == $_SESSION['csrf']) {
unset($_SESSION['csrf']);
unset($_SESSION['csrf_time']);
return true;
} else {
unset($_SESSION['csrf']);
unset($_SESSION['csrf_time']);
return false;
}
} else {
unset($_SESSION['csrf']);
unset($_SESSION['csrf_time']);
return false;
}
$token_age = $timestamp - $_SESSION['csrf_time'];
$r = ($token_age <= $config['site']['csrf_time'] && $token == $_SESSION['csrf']);
unset($_SESSION['csrf']);
unset($_SESSION['csrf_time']);
return $r;

Można nawet bardziej uprościć. Generalnie im mniej kodu tym lepiej, bo:

  1. Mam mniej do czytania na review
  2. Jak się wraca do tego to jest mniej do czytania
  3. Mniej kodu = mniej miejsc gdzie może się coś zepsuć

Comment on lines +39 to +52
$token_age = $timestamp - $_SESSION['csrf_time'];
if ($token_age <= $config['site']['csrf_time']){
return $_SESSION['csrf'];
} else {
$_SESSION['csrf'] = sha1(randomStr().$user_agent);
$_SESSION['csrf_time'] = $timestamp;
return $_SESSION['csrf'];
}
} else {
$_SESSION['csrf'] = sha1(randomStr().$user_agent);
$_SESSION['csrf_time'] = $timestamp;
return $_SESSION['csrf'];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tak jak w verifyCsrf można tu pozbyć się zbędnej logiki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants