Skip to content

Commit

Permalink
Release ready
Browse files Browse the repository at this point in the history
  • Loading branch information
t1mmen committed Mar 11, 2015
1 parent bde99d0 commit a3a6b0e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ Required parameters:
License
---------
Opauth-Slack is MIT Licensed
Copyright © 2014 Timm Stokke (http://timm.stokke.me)
Copyright © 2015 Timm Stokke (http://timm.stokke.me)

[1]: https://github.com/opauth/opauth
49 changes: 30 additions & 19 deletions SlackStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
/**
* Slack strategy for Opauth
*
* Based on work by U-Zyn Chua (http://uzyn.com)
*
* More information on Opauth: http://opauth.org
*
* @copyright Copyright © 2012 U-Zyn Chua (http://uzyn.com)
* @copyright Copyright © 2015 Timm Stokke (http://timm.stokke.me)
* @link http://opauth.org
* @package Opauth.SlackStrategy
* @package Opauth.BasecampStrategy
* @license MIT License
*/


/**
* Slack strategy for Opauth
*
Expand All @@ -29,7 +32,7 @@ class SlackStrategy extends OpauthStrategy {

/**
* Optional config keys with respective default values, listed as associative arrays
* eg. array('scope' => 'email');
* eg. array('scope' => 'post');
*/
public $defaults = array(
'redirect_uri' => '{complete_url_to_strategy}oauth2callback'
Expand All @@ -39,7 +42,7 @@ class SlackStrategy extends OpauthStrategy {
* Auth request
*/
public function request() {
$url = 'https://github.com/login/oauth/authorize';
$url = 'https://slack.com/oauth/authorize';
$params = array(
'client_id' => $this->strategy['client_id'],
'redirect_uri' => $this->strategy['redirect_uri']
Expand All @@ -58,40 +61,40 @@ public function request() {
public function oauth2callback() {
if (array_key_exists('code', $_GET) && !empty($_GET['code'])) {
$code = $_GET['code'];
$url = 'https://github.com/login/oauth/access_token';
$url = 'https://slack.com/api/oauth.access';

$params = array(
'code' => $code,
'client_id' => $this->strategy['client_id'],
'client_secret' => $this->strategy['client_secret'],
'redirect_uri' => $this->strategy['redirect_uri'],
);

if (!empty($this->strategy['state'])) $params['state'] = $this->strategy['state'];

$response = $this->serverPost($url, $params, null, $headers);
parse_str($response, $results);
$results = json_decode($response,true);

if (!empty($results) && !empty($results['access_token'])) {

$user = $this->user($results['access_token']);


$this->auth = array(
'uid' => $user['id'],
'uid' => $user['basics']['user_id'],
'info' => array(),
'credentials' => array(
'token' => $results['access_token']
),
'raw' => $user
);

$this->mapProfile($user, 'name', 'info.name');
$this->mapProfile($user, 'blog', 'info.urls.blog');
$this->mapProfile($user, 'avatar_url', 'info.image');
$this->mapProfile($user, 'bio', 'info.description');
$this->mapProfile($user, 'login', 'info.nickname');
$this->mapProfile($user, 'html_url', 'info.urls.github');
$this->mapProfile($user, 'email', 'info.email');
$this->mapProfile($user, 'location', 'info.location');
$this->mapProfile($user, 'url', 'info.urls.github_api');
$this->mapProfile($user, 'user.real_name', 'info.name');
$this->mapProfile($user, 'user.name', 'info.nickname');
$this->mapProfile($user, 'user.profile.first_name', 'info.first_name');
$this->mapProfile($user, 'user.profile.last_name', 'info.last_name');
$this->mapProfile($user, 'user.profile.email', 'info.email');
$this->mapProfile($user, 'user.profile.image_48', 'info.image');

$this->callback();
}
Expand Down Expand Up @@ -125,15 +128,23 @@ public function oauth2callback() {
* @return array Parsed JSON results
*/
private function user($access_token) {
$user = $this->serverGet('https://api.github.com/user', array('access_token' => $access_token), null, $headers);
$user = $this->serverGet('https://slack.com/api/auth.test', array('token' => $access_token), null, $headers);

if (!empty($user)) {
return $this->recursiveGetObjectVars(json_decode($user));
$basics = $this->recursiveGetObjectVars(json_decode($user));

// Get detailed info:
$getDetails = $this->serverGet('https://slack.com/api/users.info', array('token' => $access_token, 'user' => $basics['user_id']), null, $headers);
$details = $this->recursiveGetObjectVars(json_decode($getDetails));

$details['basics'] = $basics;

return $details;
}
else {
$error = array(
'code' => 'userinfo_error',
'message' => 'Failed when attempting to query GitHub v3 API for user information',
'message' => 'Failed when attempting to query Slack API for user information',
'raw' => array(
'response' => $user,
'headers' => $headers
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "t1mmen/opauth-slack",
"description": "Basecamp strategy for Slack",
"keywords": ["authentication","auth","slack"],
"keywords": ["authentication","auth","slack", "opauth", "oauth"],
"homepage": "https://github.com/t1mmen/opauth-slack",
"license": "MIT",
"authors": [
Expand Down

0 comments on commit a3a6b0e

Please sign in to comment.