Skip to content

Commit

Permalink
Merge 3700321 into ccc329e
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-paterson committed Sep 20, 2017
2 parents ccc329e + 3700321 commit d795fe4
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 9 deletions.
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ This package provides Slack OAuth 2.0 support for the PHP League's [OAuth 2.0 Cl

To install, use composer:

```
composer require adam-paterson/oauth2-slack
```bash
$ composer require adam-paterson/oauth2-slack
```
## Usage

Expand Down Expand Up @@ -73,6 +73,44 @@ Usage is the same as The League's OAuth client, using `\AdamPaterson\OAuth2\Clie
echo $token->getToken();
}

## Scopes
OAuth scopes, indicating which parts of the Slack user's account you'd like your app to be able to access. The complete list of scopes can be found [here](https://api.slack.com/docs/oauth-scopes).

```php
$provider = new \AdamPaterson\OAuth2\Client\Provider\Slack([
'clientId' => '{slack-client-id}',
'clientSecret' => '{slack-client-secret}',
'redirectUri' => 'https://example.com/callback-url',
]);

$authUrl = $provider->$provider->getAuthorizationUrl([
'scope' => 'user:read user:write file:write'
]);
```

## Bot Access Tokens
If your Slack app includes a bot user, upon approval the JSON response will contain an additional node containing an access token to be specifically used for your bot user, within the context of the approving workspace.

**Note: You must pass the `bot` scope for this additional node to be present**

```php

$authUrl = $provider->$provider->getAuthorizationUrl([
'scope' => 'bot'
]);

$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);

$values = $token->getValues();

// bot user id
$botUserId = $values['bot']['bot_user_id'];
$botAccessToken = $values['bot']['bot_access_token'];

```

## Testing

``` bash
Expand Down
10 changes: 4 additions & 6 deletions src/Provider/Slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Slack extends AbstractProvider
*/
public function getBaseAuthorizationUrl()
{
return "https://slack.com/oauth/authorize";
return 'https://slack.com/oauth/authorize';
}

/**
Expand All @@ -35,7 +35,7 @@ public function getBaseAuthorizationUrl()
*/
public function getBaseAccessTokenUrl(array $params)
{
return "https://slack.com/api/oauth.access";
return 'https://slack.com/api/oauth.access';
}

/**
Expand All @@ -54,9 +54,7 @@ public function getResourceOwnerDetailsUrl(AccessToken $token)
'user' => $authorizedUser->getId()
];

$url = 'https://slack.com/api/users.info?'.http_build_query($params);

return $url;
return 'https://slack.com/api/users.info?'.http_build_query($params);
}

/**
Expand All @@ -66,7 +64,7 @@ public function getResourceOwnerDetailsUrl(AccessToken $token)
*/
public function getAuthorizedUserTestUrl($token)
{
return "https://slack.com/api/auth.test?token=".$token;
return 'https://slack.com/api/auth.test?token=' . $token;
}

/**
Expand Down
35 changes: 34 additions & 1 deletion src/Provider/SlackAuthorizedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@

use League\OAuth2\Client\Provider\ResourceOwnerInterface;

/**
* Class SlackAuthorizedUser
*
* @package AdamPaterson\OAuth2\Client\Provider
*/
class SlackAuthorizedUser implements ResourceOwnerInterface
{
/**
* @var array
*/
protected $response;

/**
Expand All @@ -22,7 +30,7 @@ public function __construct(array $response)
/**
* Returns the identifier of the authorized resource owner.
*
* @return mixed
* @return string
*/
public function getId()
{
Expand All @@ -39,26 +47,51 @@ public function toArray()
return $this->response;
}

/**
* Get authorized user url
*
* @return string|null
*/
public function getUrl()
{
return $this->response['url'] ?: null;
}

/**
* Get team
*
* @return string|null
*/
public function getTeam()
{
return $this->response['team'] ?: null;
}

/**
* Get user id
*
* @return string|null
*/
public function getUser()
{
return $this->response['user'] ?: null;
}

/**
* Get team id
*
* @return string|null
*/
public function getTeamId()
{
return $this->response['team_id'] ?: null;
}

/**
* Get user id
*
* @return string|null
*/
public function getUserId()
{
return $this->response['user_id'] ?: null;
Expand Down
108 changes: 108 additions & 0 deletions src/Provider/SlackResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@
class SlackResourceOwner implements ResourceOwnerInterface
{

/**
* @var array
*/
protected $response;

/**
* SlackResourceOwner constructor.
*
* @param array $response
*/
public function __construct(array $response)
{
$this->response = $response;
Expand All @@ -31,102 +39,202 @@ public function toArray()
return $this->response;
}

/**
* Get user id
*
* @return string|null
*/
public function getId()
{
return $this->response['user']['id'] ?: null;
}


/**
* Get user name
*
* @return string|null
*/
public function getName()
{
return $this->response['user']['name'] ?: null;
}

/**
* Is user deleted?
*
* @return bool|null
*/
public function isDeleted()
{
return $this->response['user']['deleted'] ?: null;
}

/**
* Get user color
*
* @return string|null
*/
public function getColor()
{
return $this->response['user']['color'] ?: null;
}

/**
* Get user profile
*
* @return string|null
*/
public function getProfile()
{
return $this->response['user']['profile'] ?: null;
}

/**
* Get user first name
*
* @return string|null
*/
public function getFirstName()
{
return $this->response['user']['profile']['first_name'] ?: null;
}

/**
* Get user last name
*
* @return string|null
*/
public function getLastName()
{
return $this->response['user']['profile']['last_name'] ?: null;
}

/**
* Get user real name
*
* @return string|null
*/
public function getRealName()
{
return $this->response['user']['profile']['real_name'] ?: null;
}

/**
* Get user email
*
* @return string|null
*/
public function getEmail()
{
return $this->response['user']['profile']['email'] ?: null;
}

/**
* Get Skype username
*
* @return string|null
*/
public function getSkype()
{
return $this->response['user']['profile']['skype'] ?: null;
}

/**
* Get phone number
*
* @return string|null
*/
public function getPhone()
{
return $this->response['user']['profile']['phone'] ?: null;
}

/**
* Get 24x24 image url
*
* @return string|null
*/
public function getImage24()
{
return $this->response['user']['profile']['image_24'] ?: null;
}

/**
* Get 32x32 image url
*
* @return string|null
*/
public function getImage32()
{
return $this->response['user']['profile']['image_32'] ?: null;
}

/**
* Get 48x48 image url
*
* @return string|null
*/
public function getImage48()
{
return $this->response['user']['profile']['image_48'] ?: null;
}

/**
* Get 72x72 image url
*
* @return string|null
*/
public function getImage72()
{
return $this->response['user']['profile']['image_72'] ?: null;
}

/**
* Get 192x192 image url
*
* @return string|null
*/
public function getImage192()
{
return $this->response['user']['profile']['image_192'] ?: null;
}

/**
* Is user admin?
*
* @return bool|null
*/
public function isAdmin()
{
return $this->response['user']['is_admin'] ?: null;
}

/**
* Is user owner?
*
* @return string|null
*/
public function isOwner()
{
return $this->response['user']['is_owner'] ?: null;
}

/**
* Does user have 2FA enabled?
*
* @return bool|null
*/
public function hasTwoFactorAuthentication()
{
return $this->response['user']['has_2fa'] ?: null;
}

/**
* Does user have files?
*
* @return bool|null
*/
public function hasFiles()
{
return $this->response['user']['has_files'] ?: null;
Expand Down

0 comments on commit d795fe4

Please sign in to comment.