Completed
Push — main ( 02b4a5...a91cd2 )
by Dylan
02:26 queued 02:26
created

Auth::process()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 30
rs 9.9
1
<?php
2
3
namespace Examples\Controllers;
4
5
use Examples\Models\Store;
6
use Lifeboat\App;
7
use Lifeboat\Exceptions\OAuthException;
8
9
/**
10
 * Class Auth
11
 * @package Examples\Controllers
12
 *
13
 * An example controller as how to handle the Lifeboat Auth process
14
 *
15
 * BEFORE YOU START
16
 * You will need to register your app with Lifeboat team.
17
 * Contact [email protected] to get your app credentials.
18
 *
19
 * This controller is designed to show how your app will authenticate with Lifeboat APIs
20
 * and allow your app to interact with Lifeboat APIs where the logged in user permissions
21
 * are automatically checked at an API level.
22
 *
23
 * Base url: /auth
24
 *
25
 * /auth/process
26
 * @see Auth::process()
27
 * This controller action shows how to handle the response from the Lifeboat Auth,
28
 * using the Lifeboat SDK
29
 *
30
 * /auth/error
31
 * @see Auth::error()
32
 * This controller action shows how to handle Lifeboat Auth errors
33
 */
34
class Auth extends Controller {
0 ignored issues
show
Bug introduced by
The type Examples\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
36
    const LIFEBOAT_APP_ID       = '[[Lifeboat App ID]]';
37
    const LIFEBOAT_APP_SECRET   = '[[Lifeboat App Secret]]';
38
39
    private static $url_segment     = 'auth';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
40
    private static $allowed_actions = ['process', 'error'];
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
41
42
    /** @var \Lifeboat\App $app */
43
    private static $_app;
44
45
    /**
46
     * Process the code returns by the Lifeboat Auth process
47
     * and ensure the user has selected an active site
48
     */
49
    public function process()
50
    {
51
        // It's essential for the app to run correctly that sessions
52
        // are started and working
53
        if (session_status() !== PHP_SESSION_ACTIVE) {
54
            session_start();
55
        }
56
57
        // This function will automatically create an access token
58
        // and save it into $_SESSIONS
59
        try {
60
            self::get_app()->fetchAccessToken($_GET['code'] ?? '');
61
        } catch (OAuthException $e) {
62
            error_log($e);
63
            $this->reloadAuth();
64
        }
65
66
        /**
67
         * OPTIONAL:
68
         * If you need to perform actions off-session (cron, etc...)
69
         * You'll need to store the host and site_key
70
         * @see Store::find_or_make()
71
         * @see App::setActiveSite()
72
         * @see App::getAccessToken()
73
         */
74
        Store::find_or_make(self::get_app()->getSiteKey(), self::get_app()->getHost());
0 ignored issues
show
Bug introduced by
It seems like self::get_app()->getHost() can also be of type null; however, parameter $host of Examples\Models\Store::find_or_make() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        Store::find_or_make(self::get_app()->getSiteKey(), /** @scrutinizer ignore-type */ self::get_app()->getHost());
Loading history...
Bug introduced by
It seems like self::get_app()->getSiteKey() can also be of type null; however, parameter $site_key of Examples\Models\Store::find_or_make() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        Store::find_or_make(/** @scrutinizer ignore-type */ self::get_app()->getSiteKey(), self::get_app()->getHost());
Loading history...
75
76
        header("Location: /");
77
        flush();
78
        die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
79
    }
80
81
    public function reloadAuth()
82
    {
83
        // URL to process the auth response
84
        $process = '/auth/process';
85
86
        // URL to handle auth errors
87
        $error = '/auth/error';
88
89
        // A one-time use challenge code to prevent man in the middle attacks
90
        $challenge = self::get_app()->getAPIChallenge();
91
92
        // Redirect to the auth URL
93
        header("Location: " . self::get_app()->getAuthURL($process, $error, $challenge));
94
        flush();
95
        die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
96
    }
97
98
99
    /**
100
     * @return App
101
     */
102
    public static function get_app(): App
103
    {
104
        if (!self::$_app) {
105
            self::$_app = new App(self::LIFEBOAT_APP_ID, self::LIFEBOAT_APP_SECRET);
106
        }
107
108
        return self::$_app;
109
    }
110
}
111