Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0 #37

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open

1.0 #37

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 0 additions & 146 deletions FacebookStrategy.php

This file was deleted.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Getting started
----------------
1. Install Opauth-Facebook:
```bash
cd path_to_opauth/Strategy
git clone git://github.com/uzyn/opauth-facebook.git Facebook
cd path/to/app/root
composer require opauth/facebook:dev-wip/1.0
```

2. Create Facebook application at https://developers.facebook.com/apps/
Expand All @@ -33,13 +33,13 @@ Required parameters:
)
```

Even though `scope` is an optional configuration parameter for Opauth-Facebook, for most cases you would like to explicitly define it. It should be defined in a comma-separated string.
Even though `scope` is an optional configuration parameter for Opauth-Facebook, for most cases you would like to explicitly define it. It should be defined in a comma-separated string.

Refer to [Facebook Permissions Reference](https://developers.facebook.com/docs/authentication/permissions/) for list of valid permissions..

License
---------
Opauth-Facebook is MIT Licensed
Opauth-Facebook is MIT Licensed
Copyright © 2012 U-Zyn Chua (http://uzyn.com)

[1]: https://github.com/uzyn/opauth
48 changes: 26 additions & 22 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
{
"name": "opauth/facebook",
"description": "Facebook strategy for Opauth",
"keywords": ["authentication","auth","facebook"],
"homepage": "http://opauth.org",
"license": "MIT",
"authors": [
{
"name": "U-Zyn Chua",
"email": "[email protected]",
"homepage": "http://uzyn.com"
}
],
"require": {
"php": ">=5.2.0",
"opauth/opauth": ">=0.2.0"
},
"autoload": {
"psr-0": {
"": "."
}
}
}
"name": "opauth/facebook",
"description": "Facebook strategy for Opauth",
"keywords": ["authentication", "auth", "facebook"],
"homepage": "http://opauth.org",
"license": "MIT",
"authors": [
{
"name": "U-Zyn Chua",
"email": "[email protected]",
"homepage": "http://uzyn.com"
},
{
"name": "Ceeram",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3.0",
"opauth/opauth": "~1.0"
},
"autoload": {
"psr-4": {
"Opauth\\Facebook\\Strategy\\": "src"
}
}
}
146 changes: 146 additions & 0 deletions src/Facebook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* Facebook strategy for Opauth
* based on https://developers.facebook.com/docs/authentication/server-side/
*
* More information on Opauth: http://opauth.org
*
* @copyright Copyright © 2012 U-Zyn Chua (http://uzyn.com)
* @link http://opauth.org
* @package Opauth.FacebookStrategy
* @license MIT License
*/
namespace Opauth\Facebook\Strategy;

use Opauth\Opauth\AbstractStrategy;

class Facebook extends AbstractStrategy
{

/**
* Compulsory config keys, listed as numeric indexed arrays
* eg. array('app_id', 'app_secret');
*/
public $expects = array('app_id', 'app_secret');

/**
* Map response from raw data
*
* @var array
*/
public $responseMap = array(
'name' => 'name',
'uid' => 'id',
'info.name' => 'name',
'info.email' => 'email',
'info.first_name' => 'first_name',
'info.last_name' => 'last_name',
'info.location' => 'location.name',
'info.urls.website' => 'website'
);

/**
* Auth request
*
* @return void
*/
public function request()
{
$url = 'https://www.facebook.com/dialog/oauth';
$strategyKeys = array(
'scope',
'state',
'response_type',
'display',
'auth_type',
'app_id' => 'client_id'
);
$params = $this->addParams($strategyKeys);
$params['redirect_uri'] = $this->callbackUrl();
$this->redirect($url, $params);
}

/**
* Internal callback, after Facebook's OAuth
*
* @return \Opauth\Opauth\Response
*/
public function callback()
{
if (!array_key_exists('code', $_GET) || empty($_GET['code'])) {
return $this->codeError();
}

$url = 'https://graph.facebook.com/oauth/access_token';
$params = $this->callbackParams();
$response = $this->http->get($url, $params);
parse_str($response, $results);

if (empty($results['access_token'])) {
return $this->tokenError($response);
}

$me = $this->me($results['access_token']);
if (!$me) {
return $this->error('Failed when attempting to query for user information.', 'me_error');
}

$response = $this->response($me);
$response->credentials = array(
'token' => $results['access_token'],
'expires' => isset($results['expires']) ? date('c', time() + $results['expires']) : null
);
$response->info['image'] = 'https://graph.facebook.com/' . $me['id'] . '/picture?type=square';
return $response;
}

/**
* Helper method for callback()
*
* @return array Parameter array
*/
protected function callbackParams()
{
$params = array(
'redirect_uri' => $this->callbackUrl(),
'code' => trim($_GET['code'])
);
$strategyKeys = array(
'app_id' => 'client_id',
'app_secret' => 'client_secret'
);
return $this->addParams($strategyKeys, $params);
}

/**
* @return \Opauth\Opauth\Response
*/
protected function codeError()
{
return $this->error($_GET['error_description'], $_GET['error'], $_GET);
}

/**
* @param string $raw
* @return \Opauth\Opauth\Response
*/
protected function tokenError($raw)
{
return $this->error('Failed when attempting to obtain access token.', 'access_token_error', $raw);
}

/**
* Queries Facebook Graph API for user info
*
* @param string $access_token
* @return array Parsed JSON results
*/
protected function me($access_token)
{
$me = $this->http->get('https://graph.facebook.com/me', array('access_token' => $access_token));
if (empty($me)) {
return false;
}
return $this->recursiveGetObjectVars(json_decode($me));
}
}