diff --git a/src/Branca.php b/src/Branca.php index 4c7c098..db7dcf4 100644 --- a/src/Branca.php +++ b/src/Branca.php @@ -35,7 +35,6 @@ namespace Branca; -use InvalidArgumentException; use Tuupola\Base62; class Branca @@ -70,7 +69,7 @@ public function __construct(string $key) } if (SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES !== strlen($key)) { - throw new InvalidArgumentException( + throw new InvalidKeyException( sprintf("Key must be exactly %d bytes", SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES) ); } @@ -132,12 +131,12 @@ public function decode(string $token, int $ttl = null): string /* Unpack failed, should not ever happen. */ if (false === $parts) { - throw new \RuntimeException("Cannot extract token header"); + throw new InvalidTokenException("Cannot extract token header"); } /* Implementation should accept only current version. */ if ($parts["version"] !== self::VERSION) { - throw new \RuntimeException("Invalid token version"); + throw new InvalidTokenVersionException("Invalid token version"); } try { @@ -148,15 +147,15 @@ public function decode(string $token, int $ttl = null): string $this->key ); } catch (\Throwable $error) { - throw new \RuntimeException("Invalid token"); + throw new InvalidTokenException("Invalid token"); /** @phpstan-ignore-next-line */ } catch (\Exception $error) { - throw new \RuntimeException("Invalid token"); + throw new InvalidTokenException("Invalid token"); } /* In some cases sodium returns false. */ if (false === $payload) { - throw new \RuntimeException("Invalid token"); + throw new InvalidTokenException("Invalid token"); } /* Store timestamp value for the helper. */ @@ -166,7 +165,7 @@ public function decode(string $token, int $ttl = null): string if (is_integer($ttl)) { $future = $parts["time"] + $ttl; if ($future < time()) { - throw new \RuntimeException("Token is expired"); + throw new ExpiredTokenException("Token is expired"); } } diff --git a/src/ExpiredTokenException.php b/src/ExpiredTokenException.php new file mode 100644 index 0000000..dc33b3a --- /dev/null +++ b/src/ExpiredTokenException.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types = 1); + +/* + +Copyright (c) 2017-2021 Mika Tuupola + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*/ + +/** + * @see https://branca.io/ + * @see https://github.com/tuupola/branca-php + * @see https://github.com/tuupola/branca-spec + * @license https://www.opensource.org/licenses/mit-license.php + */ + +namespace Branca; + +use RunTimeException; + +class ExpiredTokenException extends RunTimeException +{ +} diff --git a/src/InvalidKeyException.php b/src/InvalidKeyException.php new file mode 100644 index 0000000..0f5eabe --- /dev/null +++ b/src/InvalidKeyException.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types = 1); + +/* + +Copyright (c) 2017-2021 Mika Tuupola + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*/ + +/** + * @see https://branca.io/ + * @see https://github.com/tuupola/branca-php + * @see https://github.com/tuupola/branca-spec + * @license https://www.opensource.org/licenses/mit-license.php + */ + +namespace Branca; + +use InvalidArgumentException; + +class InvalidKeyException extends InvalidArgumentException +{ +} diff --git a/src/InvalidTokenException.php b/src/InvalidTokenException.php new file mode 100644 index 0000000..ee2b100 --- /dev/null +++ b/src/InvalidTokenException.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types = 1); + +/* + +Copyright (c) 2017-2021 Mika Tuupola + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*/ + +/** + * @see https://branca.io/ + * @see https://github.com/tuupola/branca-php + * @see https://github.com/tuupola/branca-spec + * @license https://www.opensource.org/licenses/mit-license.php + */ + +namespace Branca; + +use RunTimeException; + +class InvalidTokenException extends RunTimeException +{ +} diff --git a/src/InvalidTokenVersionException.php b/src/InvalidTokenVersionException.php new file mode 100644 index 0000000..5d7e46f --- /dev/null +++ b/src/InvalidTokenVersionException.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types = 1); + +/* + +Copyright (c) 2017-2021 Mika Tuupola + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*/ + +/** + * @see https://branca.io/ + * @see https://github.com/tuupola/branca-php + * @see https://github.com/tuupola/branca-spec + * @license https://www.opensource.org/licenses/mit-license.php + */ + +namespace Branca; + +use RunTimeException; + +class InvalidTokenVersionException extends InvalidTokenException +{ +}