Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
24.00% covered (danger)
24.00%
6 / 25
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
google
24.00% covered (danger)
24.00%
6 / 25
40.00% covered (danger)
40.00%
2 / 5
53.90
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_auth_scope
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 get_service_credentials
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 perform_auth_login
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 perform_token_auth
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\auth\provider\oauth\service;
15
16/**
17 * Google OAuth service
18 */
19class google extends base
20{
21    /** @var \phpbb\config\config */
22    protected $config;
23
24    /** @var \phpbb\request\request_interface */
25    protected $request;
26
27    /**
28     * Constructor.
29     *
30     * @param \phpbb\config\config                $config        Config object
31     * @param \phpbb\request\request_interface    $request    Request object
32     */
33    public function __construct(\phpbb\config\config $config, \phpbb\request\request_interface $request)
34    {
35        $this->config    = $config;
36        $this->request    = $request;
37    }
38
39    /**
40     * {@inheritdoc}
41     */
42    public function get_auth_scope()
43    {
44        return [
45            'userinfo_email',
46            'userinfo_profile',
47        ];
48    }
49
50    /**
51     * {@inheritdoc}
52     */
53    public function get_service_credentials()
54    {
55        return [
56            'key'        => $this->config['auth_oauth_google_key'],
57            'secret'    => $this->config['auth_oauth_google_secret'],
58        ];
59    }
60
61    /**
62     * {@inheritdoc}
63     */
64    public function perform_auth_login()
65    {
66        if (!($this->service_provider instanceof \OAuth\OAuth2\Service\Google))
67        {
68            throw new exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
69        }
70
71        try
72        {
73            // This was a callback request, get the token
74            $this->service_provider->requestAccessToken($this->request->variable('code', ''));
75        }
76        catch (\OAuth\Common\Http\Exception\TokenResponseException $e)
77        {
78            throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
79        }
80
81        try
82        {
83            // Send a request with it
84            $result = (array) json_decode($this->service_provider->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
85        }
86        catch (\OAuth\Common\Exception\Exception $e)
87        {
88            throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
89        }
90
91        // Return the unique identifier
92        return $result['id'];
93    }
94
95    /**
96     * {@inheritdoc}
97     */
98    public function perform_token_auth()
99    {
100        if (!($this->service_provider instanceof \OAuth\OAuth2\Service\Google))
101        {
102            throw new exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
103        }
104
105        try
106        {
107            // Send a request with it
108            $result = (array) json_decode($this->service_provider->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
109        }
110        catch (\OAuth\Common\Exception\Exception $e)
111        {
112            throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
113        }
114
115        // Return the unique identifier
116        return $result['id'];
117    }
118}