Initial commit
This commit is contained in:
		
							
								
								
									
										21
									
								
								composer.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								composer.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | |||||||
|  | { | ||||||
|  |     "name": "beu/oauth2-trackmania", | ||||||
|  |     "description": "A TrackMania 2020 provider for league/oauth2-client", | ||||||
|  |     "authors": [ | ||||||
|  |         { | ||||||
|  |             "name": "Beu", | ||||||
|  |             "email": "benoit+symfonybundle@virtit.fr", | ||||||
|  |             "homepage": "http://virtit.fr", | ||||||
|  |             "role": "Developer" | ||||||
|  |         } | ||||||
|  |     ], | ||||||
|  |     "autoload": { | ||||||
|  |         "psr-4": { | ||||||
|  |             "Beu\\TrackMania\\OAuth2\\Client\\": "src/" | ||||||
|  |         } | ||||||
|  |     }, | ||||||
|  |     "require": { | ||||||
|  |         "php": ">=7.4.0", | ||||||
|  |         "league/oauth2-client": "^2.2" | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										94
									
								
								src/Provider/TrackManiaProvider.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								src/Provider/TrackManiaProvider.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,94 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | namespace Beu\TrackMania\OAuth2\Client\Provider; | ||||||
|  |  | ||||||
|  | use League\OAuth2\Client\Provider\AbstractProvider; | ||||||
|  | use League\OAuth2\Client\Provider\Exception\IdentityProviderException; | ||||||
|  | use League\OAuth2\Client\Provider\ResourceOwnerInterface; | ||||||
|  | use League\OAuth2\Client\Token\AccessToken; | ||||||
|  | use League\OAuth2\Client\Tool\BearerAuthorizationTrait; | ||||||
|  | use Psr\Http\Message\ResponseInterface; | ||||||
|  |  | ||||||
|  | class TrackManiaProvider extends AbstractProvider | ||||||
|  | { | ||||||
|  |     use BearerAuthorizationTrait; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Returns the base URL for authorizing a client. | ||||||
|  |      * | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|  |     public function getBaseAuthorizationUrl() | ||||||
|  |     { | ||||||
|  |         return 'https://api.trackmania.com/oauth/authorize'; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Returns the base URL for requesting an access token. | ||||||
|  |      * | ||||||
|  |      * Eg. https://oauth.service.com/token | ||||||
|  |      * | ||||||
|  |      * @param array $params | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|  |     public function getBaseAccessTokenUrl(array $params) | ||||||
|  |     { | ||||||
|  |         return 'https://api.trackmania.com/api/access_token'; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Returns the URL for requesting the resource owner's details. | ||||||
|  |      * | ||||||
|  |      * @param AccessToken $token | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|  |     public function getResourceOwnerDetailsUrl(AccessToken $token) | ||||||
|  |     { | ||||||
|  |         return 'https://api.trackmania.com/api/user'; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Returns the default scopes used by this provider. | ||||||
|  |      * | ||||||
|  |      * This should only be the scopes that are required to request the details | ||||||
|  |      * of the resource owner, rather than all the available scopes. | ||||||
|  |      * | ||||||
|  |      * @return string[] | ||||||
|  |      */ | ||||||
|  |     protected function getDefaultScopes() | ||||||
|  |     { | ||||||
|  |         return ['']; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Checks a provider response for errors. | ||||||
|  |      * | ||||||
|  |      * @throws IdentityProviderException | ||||||
|  |      * @param  ResponseInterface $response | ||||||
|  |      * @param  array|string $data Parsed response data | ||||||
|  |      * @return void | ||||||
|  |      */ | ||||||
|  |     protected function checkResponse(ResponseInterface $response, $data) | ||||||
|  |     { | ||||||
|  |         if (isset($data['error'])) { | ||||||
|  |             throw new IdentityProviderException( | ||||||
|  |                 $data['error'] ?: $response->getReasonPhrase(), | ||||||
|  |                 $response->getStatusCode(), | ||||||
|  |                 $response->getBody() | ||||||
|  |             ); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Generates a resource owner object from a successful resource owner | ||||||
|  |      * details request. | ||||||
|  |      * | ||||||
|  |      * @param  array $response | ||||||
|  |      * @param  AccessToken $token | ||||||
|  |      * @return ResourceOwnerInterface | ||||||
|  |      */ | ||||||
|  |     protected function createResourceOwner(array $response, AccessToken $token) | ||||||
|  |     { | ||||||
|  |         return new TrackManiaProviderOwner($response); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										56
									
								
								src/Provider/TrackManiaProviderOwner.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/Provider/TrackManiaProviderOwner.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | namespace Beu\TrackMania\OAuth2\Client\Provider; | ||||||
|  |  | ||||||
|  | use League\OAuth2\Client\Provider\ResourceOwnerInterface; | ||||||
|  | use League\OAuth2\Client\Tool\ArrayAccessorTrait; | ||||||
|  |  | ||||||
|  | class TrackManiaProviderOwner implements ResourceOwnerInterface | ||||||
|  | { | ||||||
|  |     use ArrayAccessorTrait; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Raw response | ||||||
|  |      * | ||||||
|  |      * @var array | ||||||
|  |      */ | ||||||
|  |     protected $response; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @param array $response | ||||||
|  |      */ | ||||||
|  |     public function __construct(array $response) | ||||||
|  |     { | ||||||
|  |         $this->response = $response; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Returns the Account Id of the player | ||||||
|  |      * | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|  |     public function getId() | ||||||
|  |     { | ||||||
|  |         return $this->getValueByKey($this->response, 'accountId'); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Returns the Nickname of the Player | ||||||
|  |      * | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|  |     public function getDisplayName() | ||||||
|  |     { | ||||||
|  |         return $this->getValueByKey($this->response, 'displayName'); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Return all of the owner details available as an array. | ||||||
|  |      * | ||||||
|  |      * @return array | ||||||
|  |      */ | ||||||
|  |     public function toArray() | ||||||
|  |     { | ||||||
|  |         return $this->response; | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user