forked from calmery/Mizuderu
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'n1215-feature_csrf_protect'
- Loading branch information
Showing
11 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php namespace Mizuderu\Auth; | ||
|
||
class AntiCSRF { | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $salt; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $algorithm; | ||
|
||
/** | ||
* .envの設定から作成 | ||
* @return AntiCSRF | ||
*/ | ||
public static function fromEnv() | ||
{ | ||
return new self(getenv('ANTI_CSRF_SALT')); | ||
} | ||
|
||
/** | ||
* AntiCSRF constructor. | ||
* @param string $salt | ||
* @param string $algorithm | ||
*/ | ||
public function __construct($salt, $algorithm = 'sha256') | ||
{ | ||
if(!is_string($salt) || empty($salt)) { | ||
throw new \InvalidArgumentException('saltは空でない文字列で指定してください。'); | ||
} | ||
|
||
$this->salt = $salt; | ||
$this->algorithm = $algorithm; | ||
} | ||
|
||
/** | ||
* CSRF対策トークンを生成 | ||
* @return string | ||
*/ | ||
public function generateToken() { | ||
if (session_status() !== PHP_SESSION_ACTIVE) { | ||
throw new \BadMethodCallException('セッションがアクティブではありません。'); | ||
} | ||
|
||
return hash($this->algorithm, session_id() . $this->salt); | ||
} | ||
|
||
/** | ||
* CSRF対策トークンの妥当性を検証 | ||
* @param string $token | ||
* @return bool | ||
*/ | ||
public function validate($token) | ||
{ | ||
return $this->generateToken() === $token; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters