summaryrefslogtreecommitdiff
path: root/app/Core/Http/OAuth2.php
blob: f47927e1e5d2403b4b6f83132b8ace04a58b3faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

namespace Kanboard\Core\Http;

use Kanboard\Core\Base;

/**
 * OAuth2 Client
 *
 * @package  http
 * @author   Frederic Guillot
 */
class OAuth2 extends Base
{
    protected $clientId;
    protected $secret;
    protected $callbackUrl;
    protected $authUrl;
    protected $tokenUrl;
    protected $scopes;
    protected $tokenType;
    protected $accessToken;

    /**
     * Create OAuth2 service
     *
     * @access public
     * @param  string  $clientId
     * @param  string  $secret
     * @param  string  $callbackUrl
     * @param  string  $authUrl
     * @param  string  $tokenUrl
     * @param  array   $scopes
     * @return OAuth2
     */
    public function createService($clientId, $secret, $callbackUrl, $authUrl, $tokenUrl, array $scopes)
    {
        $this->clientId = $clientId;
        $this->secret = $secret;
        $this->callbackUrl = $callbackUrl;
        $this->authUrl = $authUrl;
        $this->tokenUrl = $tokenUrl;
        $this->scopes = $scopes;

        return $this;
    }

    /**
     * Generate OAuth2 state and return the token value
     *
     * @access public
     * @return string
     */
    public function getState()
    {
        if (! session_exists('oauthState')) {
            session_set('oauthState', $this->token->getToken());
        }

        return session_get('oauthState');
    }

    /**
     * Check the validity of the state (CSRF token)
     *
     * @access public
     * @param  string $state
     * @return bool
     */
    public function isValidateState($state)
    {
        return $state === $this->getState();
    }

    /**
     * Get authorization url
     *
     * @access public
     * @return string
     */
    public function getAuthorizationUrl()
    {
        $params = array(
            'response_type' => 'code',
            'client_id' => $this->clientId,
            'redirect_uri' => $this->callbackUrl,
            'scope' => implode(' ', $this->scopes),
            'state' => $this->getState(),
        );

        return $this->authUrl.'?'.http_build_query($params);
    }

    /**
     * Get authorization header
     *
     * @access public
     * @return string
     */
    public function getAuthorizationHeader()
    {
        if (strtolower($this->tokenType) === 'bearer') {
            return 'Authorization: Bearer '.$this->accessToken;
        }

        return '';
    }

    /**
     * Get access token
     *
     * @access public
     * @param  string  $code
     * @return string
     */
    public function getAccessToken($code)
    {
        if (empty($this->accessToken) && ! empty($code)) {
            $params = array(
                'code' => $code,
                'client_id' => $this->clientId,
                'client_secret' => $this->secret,
                'redirect_uri' => $this->callbackUrl,
                'grant_type' => 'authorization_code',
                'state' => $this->getState(),
            );

            $response = json_decode($this->httpClient->postForm($this->tokenUrl, $params, array('Accept: application/json')), true);

            $this->tokenType = isset($response['token_type']) ? $response['token_type'] : '';
            $this->accessToken = isset($response['access_token']) ? $response['access_token'] : '';
        }

        return $this->accessToken;
    }

    /**
     * Set access token
     *
     * @access public
     * @param  string  $token
     * @param  string  $type
     * @return $this
     */
    public function setAccessToken($token, $type = 'bearer')
    {
        $this->accessToken = $token;
        $this->tokenType = $type;
        return $this;
    }
}