-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
256 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace Hybridauth\Provider; | ||
|
||
use Hybridauth\Adapter\OAuth2; | ||
use Hybridauth\Exception\InvalidApplicationCredentialsException; | ||
use Hybridauth\Exception\UnexpectedApiResponseException; | ||
use Hybridauth\Data; | ||
use Hybridauth\User\Profile; | ||
|
||
class Mastodon extends OAuth2 | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public $scope = 'read'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $apiDocumentation = 'https://docs.joinmastodon.org/spec/oauth/'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
parent::configure(); | ||
|
||
if (!$this->config->exists('url')) { | ||
throw new InvalidApplicationCredentialsException( | ||
'You must define a Mastodon instance url' | ||
); | ||
} | ||
$url = $this->config->get('url'); | ||
|
||
$this->apiBaseUrl = $url . '/api/v1'; | ||
|
||
$this->authorizeUrl = $url . '/oauth/authorize'; | ||
$this->accessTokenUrl = $url . '/oauth/token'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getUserProfile() | ||
{ | ||
$response = $this->apiRequest('accounts/verify_credentials', 'GET', []); | ||
|
||
$data = new Data\Collection($response); | ||
|
||
if (!$data->exists('id') || !$data->get('id')) { | ||
throw new UnexpectedApiResponseException( | ||
'Provider API returned an unexpected response.' | ||
); | ||
} | ||
|
||
$userProfile = new Profile(); | ||
|
||
$userProfile->identifier = $data->get('id'); | ||
$userProfile->displayName = $data->get('username'); | ||
$userProfile->photoURL = | ||
$data->get('avatar') ?: $data->get('avatar_static'); | ||
$userProfile->webSiteURL = $data->get('url'); | ||
$userProfile->description = $data->get('note'); | ||
$userProfile->firstName = $data->get('display_name'); | ||
|
||
return $userProfile; | ||
} | ||
|
||
public function setUserStatus($status) | ||
{ | ||
// Prepare request parameters. | ||
$params = []; | ||
if (isset($status['message'])) { | ||
$params['status'] = $status['message']; | ||
} | ||
|
||
if (isset($status['picture'])) { | ||
$headers = [ | ||
'Content-Type' => 'multipart/form-data', | ||
]; | ||
|
||
$pictures = $status['picture']; | ||
|
||
$ids = []; | ||
|
||
foreach ($pictures as $picture) { | ||
$images = $this->apiRequest( | ||
$this->config->get('url') . '/api/v2/media', | ||
'POST', | ||
[ | ||
'file' => new \CurlFile( | ||
$picture, | ||
'image/jpg', | ||
'filename' | ||
), | ||
], | ||
$headers, | ||
true | ||
); | ||
|
||
$ids[] = $images->id; | ||
} | ||
|
||
$params['media_ids'] = $ids; | ||
} | ||
|
||
$headers = [ | ||
'Content-Type' => 'application/json', | ||
]; | ||
|
||
$response = $this->apiRequest( | ||
'statuses', | ||
'POST', | ||
$params, | ||
$headers, | ||
false | ||
); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/*! | ||
* Hybridauth | ||
* https://hybridauth.github.io | https://github.com/hybridauth/hybridauth | ||
* (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html | ||
*/ | ||
|
||
namespace Hybridauth\Provider; | ||
|
||
use Hybridauth\Adapter\OAuth2; | ||
use Hybridauth\Exception\UnexpectedApiResponseException; | ||
use Hybridauth\Data; | ||
use Hybridauth\User; | ||
|
||
/** | ||
* Seznam OAuth2 provider adapter. | ||
*/ | ||
class Seznam extends OAuth2 | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $apiBaseUrl = 'https://login.szn.cz/'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $authorizeUrl = 'https://login.szn.cz/api/v1/oauth/auth'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $accessTokenUrl = 'https://login.szn.cz/api/v1/oauth/token'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $apiDocumentation = 'https://vyvojari.seznam.cz/oauth/doc'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getUserProfile() | ||
{ | ||
$response = $this->apiRequest('api/v1/user', 'GET', ['format' => 'json']); | ||
|
||
$data = new Data\Collection($response); | ||
|
||
if (!$data->exists('oauth_user_id')) { | ||
throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); | ||
} | ||
|
||
$userProfile = new User\Profile(); | ||
|
||
$userProfile->identifier = $data->get('oauth_user_id'); | ||
$userProfile->email = $data->get('account_name'); | ||
$userProfile->firstName = $data->get('firstname'); | ||
$userProfile->lastName = $data->get('lastname'); | ||
$userProfile->photoURL = $data->get('avatar_url'); | ||
|
||
return $userProfile; | ||
} | ||
} |