Skip to content

Commit

Permalink
Merge pull request #1 from adam-paterson/features/swap-resource-owner
Browse files Browse the repository at this point in the history
Changing resource owner
  • Loading branch information
adam-paterson committed Sep 7, 2015
2 parents ab60612 + d985a23 commit 2710b26
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 118 deletions.
65 changes: 58 additions & 7 deletions src/Provider/Slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,25 @@ public function getBaseAccessTokenUrl(array $params)
*/
public function getResourceOwnerDetailsUrl(AccessToken $token)
{
return "https://slack.com/api/team.info?token=".$token;
$authorizedUser = $this->getAuthorizedUser($token);

$url = sprintf(
'https://slack.com/api/users.info?token=%s&user=%s',
$token,
$authorizedUser->getId()
);

return $url;
}

/**
* @param $token
*
* @return string
*/
public function getAuthorizedUserTestUrl($token)
{
return "https://slack.com/api/auth.test?token=".$token;
}

/**
Expand All @@ -61,10 +79,7 @@ public function getResourceOwnerDetailsUrl(AccessToken $token)
*/
protected function checkResponse(ResponseInterface $response, $data)
{
if (isset($data['ok']) && $data['ok'] == false) {
$error = isset($error['error']) ? $error['error']: 'Unknown error';
throw new IdentityProviderException($error, 400, $data);
}

}

/**
Expand All @@ -73,11 +88,11 @@ protected function checkResponse(ResponseInterface $response, $data)
* @param array $response
* @param AccessToken $token
*
* @return SlackTeamResourceOwner
* @return SlackResourceOwner
*/
protected function createResourceOwner(array $response, AccessToken $token)
{
return new SlackTeamResourceOwner($response, null);
return new SlackResourceOwner($response);
}

/**
Expand All @@ -87,4 +102,40 @@ protected function getDefaultScopes()
{
return [];
}

/**
* @param AccessToken $token
*
* @return mixed
*/
public function fetchAuthorizedUserDetails(AccessToken $token)
{
$url = $this->getAuthorizedUserTestUrl($token);

$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token);

return $this->getResponse($request);
}

/**
* @param AccessToken $token
*
* @return SlackAuthorizedUser
*/
public function getAuthorizedUser(AccessToken $token)
{
$response = $this->fetchAuthorizedUserDetails($token);

return $this->createAuthorizedUser($response);
}

/**
* @param $response
*
* @return SlackAuthorizedUser
*/
protected function createAuthorizedUser($response)
{
return new SlackAuthorizedUser($response);
}
}
66 changes: 66 additions & 0 deletions src/Provider/SlackAuthorizedUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php


namespace AdamPaterson\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\ResourceOwnerInterface;

class SlackAuthorizedUser implements ResourceOwnerInterface
{
protected $response;

/**
* SlackAuthorizedUser constructor.
*
* @param $response
*/
public function __construct(array $response)
{
$this->response = $response;
}

/**
* Returns the identifier of the authorized resource owner.
*
* @return mixed
*/
public function getId()
{
return $this->response['user_id'];
}

/**
* Return all of the owner details available as an array.
*
* @return array
*/
public function toArray()
{
return $this->response;
}

public function getUrl()
{
return $this->response['url'] ?: null;
}

public function getTeam()
{
return $this->response['team'] ?: null;
}

public function getUser()
{
return $this->response['user'] ?: null;
}

public function getTeamId()
{
return $this->response['team_id'] ?: null;
}

public function getUserId()
{
return $this->response['user_id'] ?: null;
}
}
133 changes: 133 additions & 0 deletions src/Provider/SlackResourceOwner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace AdamPaterson\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\ResourceOwnerInterface;

/**
* Class SlackResourceOwner
* @author Adam Paterson <hello@adampaterson.co.uk>
*
* @package AdamPaterson\OAuth2\Client\Provider
*/
class SlackResourceOwner implements ResourceOwnerInterface
{

protected $response;

public function __construct(array $response)
{
$this->response = $response;
}

/**
* Return all of the owner details available as an array.
*
* @return array
*/
public function toArray()
{
return $this->response;
}

public function getId()
{
return $this->response['user']['id'] ?: null;
}


public function getName()
{
return $this->response['user']['name'] ?: null;
}

public function isDeleted()
{
return $this->response['user']['deleted'] ?: null;
}

public function getColor()
{
return $this->response['user']['color'] ?: null;
}

public function getProfile()
{
return $this->response['user']['profile'] ?: null;
}

public function getFirstName()
{
return $this->response['user']['profile']['first_name'] ?: null;
}

public function getLastName()
{
return $this->response['user']['profile']['last_name'] ?: null;
}

public function getRealName()
{
return $this->response['user']['profile']['real_name'] ?: null;
}

public function getEmail()
{
return $this->response['user']['profile']['email'] ?: null;
}

public function getSkype()
{
return $this->response['user']['profile']['skype'] ?: null;
}

public function getPhone()
{
return $this->response['user']['profile']['phone'] ?: null;
}

public function getImage24()
{
return $this->response['user']['profile']['image_24'] ?: null;
}

public function getImage32()
{
return $this->response['user']['profile']['image_32'] ?: null;
}

public function getImage48()
{
return $this->response['user']['profile']['image_48'] ?: null;
}

public function getImage72()
{
return $this->response['user']['profile']['image_72'] ?: null;
}

public function getImage192()
{
return $this->response['user']['profile']['image_192'] ?: null;
}

public function isAdmin()
{
return $this->response['user']['is_admin'] ?: null;
}

public function isOwner()
{
return $this->response['user']['is_owner'] ?: null;
}

public function hasTwoFactorAuthentication()
{
return $this->response['user']['has_2fa'] ?: null;
}

public function hasFiles()
{
return $this->response['user']['has_files'] ?: null;
}
}
85 changes: 0 additions & 85 deletions src/Provider/SlackTeamResourceOwner.php

This file was deleted.

Loading

0 comments on commit 2710b26

Please sign in to comment.