Methods

Source

LineCode
1
<?php
2
3
namespace AuthStack\SAMLServiceProvider\Model;
4
5
use LightSaml\Credential\KeyHelper;
6
use LightSaml\Credential\X509Certificate;
7
use LightSaml\Model\XmlDSig\SignatureWriter;
8
use RobRichards\XMLSecLibs\XMLSecurityKey;
9
10
class IdentityProvider
11
{
12
    protected $entity_id;
13
14
    public function __construct(string $entity_id)
15
    {
16
        $this->entity_id = $entity_id;
17
    }
18
19
    /**
20
     * @return \Illuminate\Config\Repository|mixed
21
     */
22
    public function getBaseUrl()
23
    {
24
        return $this->fromConfig('baseUrl');
25
    }
26
27
    /**
28
     * @return string
29
     */
30
    public function getEntityID(): string
31
    {
32
        return $this->entity_id;
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function getSSOUrl(): string
39
    {
40
        return sprintf("%s%s", $this->getBaseUrl(), $this->fromConfig('SSO.url'));
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getSSOBinding(): string
47
    {
48
        return $this->fromConfig('SSO.binding');
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getLogoutURL(): string
55
    {
56
        return sprintf("%s%s", $this->getBaseUrl(), $this->fromConfig('SLO.url'));
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getLogoutBinding(): string
63
    {
64
        return $this->fromConfig('SLO.binding');
65
    }
66
67
    /**
68
     * @return \Illuminate\Config\Repository|mixed
69
     */
70
    public function getX509()
71
    {
72
        return $this->fromConfig('x509');
73
    }
74
75
    /**
76
     * @return XMLSecurityKey
77
     */
78
    public function getPublicKey(): XMLSecurityKey
79
    {
80
        return KeyHelper::createPublicKey(
81
            (new X509Certificate())->setData($this->getX509())
82
        );
83
    }
84
85
    /**
86
     * @return bool
87
     */
88
    public function wantsSignedReguests(): bool
89
    {
90
        return $this->fromConfig('wantsSignedRequests');
91
    }
92
93
    /**
94
     * @param string $path
95
     * @return \Illuminate\Config\Repository|mixed
96
     */
97
    protected function fromConfig(string $path)
98
    {
99
        $path = sprintf("saml.idp.%s.%s", sha1($this->entity_id), $path);
100
101
        return config($path);
102
    }
103
104
    /**
105
     * @return XMLSecurityKey
106
     */
107
    public function getSigningPublicKey(): XMLSecurityKey
108
    {
109
        return KeyHelper::createPublicKey(
110
            (new X509Certificate())->setData($this->getSigningKeyX509Cert())
111
        );
112
    }
113
114
    /**
115
     * @return SignatureWriter
116
     */
117
    public function getSignatureWriter(): SignatureWriter
118
    {
119
        $x509 = new X509Certificate();
120
121
        $x509->setData($this->getSigningKeyX509Cert());
122
123
        $priv = KeyHelper::createPrivateKey(
124
            $this->getSigningPrivateKey(),
125
            $this->getSigningPrivateKeyPassphrase(),
126
            false,
127
            $this->getSigningDigestAlgorithm()
128
        );
129
130
        return new SignatureWriter($x509, $priv);
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getSigningKeyX509Cert(): string
137
    {
138
        return config(sprintf('saml.idp.%s.keys.signing.x509', sha1($this->getEntityID())));
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getSigningPrivateKey(): string
145
    {
146
        return config(sprintf('saml.idp.%s.keys.signing.private', sha1($this->getEntityID())));
147
    }
148
149
    /**
150
     * @return null|string
151
     */
152
    public function getSigningPrivateKeyPassphrase(): ?string
153
    {
154
        return config(sprintf('saml.idp.%s.keys.signing.passphrase', sha1($this->getEntityID())));
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getSigningDigestAlgorithm(): string
161
    {
162
        return config(sprintf('saml.idp.%s.keys.signing.algorithm', sha1($this->getEntityID())));
163
    }
164
165
    /**
166
     * @return \DateTime
167
     */
168
    public function getUpperDT(): \DateTime
169
    {
170
        return new \DateTime($this->getValidationUpperTimeBoundary(), new \DateTimeZone('UTC'));
171
    }
172
173
    /**
174
     * @return \DateTime
175
     */
176
    public function getLowerDT(): \DateTime
177
    {
178
        return new \DateTime($this->getValidationLowerTimeBoundary(), new \DateTimeZone('UTC'));
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function getValidationUpperTimeBoundary(): string
185
    {
186
        return config(sprintf('saml.idp.%s.upper', sha1($this->getEntityID())));
187
    }
188
189
    /**
190
     * @return string
191
     */
192
    public function getValidationLowerTimeBoundary(): string
193
    {
194
        return config(sprintf('saml.idp.%s.lower', sha1($this->getEntityID())));
195
    }
196
}