Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.73% covered (warning)
72.73%
8 / 11
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
IndieAuthException
72.73% covered (warning)
72.73%
8 / 11
50.00% covered (danger)
50.00%
3 / 6
7.99
0.00% covered (danger)
0.00%
0 / 1
 create
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 getStatusCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExplanation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 trustQueryParams
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRequest
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2
3namespace Taproot\IndieAuth;
4
5use Exception;
6use Psr\Http\Message\ServerRequestInterface;
7use Throwable;
8
9class IndieAuthException extends Exception {
10    const INTERNAL_ERROR = 0;
11    const INTERNAL_ERROR_REDIRECT = 1;
12    const AUTHENTICATION_CALLBACK_MISSING_ME_PARAM = 2;
13    const AUTHORIZATION_APPROVAL_REQUEST_MISSING_HASH = 3;
14    const AUTHORIZATION_APPROVAL_REQUEST_INVALID_HASH = 4;
15    const HTTP_EXCEPTION_FETCHING_CLIENT_ID = 5;
16    const INTERNAL_EXCEPTION_FETCHING_CLIENT_ID = 6;
17    const INVALID_REDIRECT_URI = 7;
18    const INVALID_CLIENT_ID = 8;
19    const INVALID_STATE = 9;
20    const INVALID_CODE_CHALLENGE = 10;
21    const INVALID_SCOPE = 11;
22    const INVALID_GRANT = 12;
23    const INVALID_REQUEST = 13;
24    const INVALID_REQUEST_REDIRECT = 14;
25    const AUTHENTICATION_CALLBACK_INVALID_RETURN_VALUE = 15;
26
27    const EXC_INFO = [
28        self::INTERNAL_ERROR => ['statusCode' => 500, 'name' => 'Internal Server Error', 'explanation' => 'An internal server error occurred.'],
29        self::INTERNAL_ERROR_REDIRECT => ['statusCode' => 302, 'name' => 'Internal Server Error', 'error' => 'internal_error'],
30        self::AUTHENTICATION_CALLBACK_INVALID_RETURN_VALUE => ['statusCode' => 302, 'name' => 'Internal Server Error', 'error' => 'internal_error'],
31        self::AUTHENTICATION_CALLBACK_MISSING_ME_PARAM => ['statusCode' => 302, 'name' => 'Internal Server Error', 'error' => 'internal_error'],
32        self::AUTHORIZATION_APPROVAL_REQUEST_MISSING_HASH => ['statusCode' => 302, 'name' => 'Request Missing Hash', 'error' => 'internal_error'],
33        self::AUTHORIZATION_APPROVAL_REQUEST_INVALID_HASH => ['statusCode' => 302, 'name' => 'Request Hash Invalid', 'error' => 'internal_error'],
34        // TODO: should this one be a 500 because it’s an internal server error, or a 400 because the client_id was likely invalid? Is anyone ever going to notice, or care?
35        self::HTTP_EXCEPTION_FETCHING_CLIENT_ID => ['statusCode' => 500, 'name' => 'Error Fetching Client App URL',  'explanation' => 'Fetching the client app (client_id) failed.'],
36        self::INTERNAL_EXCEPTION_FETCHING_CLIENT_ID => ['statusCode' => 500, 'name' => 'Internal Error fetching client app URI', 'explanation' => 'Fetching the client app (client_id) failed due to an internal error.'],
37        self::INVALID_REDIRECT_URI => ['statusCode' => 400, 'name' => 'Invalid Client App Redirect URI', 'explanation' => 'The client app redirect URI (redirect_uri) either was not a valid URI, did not sufficiently match client_id, or did not exactly match any redirect URIs parsed from fetching the client_id.'],
38        self::INVALID_CLIENT_ID => ['statusCode' => 400, 'name' => 'Invalid Client Identifier URI', 'explanation' => 'The Client Identifier was not valid.'],
39        self::INVALID_STATE => ['statusCode' => 302, 'name' => 'Invalid state Parameter', 'error' => 'invalid_request'],
40        self::INVALID_CODE_CHALLENGE => ['statusCode' => 302, 'name' => 'Invalid code_challenge Parameter', 'error' => 'invalid_request'],
41        self::INVALID_SCOPE => ['statusCode' => 302, 'name' => 'Invalid scope Parameter', 'error' => 'invalid_request'],
42        self::INVALID_GRANT => ['statusCode' => 400, 'name' => 'The provided credentials were not valid.', 'error' => 'invalid_grant'],
43        self::INVALID_REQUEST => ['statusCode' => 400, 'name' => 'Invalid Request', 'error' => 'invalid_request'],
44        self::INVALID_REQUEST_REDIRECT => ['statusCode' => 302, 'name' => 'Invalid Request', 'error' => 'invalid_request'],
45    ];
46
47    /** @var ServerRequestInterface $request */
48    protected $request;
49
50    public static function create(int $code, ServerRequestInterface $request, ?Throwable $previous=null): self {
51        // Only accept known codes. Default to 0 (generic internal error) on an unrecognised code.
52        if (!in_array($code, array_keys(self::EXC_INFO))) {
53            $code = 0;
54        }
55        $message = self::EXC_INFO[$code]['name'];
56        $e = new self($message, $code, $previous);
57        $e->request = $request;
58        return $e;
59    }
60
61    public function getStatusCode() {
62        return $this->getInfo()['statusCode'] ?? 500;
63    }
64
65    public function getExplanation() {
66        return $this->getInfo()['explanation'] ?? 'An unknown error occured.';
67    }
68
69    public function getInfo() {
70        return self::EXC_INFO[$this->code] ?? self::EXC_INFO[self::INTERNAL_ERROR];
71    }
72
73    /**
74     * Trust Query Params
75     * 
76     * Only useful on authorization form submission requests. If this returns false,
77     * the client_id and/or request_uri have likely been tampered with, and the error
78     * page SHOULD NOT offer the user a link to them.
79     */
80    public function trustQueryParams() {
81        return !in_array($this->code, [self::AUTHORIZATION_APPROVAL_REQUEST_INVALID_HASH, self::AUTHORIZATION_APPROVAL_REQUEST_MISSING_HASH]);
82    }
83
84    public function getRequest() {
85        return $this->request;
86    }
87}