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

Change from generic to named exceptions #20

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/Branca.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

namespace Branca;

use InvalidArgumentException;
use Tuupola\Base62;

class Branca
Expand Down Expand Up @@ -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)
);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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. */
Expand All @@ -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");
}
}

Expand Down
42 changes: 42 additions & 0 deletions src/ExpiredTokenException.php
Original file line number Diff line number Diff line change
@@ -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
{
}
42 changes: 42 additions & 0 deletions src/InvalidKeyException.php
Original file line number Diff line number Diff line change
@@ -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
{
}
42 changes: 42 additions & 0 deletions src/InvalidTokenException.php
Original file line number Diff line number Diff line change
@@ -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
{
}
42 changes: 42 additions & 0 deletions src/InvalidTokenVersionException.php
Original file line number Diff line number Diff line change
@@ -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
{
}