| 1 |  |  | <?php
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Mezon\SocialNetwork\Auth;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Mezon\SocialNetwork\BaseAuth;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Class Odnoklassniki
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package BaseAuth
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @subpackage Odnoklassniki
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author Dodonov A.A.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @version v.1.0 (2019/08/17)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @copyright Copyright (c) 2019, aeon.org
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Class provides integration with OK
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author Dodonov A.A.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class Odnoklassniki extends BaseAuth
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Method returns URL wich generates tokens
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return string URL
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function getOauthUri(): string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return 'https://connect.ok.ru/oauth/authorize?scope=VALUABLE_ACCESS;PHOTO_CONTENT&';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * {@inheritdoc}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @see \Mezon\SocialNetwork\BaseAuth::getUserInfoUri()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function getUserInfoUri(string $token = ''): string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $signature = md5(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             'application_key=' . $this->settings['client_public'] . 'fields=' . $this->getDesiredFields() .
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             'format=jsonmethod=users.getCurrentUser' . md5($token . $this->settings['client_secret']));
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         return 'http://api.odnoklassniki.ru/fb.do?application_key=' . $this->settings['client_public'] .
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             '&format=json&method=users.getCurrentUser&sig=' . $signature . '&';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Method returns
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return string URL
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function getTokenUri(): string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return 'http://api.odnoklassniki.ru/oauth/token.do?grant_type=authorization_code&';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Method returns a list of desired fields
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return string Comma separated of the desired fields
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function getDesiredFields(): string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return 'UID,LOCALE,FIRST_NAME,LAST_NAME,EMAIL,PIC190X190,PIC640X480';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Method dispatches user info
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param array $userInfo
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      *            user info got from social network
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return array dispatched user info. Must be as array with keys id, first_name, last_name, email, picture
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function dispatchUserInfo(array $userInfo): array
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $userInfo['email'] = $userInfo['email'] ?? '';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $return = [
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'id' => $userInfo['uid'],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'first_name' => $userInfo['first_name'],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'last_name' => $userInfo['last_name'],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'picture' => $userInfo['pic190x190'],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'email' => $userInfo['email']
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         ];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $return;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Method returns params for getting token
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param string $code
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      *            - Access code
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return array Params
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     public function getTokenParams(string $code): array
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return [
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             'client_id' => $this->settings['client_id'],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             'redirect_uri' => $this->settings['redirect_uri'],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             'client_secret' => $this->settings['client_secret'],
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             'grant_type' => 'authorization_code',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             'code' => $code
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         ];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * Method requests token from server
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param array $params
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      *            - Request params
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return array Token data
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @codeCoverageIgnore
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |      */
 | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     public function requestToken(array $params): array
 | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |     {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         // TODO create CurlWrapperMock in InfraStructureLayer
 | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |         $result = \Mezon\CustomClient\CurlWrapper::sendRequest(
 | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             'http://api.odnoklassniki.ru/oauth/token.do',
 | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |             [],
 | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |             'POST',
 | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |             $params);
 | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  | 
 | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |         return json_decode($result[0], true);
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     }
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  | }
 | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  |