Passed
Push — bugfix/relatives_not_saving ( 6485fa...f1e1c5 )
by Tristan
13:39
created

RequestTokenParser::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace App\Services\Auth;
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
12
/**
13
 * Adapted from the OpenIDConnect Laravel package at
14
 * https://github.com/furdarius/oidconnect-laravel
15
 */
16
class RequestTokenParser
17
{
18
    const AUTH_HEADER = "Authorization";
19
    const COOKIE_KEY = "id_token";
20
21
    /**
22
     * @var Parser
23
     */
24
    private $parser;
25
    /**
26
     * TokenMiddleware constructor.
27
     *
28
     * @param Parser         $parser
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 9 found
Loading history...
29
     */
30
    public function __construct(
31
        Parser $parser
32
    ) {
33
        $this->parser = $parser;
34
    }
35
    /**
36
     * @param Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
37
     *
38
     * @return Token
39
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
40
    public function parse(Request $request)
0 ignored issues
show
introduced by
Method \App\Services\Auth\RequestTokenParser::parse() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Token".
Loading history...
41
    {
42
//        if ($request->session()->has(static::COOKIE_KEY)) {
43
//            $token = $request->session()->get(static::COOKIE_KEY);
44
//        } else {
45
//            throw new AuthenticationException("Request doesn't contain id token");
46
//        }
47
        $token = $request->cookie(static::COOKIE_KEY);
48
        if (empty($token)) {
49
            throw new AuthenticationException("Request doesn't contain id token");
50
        }
51
        return $this->parser->parse($token);
0 ignored issues
show
Bug introduced by
It seems like $token can also be of type array; however, parameter $jwt of Lcobucci\JWT\Parser::parse() 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

51
        return $this->parser->parse(/** @scrutinizer ignore-type */ $token);
Loading history...
52
53
         /*
54
        $bearer = $request->headers->get(static::AUTH_HEADER);
55
        if (empty($bearer)) {
56
            throw new AuthenticationException("Request doesn't contain auth token");
57
        }
58
        $parts = explode(" ", $bearer);
59
        if (count($parts) < 2) {
60
            throw new AuthenticationException("Invalid format of auth header");
61
        }
62
        $jwt = $parts[1];
63
        return $this->parser->parse($jwt);
64
          *
65
          */
66
    }
67
68
    /**
69
     * @param string $tokenString     *
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
Coding Style introduced by
Parameter comment must start with a capital letter
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
70
     * @return Token
71
     */
72
    public function parseFromString(string $tokenString) {
0 ignored issues
show
introduced by
Method \App\Services\Auth\RequestTokenParser::parseFromString() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Token".
Loading history...
73
        return $this->parser->parse($tokenString);
74
    }
75
76
    public function save(Token $token) {
1 ignored issue
show
introduced by
Method \App\Services\Auth\RequestTokenParser::save() does not have void return type hint.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function save()
Loading history...
77
        //session([static::COOKIE_KEY => (string)$token]);
78
        //Cookie::queue(static::COOKIE_KEY, (string)$token);
79
        cookie()->queue(cookie()->forever(static::COOKIE_KEY, (string)$token));
80
    }
81
82
    public function forget() {
1 ignored issue
show
introduced by
Method \App\Services\Auth\RequestTokenParser::forget() does not have void return type hint.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function forget()
Loading history...
83
        //session()->forget(static::COOKIE_KEY);
84
        // if (Cookie::hasQueued(static::COOKIE_KEY)) {
85
        //   Cookie::unqueue(static::COOKIE_KEY);
86
        // }
87
        // Cookie::queue(Cookie::forget(static::COOKIE_KEY));
88
        cookie()->queue(cookie()->forget(static::COOKIE_KEY));
89
    }
90
}
91