-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Auth & CSRF #16
Conversation
There was a problem hiding this 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']; |
There was a problem hiding this comment.
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
$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']; |
There was a problem hiding this comment.
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
case 0: | ||
$perms = "Użytkownik"; | ||
break; | ||
case 1: | ||
$perms = "Redaktor serwisu"; | ||
break; | ||
case 2: | ||
$perms = "Administrator"; | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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] |
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; | ||
} |
There was a problem hiding this comment.
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:
$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:
- Mam mniej do czytania na review
- Jak się wraca do tego to jest mniej do czytania
- Mniej kodu = mniej miejsc gdzie może się coś zepsuć
$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']; | ||
} |
There was a problem hiding this comment.
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
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