Completed
Pull Request — dev (#321)
by Tristan
06:34
created

RequestTokenParser::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 14
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
namespace App\Services\Auth;
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
4
use Illuminate\Auth\AuthenticationException;
5
use Illuminate\Support\Facades\Cookie;
6
use Illuminate\Http\Request;
7
//use Lcobucci\JWT\Token;
8
//use Lcobucci\JWT\Token\Parser;
9
use Lcobucci\JWT\Token;
10
use Lcobucci\JWT\Parser;
11
use Barryvdh\Debugbar\Facade as Debugbar;
12
13
/**
14
 * Adapted from the OpenIDConnect Laravel package at
15
 * https://github.com/furdarius/oidconnect-laravel
16
 */
0 ignored issues
show
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
17
class RequestTokenParser
18
{
19
    const AUTH_HEADER = "Authorization";
20
    const COOKIE_KEY = "id_token";
21
22
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
     * @var Parser
24
     */
25
    private $parser;
0 ignored issues
show
Coding Style introduced by
Private member variable "parser" must be prefixed with an underscore
Loading history...
26
    /**
27
     * TokenMiddleware constructor.
28
     *
29
     * @param Parser         $parser
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 9 found
Loading history...
30
     */
31
    public function __construct(
32
        Parser $parser
33
    ) {
34
        $this->parser = $parser;
35
    }
36
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
     * @param Request $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
38
     *
39
     * @return Token
40
     */
41
    public function parse(Request $request): Token
42
    {
43
//        if ($request->session()->has(static::COOKIE_KEY)) {
44
//            $token = $request->session()->get(static::COOKIE_KEY);
45
//        } else {
46
//            throw new AuthenticationException("Request doesn't contain id token");
47
//        }
48
        $token = $request->cookie(static::COOKIE_KEY);
49
        Debugbar::info("Retrieving token:");
50
        Debugbar::info($token);
51
        if (empty($token)) {
52
            throw new AuthenticationException("Request doesn't contain id token");
53
        }
54
        return $this->parser->parse($token);
55
56
         /*
57
        $bearer = $request->headers->get(static::AUTH_HEADER);
58
        if (empty($bearer)) {
59
            throw new AuthenticationException("Request doesn't contain auth token");
60
        }
61
        $parts = explode(" ", $bearer);
62
        if (count($parts) < 2) {
63
            throw new AuthenticationException("Invalid format of auth header");
64
        }
65
        $jwt = $parts[1];
66
        return $this->parser->parse($jwt);
67
          *
68
          */
69
    }
70
71
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
72
     * @param string $tokenString     *
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
73
     * @return Token
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
74
     */
75
    public function parseFromString(string $tokenString): Token {
76
        return $this->parser->parse($tokenString);
77
    }
78
79
    public function save(Token $token) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function save()
Loading history...
80
        //session([static::COOKIE_KEY => (string)$token]);
81
        //Cookie::queue(static::COOKIE_KEY, (string)$token);
82
        Debugbar::info("Saving token:");
83
        Debugbar::info($token);
84
        cookie()->queue(cookie()->forever(static::COOKIE_KEY, (string)$token));
85
    }
86
87
    public function forget() {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function forget()
Loading history...
88
        //session()->forget(static::COOKIE_KEY);
89
        // if (Cookie::hasQueued(static::COOKIE_KEY)) {
90
        //   Cookie::unqueue(static::COOKIE_KEY);
91
        // }
92
        // Cookie::queue(Cookie::forget(static::COOKIE_KEY));
93
        cookie()->queue(cookie()->forget(static::COOKIE_KEY));
94
    }
95
}
96