Skip to content

Commit

Permalink
Update Slack provider to use v2 API and allow Bot tokens (#645)
Browse files Browse the repository at this point in the history
* Update Slack driver to use v2 API and allow Bot tokens

* Update SlackProvider.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
jbrooksuk and taylorotwell committed Jul 14, 2023
1 parent 851c822 commit 50148ed
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/Two/SlackProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,39 @@ class SlackProvider extends AbstractProvider
*/
protected $scopes = ['identity.basic', 'identity.email', 'identity.team', 'identity.avatar'];

/**
* The key used for scopes.
*
* @var string
*/
protected $scopeKey = 'user_scope';

/**
* Indicate that the requested token should be for a bot user.
*
* @return $this
*/
public function asBotUser()
{
$this->scopeKey = 'scope';

return $this;
}

/**
* {@inheritdoc}
*/
public function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://slack.com/oauth/authorize', $state);
return $this->buildAuthUrlFromBase('https://slack.com/oauth/v2/authorize', $state);
}

/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://slack.com/api/oauth.access';
return 'https://slack.com/api/oauth.v2.access';
}

/**
Expand Down Expand Up @@ -55,4 +74,38 @@ protected function mapUserToObject(array $user)
'organization_id' => Arr::get($user, 'team.id'),
]);
}

/**
* {@inheritdoc}
*/
protected function getCodeFields($state = null)
{
$fields = parent::getCodeFields($state);

if ($this->scopeKey === 'user_scope') {
$fields['scope'] = '';
$fields['user_scope'] = $this->formatScopes($this->scopes, $this->scopeSeparator);
}

return $fields;
}

/**
* {@inheritdoc}
*/
public function getAccessTokenResponse($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => $this->getTokenHeaders($code),
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
]);

$result = json_decode($response->getBody(), true);

if ($this->scopeKey === 'user_scope') {
return $result['authed_user'];
}

return $result;
}
}

0 comments on commit 50148ed

Please sign in to comment.