Completed
Pull Request — master (#114)
by Beñat
09:39
created

OauthManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 104
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A password() 0 7 1
A refreshToken() 0 6 1
A grantType() 0 16 1
A getClient() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Kreta\Bundle\UserBundle\Manager;
14
15
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
16
use Kreta\Bundle\CoreBundle\Model\Interfaces\RefreshTokenInterface;
17
use OAuth2\OAuth2;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * Oauth manager class.
22
 *
23
 * @author Beñat Espiña <[email protected]>
24
 */
25
class OauthManager
26
{
27
    /**
28
     * The client manager.
29
     *
30
     * @var \FOS\OAuthServerBundle\Model\ClientManagerInterface
31
     */
32
    protected $clientManager;
33
34
    /**
35
     * The client secret.
36
     *
37
     * @var string|null
38
     */
39
    protected $clientSecret;
40
41
    /**
42
     * The instance of OAuth server.
43
     *
44
     * @var \OAuth2\OAuth2
45
     */
46
    protected $oauthServer;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param \FOS\OAuthServerBundle\Model\ClientManagerInterface $clientManager The client manager
52
     * @param \OAuth2\OAuth2                                      $oauthServer   The instance of OAuth server
53
     * @param string|null                                         $clientSecret  The client secret
54
     */
55
    public function __construct(ClientManagerInterface $clientManager, OAuth2 $oauthServer, $clientSecret = null)
56
    {
57
        $this->clientManager = $clientManager;
58
        $this->oauthServer = $oauthServer;
59
        $this->clientSecret = $clientSecret;
60
    }
61
62
    /**
63
     * Executes the "password" grant type.
64
     *
65
     * @param string $username The username
66
     * @param string $password The password
67
     *
68
     * @return array
69
     */
70
    public function password($username, $password)
71
    {
72
        return $this->grantType(OAuth2::GRANT_TYPE_USER_CREDENTIALS, [
73
            'username' => $username,
74
            'password' => $password,
75
        ]);
76
    }
77
78
    /**
79
     * Executes the "refresh token" grant type.
80
     *
81
     * @param RefreshTokenInterface $refreshToken The refresh token
82
     *
83
     * @return array
84
     */
85
    public function refreshToken(RefreshTokenInterface $refreshToken)
86
    {
87
        return $this->grantType(OAuth2::GRANT_TYPE_REFRESH_TOKEN, [
88
            'refresh_token' => $refreshToken->getToken(),
89
        ]);
90
    }
91
92
    /**
93
     * Scaffold method to use a OAuth gran type.
94
     *
95
     * @param string $name    The grant type
96
     * @param array  $options Array which contains the grant type custom options
97
     *
98
     * @throws \OAuth2\OAuth2ServerException
99
     *
100
     * @return array
101
     */
102
    protected function grantType($name, array $options = [])
103
    {
104
        $client = $this->getClient();
105
106
        $request = new Request();
107
        $request->query->add(array_merge([
108
            'grant_type'    => $name,
109
            'client_secret' => $this->clientSecret,
110
            'client_id'     => sprintf('%s_%s', $client->getId(), $client->getRandomId()),
111
        ], $options));
112
113
        $response = $this->oauthServer->grantAccessToken($request);
114
        $token = json_decode($response->getContent(), true);
115
116
        return [$token['access_token'], $token['refresh_token']];
117
    }
118
119
    /**
120
     * Get the OAuth client.
121
     *
122
     * @return \FOS\OAuthServerBundle\Model\ClientInterface
123
     */
124
    protected function getClient()
125
    {
126
        return $this->clientManager->findClientBy(['secret' => $this->clientSecret]);
127
    }
128
}
129