Skip to content

Commit

Permalink
add Tapd channel (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
thisliu authored Dec 28, 2020
1 parent 7fef9bf commit 4c94d6c
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ Enjoy it! :heart:
- [豆瓣 - OAuth 2.0 授权机制说明](http://developers.douban.com/wiki/?title=oauth2)
- [抖音 - 网站应用开发指南](http://open.douyin.com/platform/doc)
- [飞书 - 授权说明](https://open.feishu.cn/document/ukTMukTMukTM/uMTNz4yM1MjLzUzM)
- [Tapd - 用户授权说明](https://www.tapd.cn/help/show#1120003271001000093)

## PHP 扩展包开发

Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/BadRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Overtrue\Socialite\Exceptions;

/**
* Class BadRequestException.
*/
class BadRequestException extends Exception
{
}
149 changes: 149 additions & 0 deletions src/Providers/Tapd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace Overtrue\Socialite\Providers;

use Overtrue\Socialite\Exceptions\BadRequestException;
use Overtrue\Socialite\User;

/**
* @see https://www.tapd.cn/help/show#1120003271001000708
*/
class Tapd extends Base
{
public const NAME = 'tapd';
protected string $baseUrl = 'https://api.tapd.cn';

/**
* @return string
*/
protected function getAuthUrl(): string
{
return $this->buildAuthUrlFromBase($this->baseUrl . '/quickstart/testauth');
}

/**
* @return string
*/
protected function getTokenUrl(): string
{
return $this->baseUrl . '/tokens/request_token';
}

/**
* @return string
*/
protected function getRefreshTokenUrl(): string
{
return $this->baseUrl . '/tokens/refresh_token';
}

/**
* @param string $code
*
* @return array
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function tokenFromCode($code): array
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Basic ' . \base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret()))
],
'form_params' => $this->getTokenFields($code),
]);

return $this->normalizeAccessTokenResponse($response->getBody()->getContents());
}

/**
* @param string $code
*
* @return array
*/
protected function getTokenFields(string $code): array
{
return [
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $code,
];
}

/**
* @param $refreshToken
*
* @return array
*/
protected function getRefreshTokenFields($refreshToken): array
{
return [
'grant_type' => 'grant_type',
'redirect_uri' => $this->redirectUrl,
'refresh_token' => $refreshToken,
];
}

/**
* @param string $refreshToken
*
* @return array
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
*/
public function tokenFromRefreshToken(string $refreshToken): array
{
$response = $this->getHttpClient()->post($this->getRefreshTokenUrl(), [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Basic ' . \base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret()))
],
'form_params' => $this->getRefreshTokenFields($refreshToken),
]);

return $this->normalizeAccessTokenResponse($response->getBody()->getContents());
}

/**
* @param string $token
*
* @return array
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function getUserByToken(string $token): array
{
$response = $this->getHttpClient()->get($this->baseUrl . '/users/info', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token,
],
]);

return \json_decode($response->getBody(), true) ?? [];
}

/**
* @param array $user
*
* @return \Overtrue\Socialite\User
*
* @throws \Overtrue\Socialite\Exceptions\BadRequestException
*/
protected function mapUserToObject(array $user): User
{
if (!isset($user['status']) && $user['status'] != 1) {
throw new BadRequestException("用户信息获取失败");
}

return new User([
'id' => $user['data']['id'],
'nickname' => $user['data']['nick'],
'name' => $user['data']['name'],
'email' => '',
'avatar' => $user['data']['avatar'],
]);
}
}

0 comments on commit 4c94d6c

Please sign in to comment.