1 <?php
2 
3 namespace OAuth\Plugin;
4 
5 use OAuth\OAuth2\Service\EveOnline;
6 use helper_plugin_evesso;
7 
8 /**
9  * Class DoorkeeperAdapter
10  *
11  * This is an example on how to implement your own adapter for making DokuWiki login against
12  * a custom oAuth provider. The used Generic Service backend expects the authorization and
13  * token endpoints to be configured in the DokuWiki backend.
14  *
15  * Your custom API to access user data has to be implemented in the getUser function. The one here
16  * is setup to work with the demo setup of the "Doorkeeper" ruby gem.
17  *
18  * @link https://github.com/doorkeeper-gem/doorkeeper
19  * @package OAuth\Plugin
20  */
21 class EveOnlineAdapter extends AbstractAdapter {
22 
23     /**
24      * Retrieve the user's data
25      *
26      * The array needs to contain at least 'user', 'mail', 'name' and optional 'grps'
27      *
28      * @return array
29      */
30     public function getUser() {
31         $http = new \DokuHTTPClient();
32         $data = array();
33 
34         //Get access_token
35         $access_token = $this->oAuth->getStorage()->retrieveAccessToken($this->oAuth->service())->getAccessToken();
36 
37         //Split JWT into 3 parts > Take the 2nd part (payload) > base64 decode it > json decode it
38         $jwt_payload = json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $access_token)[1]))));
39 
40         //Get character name and id
41         $character_name=$jwt_payload->name; //Character Name
42         $character_id=explode(":",$jwt_payload->sub)[2]; //Charater ID (remove the extra stuff)
43 
44         if (!isset($character_id)) {
45             return $data;
46         }
47 
48         //Set character name and id
49         $data['user'] = $character_name;
50         $data['name'] = $character_name;
51         $data['mail'] = $character_id . '@eveonline.com';
52 
53         //Get character corporation, alliance, and faction
54         $affiliation_post = $http->post('https://esi.evetech.net/latest/characters/affiliation/?datasource=tranquility', '[' . $character_id . ']');
55         if ($affiliation_post === false) {
56             return $data;
57         }
58         $affiliation_result = json_decode($affiliation_post, true);
59 
60         $ids = array();
61         foreach ($affiliation_result as $entry) {
62             if (isset($entry['alliance_id'])) {
63                 $ids[] = $entry['alliance_id'];
64             }
65             if (isset($entry['faction_id'])) {
66                 $ids[] = $entry['faction_id'];
67             }
68             $ids[] = $entry['corporation_id'];
69         }
70 
71         //Convert ids to names
72         $names_post = $http->post('https://esi.evetech.net/latest/universe/names/?datasource=tranquility', '[' . implode(",", $ids) . ']');
73         if ($names_post === false) {
74             return $data;
75         }
76         $names_result = json_decode($names_post, true);
77 
78         foreach ($names_result as $entry) {
79             $category = $entry['category'];
80             if ($category == 'corporation') {
81                 $data['grps'][] = helper_plugin_evesso::CORPORATION_PREFIX . $entry['name'];
82             } elseif ($category == 'alliance') {
83                 $data['grps'][] = helper_plugin_evesso::ALLIANCE_PREFIX . $entry['name'];
84             } elseif ($category == 'faction') {
85                 $data['grps'][] = helper_plugin_evesso::FACTION_PREFIX . $entry['name'];
86             }
87         }
88 
89         return $data;
90     }
91 
92     public function getScope() {
93         return array(EveOnline::SCOPE_PUBLIC_DATA);
94     }
95 
96     public function login() {
97         $parameters = array();
98         $parameters['state'] = urlencode(base64_encode(json_encode(array('state' => md5(rand())))));
99         $this->storage->storeAuthorizationState($this->oAuth->service(), $parameters['state']);
100         $url = $this->oAuth->getAuthorizationUri($parameters);
101         send_redirect($url);
102     }
103 }