Saml2User::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Slides\Saml2;
4
5
use OneLogin\Saml2\Auth as OneLoginAuth;
6
use Slides\Saml2\Models\Tenant;
7
8
/**
9
 * Class Saml2User
10
 *
11
 * @package Slides\Saml2
12
 */
13
#[\AllowDynamicProperties]
14
class Saml2User
15
{
16
    /**
17
     * OneLogin authentication handler.
18
     *
19
     * @var OneLoginAuth
20
     */
21
    protected $auth;
22
23
    /**
24
     * The tenant user belongs to.
25
     *
26
     * @var Tenant
27
     */
28
    protected $tenant;
29
30
    /**
31
     * Saml2User constructor.
32
     *
33
     * @param OneLoginAuth $auth
34
     * @param Tenant $tenant
35
     */
36
    public function __construct(OneLoginAuth $auth, Tenant $tenant)
37
    {
38
        $this->auth = $auth;
39
        $this->tenant = $tenant;
40
    }
41
42
    /**
43
     * Get the user ID retrieved from assertion processed this request.
44
     *
45
     * @return string
46
     */
47
    public function getUserId()
48
    {
49
        return $this->auth->getNameId();
50
    }
51
52
    /**
53
     * Get the attributes retrieved from assertion processed this request
54
     *
55
     * @return array
56
     */
57
    public function getAttributes()
58
    {
59
        return $this->auth->getAttributes();
60
    }
61
62
    /**
63
     * Returns the requested SAML attribute
64
     *
65
     * @param string $name The requested attribute of the user.
66
     *
67
     * @return array|null Requested SAML attribute ($name).
68
     */
69
    public function getAttribute($name)
70
    {
71
        return $this->auth->getAttribute($name);
72
    }
73
    
74
    /**
75
     * The attributes retrieved from assertion processed this request.
76
     *
77
     * @return array
78
     */
79
    public function getAttributesWithFriendlyName()
80
    {
81
        return $this->auth->getAttributesWithFriendlyName();
82
    }
83
84
    /**
85
     * The SAML assertion processed this request.
86
     *
87
     * @return string
88
     */
89
    public function getRawSamlAssertion()
90
    {
91
        return app('request')->input('SAMLResponse'); //just this request
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

91
        return /** @scrutinizer ignore-call */ app('request')->input('SAMLResponse'); //just this request
Loading history...
92
    }
93
94
    /**
95
     * Get the intended URL.
96
     *
97
     * @return mixed
98
     */
99
    public function getIntendedUrl()
100
    {
101
        $relayState = app('request')->input('RelayState');
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

101
        $relayState = /** @scrutinizer ignore-call */ app('request')->input('RelayState');
Loading history...
102
103
        $url = app('Illuminate\Contracts\Routing\UrlGenerator');
104
105
        if ($relayState && $url->full() != $relayState) {
106
            return $relayState;
107
        }
108
109
        return null;
110
    }
111
112
    /**
113
     * Parses a SAML property and adds this property to this user or returns the value.
114
     *
115
     * @param string $samlAttribute
116
     * @param string $propertyName
117
     *
118
     * @return array|null
119
     */
120
    public function parseUserAttribute($samlAttribute = null, $propertyName = null)
121
    {
122
        if(empty($samlAttribute)) {
123
            return null;
124
        }
125
126
        if(empty($propertyName)) {
127
            return $this->getAttribute($samlAttribute);
128
        }
129
130
        return $this->{$propertyName} = $this->getAttribute($samlAttribute);
131
    }
132
133
    /**
134
     * Parse the SAML attributes and add them to this user.
135
     *
136
     * @param array $attributes Array of properties which need to be parsed, like ['email' => 'urn:oid:0.9.2342.19200300.100.1.3']
137
     *
138
     * @return void
139
     */
140
    public function parseAttributes($attributes = [])
141
    {
142
        foreach($attributes as $propertyName => $samlAttribute) {
143
            $this->parseUserAttribute($samlAttribute, $propertyName);
144
        }
145
    }
146
147
    /**
148
     * Get user's session index.
149
     *
150
     * @return null|string
151
     */
152
    public function getSessionIndex()
153
    {
154
        return $this->auth->getSessionIndex();
155
    }
156
157
    /**
158
     * Get user's name ID.
159
     *
160
     * @return string
161
     */
162
    public function getNameId()
163
    {
164
        return $this->auth->getNameId();
165
    }
166
167
    /**
168
     * Set a tenant
169
     *
170
     * @param Tenant $tenant
171
     *
172
     * @return void
173
     */
174
    public function setTenant(Tenant $tenant)
175
    {
176
        $this->tenant = $tenant;
177
    }
178
179
    /**
180
     * Get a resolved tenant.
181
     *
182
     * @return Tenant|null
183
     */
184
    public function getTenant()
185
    {
186
        return $this->tenant;
187
    }
188
}
189