Passed
Pull Request — master (#108)
by
unknown
05:52
created

Saml2User::getIntendedUrl()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 12
rs 10
1
<?php
2
3
namespace Slides\Saml2;
4
5
use OneLogin\Saml2\Auth as OneLoginAuth;
6
7
/**
8
 * Class Saml2User
9
 *
10
 * @package Slides\Saml2
11
 */
12
class Saml2User
13
{
14
    /**
15
     * OneLogin authentication handler.
16
     *
17
     * @var OneLoginAuth
18
     */
19
    protected $auth;
20
21
    /**
22
     * Saml2User constructor.
23
     *
24
     * @param OneLoginAuth $auth
25
     */
26 3
    public function __construct(OneLoginAuth $auth)
27
    {
28 3
        $this->auth = $auth;
29 3
    }
30
31
    /**
32
     * Get the user ID retrieved from assertion processed this request.
33
     *
34
     * @return string
35
     */
36
    public function getUserId()
37
    {
38
        return $this->auth->getNameId();
39
    }
40
41
    /**
42
     * Get the attributes retrieved from assertion processed this request
43
     *
44
     * @return array
45
     */
46
    public function getAttributes()
47
    {
48
        return $this->auth->getAttributes();
49
    }
50
51
    /**
52
     * Returns the requested SAML attribute
53
     *
54
     * @param string $name The requested attribute of the user.
55
     *
56
     * @return array|null Requested SAML attribute ($name).
57
     */
58 3
    public function getAttribute($name)
59
    {
60 3
        return $this->auth->getAttribute($name);
61
    }
62
    
63
    /**
64
     * The attributes retrieved from assertion processed this request.
65
     *
66
     * @return array
67
     */
68
    public function getAttributesWithFriendlyName()
69
    {
70
        return $this->auth->getAttributesWithFriendlyName();
71
    }
72
73
    /**
74
     * The SAML assertion processed this request.
75
     *
76
     * @return string
77
     */
78
    public function getRawSamlAssertion()
79
    {
80
        return app('request')->input('SAMLResponse'); //just this request
81
    }
82
83
    /**
84
     * Get the intended URL.
85
     *
86
     * @return mixed
87
     */
88
    public function getIntendedUrl()
89
    {
90
        $relayState = app('request')->input('RelayState');
91
92
        $url = app('Illuminate\Contracts\Routing\UrlGenerator');
93
94
        if ($relayState && $url->full() != $relayState) {
95
            return $relayState;
96
        }
97
98
        return null;
99
    }
100
101
    /**
102
     * Parses a SAML property and adds this property to this user or returns the value.
103
     *
104
     * @param string $samlAttribute
105
     * @param string $propertyName
106
     *
107
     * @return array|null
108
     */
109 2
    public function parseUserAttribute($samlAttribute = null, $propertyName = null)
110
    {
111 2
        if(empty($samlAttribute)) {
112
            return null;
113
        }
114
115 2
        if(empty($propertyName)) {
116
            return $this->getAttribute($samlAttribute);
117
        }
118
119 2
        return $this->{$propertyName} = $this->getAttribute($samlAttribute);
120
    }
121
122
    /**
123
     * Parse the SAML attributes and add them to this user.
124
     *
125
     * @param array $attributes Array of properties which need to be parsed, like ['email' => 'urn:oid:0.9.2342.19200300.100.1.3']
126
     *
127
     * @return void
128
     */
129 1
    public function parseAttributes($attributes = [])
130
    {
131 1
        foreach($attributes as $propertyName => $samlAttribute) {
132 1
            $this->parseUserAttribute($samlAttribute, $propertyName);
133
        }
134 1
    }
135
136
    /**
137
     * Get user's session index.
138
     *
139
     * @return null|string
140
     */
141
    public function getSessionIndex()
142
    {
143
        return $this->auth->getSessionIndex();
144
    }
145
146
    /**
147
     * Get user's name ID.
148
     *
149
     * @return string
150
     */
151
    public function getNameId()
152
    {
153
        return $this->auth->getNameId();
154
    }
155
156
}
157