Completed
Push — master ( c3456a...b4bda7 )
by Damien
04:06 queued 38s
created

ProviderIdentity::forceGet()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 11
cp 0
rs 9.488
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 1/12/18
6
 * Time: 10:44 PM
7
 */
8
9
namespace flipbox\saml\sp\services;
10
11
use craft\elements\User;
12
use flipbox\saml\core\exceptions\InvalidMessage;
13
use flipbox\saml\core\records\ProviderInterface;
14
use flipbox\saml\core\services\AbstractProviderIdentityService;
15
use flipbox\saml\sp\records\ProviderIdentityRecord;
16
use flipbox\saml\sp\Saml;
17
use flipbox\saml\sp\services\login\AssertionTrait;
18
use flipbox\saml\sp\traits\SamlPluginEnsured;
19
use LightSaml\Model\Protocol\Response as SamlResponse;
20
use yii\base\UserException;
21
22
/**
23
 * Class ProviderIdentity
24
 *
25
 * @package flipbox\saml\sp\services
26
 */
27
class ProviderIdentity extends AbstractProviderIdentityService
28
{
29
30
    use SamlPluginEnsured, AssertionTrait;
31
32
    /**
33
     * ACS Methods
34
     */
35
36
    /**
37
     * @param User $user
38
     * @param SamlResponse $response
39
     * @return ProviderIdentityRecord
40
     * @throws InvalidMessage
41
     * @throws UserException
42
     */
43
    public function getByUserAndResponse(User $user, \LightSaml\Model\Protocol\Response $response)
44
    {
45
46
        $idpProvider = Saml::getInstance()->getProvider()->findByEntityId(
47
            $response->getIssuer()->getValue()
48
        )->one();
49
50
        /**
51
         * Get Identity
52
         */
53
        $identity = $this->forceGet(
54
            $this->getFirstAssertion($response)->getSubject()->getNameID()->getValue(),
55
            $idpProvider
56
        );
57
58
        /**
59
         * Get Session
60
         */
61
        $sessionIndex = null;
62
        if ($response->getFirstAssertion()->hasAnySessionIndex()) {
63
            $sessionIndex = $response->getFirstAssertion()->getFirstAuthnStatement()->getSessionIndex();
64
        }
65
66
        /**
67
         * Set Identity Properties
68
         */
69
        $identity->userId = $user->id;
70
        $identity->enabled = true;
71
        $identity->sessionId = $sessionIndex;
72
        return $identity;
73
    }
74
75
76
    /**
77
     * @param string $nameId
78
     * @param ProviderInterface $provider
79
     * @return ProviderIdentityRecord
80
     * @throws UserException
81
     */
82
    protected function forceGet($nameId, ProviderInterface $provider)
83
    {
84
        // @var \flipbox\saml\sp\records\ProviderIdentityRecord $identity
85
        if (! $identity = $this->findByNameId(
86
            $nameId,
87
            $provider
88
        )->one()
89
        ) {
90
            if (! Saml::getInstance()->getSettings()->createUser) {
91
                throw new UserException("System doesn't have permission to create a new user.");
92
            }
93
94
            /**
95
             * Create the new identity if one wasn't found above.
96
             * Since we now have the user id, and we might not have above,
97
             * do this last.
98
             */
99
            $identity = new ProviderIdentityRecord(
100
                [
101
                    'providerId' => $provider->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface flipbox\saml\core\records\ProviderInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
102
                    'nameId'     => $nameId,
103
                ]
104
            );
105
        }
106
107
        return $identity;
108
    }
109
}
110