OauthTokenGuard   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 20
c 3
b 0
f 0
dl 0
loc 105
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 4 2
A user() 0 4 2
A setUser() 0 3 1
A validate() 0 3 1
A __construct() 0 14 1
A check() 0 3 1
A guest() 0 3 1
1
<?php
2
3
namespace Metaclassing\EnterpriseAuth\Middleware;
4
5
use Illuminate\Contracts\Auth\Authenticatable;
6
use Illuminate\Contracts\Auth\Guard;
7
use Illuminate\Contracts\Auth\UserProvider;
8
use Illuminate\Http\Request;
9
10
class OauthTokenGuard implements Guard
11
{
12
    protected $request;
13
    protected $provider;
14
    protected $user;
15
16
    /**
17
     * Create a new authentication guard.
18
     *
19
     * @param  \Illuminate\Contracts\Auth\UserProvider  $provider
20
     * @param  \Illuminate\Http\Request  $request
21
     * @return void
22
     */
23
    public function __construct(UserProvider $provider, Request $request)
24
    {
25
        \App::singleton(
26
            \Illuminate\Contracts\Debug\ExceptionHandler::class,
27
            \Metaclassing\EnterpriseAuth\Exceptions\Handler::class
28
        );
29
30
        $this->request = $request;
31
        $this->provider = $provider;
32
        $this->user = null;
33
34
        // use the API auth controller helper functions to check the user creds
35
        $apiAuthController = new \Metaclassing\EnterpriseAuth\Controllers\ApiAuthController();
36
        $this->user = $apiAuthController->authenticateRequest($request);
37
    }
38
39
    /**
40
     * Determine if the current user is authenticated.
41
     *
42
     * @return bool
43
     */
44
    public function check()
45
    {
46
        return ! is_null($this->user());
47
    }
48
49
    /**
50
     * Determine if the current user is a guest.
51
     *
52
     * @return bool
53
     */
54
    public function guest()
55
    {
56
        return ! $this->check();
57
    }
58
59
    /**
60
     * Get the currently authenticated user.
61
     *
62
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
63
     */
64
    public function user()
65
    {
66
        if (! is_null($this->user)) {
67
            return $this->user;
68
        }
69
    }
70
71
    /**
72
     * Get the JSON params from the current request.
73
     *
74
     * @return string
75
     */
76
    /*
77
        public function getJsonParams()
78
        {
79
            $jsondata = $this->request->query('jsondata');
80
81
            return (!empty($jsondata) ? json_decode($jsondata, TRUE) : NULL);
82
        }
83
    /**/
84
85
    /**
86
     * Get the ID for the currently authenticated user.
87
     *
88
     * @return string|null
89
     */
90
    public function id()
91
    {
92
        if ($user = $this->user()) {
93
            return $user->getAuthIdentifier();
94
        }
95
    }
96
97
    /**
98
     * Validate a user's credentials.
99
     *
100
     * @return bool
101
     */
102
    public function validate(array $credentials = [])
103
    {
104
        return is_null($this->user);
105
    }
106
107
    /**
108
     * Set the current user.
109
     *
110
     * @param  array $user User info
111
     */
112
    public function setUser(Authenticatable $user)
113
    {
114
        $this->user = $user;
115
    }
116
}
117