-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
www
committed
Aug 20, 2012
0 parents
commit d0b2338
Showing
4 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
/** | ||
* Foursquare strategy for Opauth | ||
* | ||
* More information on Opauth: http://opauth.org | ||
* | ||
* @copyright Copyright © 2012 Pocket7878 (http://poketo7878.dip.jp) | ||
* @link http://opauth.org | ||
* @package Opauth.FoursquareStrategy | ||
* @license MIT License | ||
*/ | ||
|
||
class FoursquareStrategy extends OpauthStrategy{ | ||
|
||
/** | ||
* Compulsory config keys, listed as unassociative arrays | ||
* eg. array('app_id', 'app_secret'); | ||
*/ | ||
public $expects = array('client_id', 'client_secret'); | ||
|
||
/** | ||
* Optional config keys with respective default values, listed as associative arrays | ||
* eg. array('scope' => 'email'); | ||
*/ | ||
public $defaults = array( | ||
'redirect_uri' => '{complete_url_to_strategy}int_callback', | ||
); | ||
|
||
/** | ||
* Auth request | ||
*/ | ||
public function request(){ | ||
$url = 'https://foursquare.com/oauth2/authenticate'; | ||
$params = array( | ||
'client_id' => $this->strategy['client_id'], | ||
'response_type' => 'code', | ||
'redirect_uri' => $this->strategy['redirect_uri'] | ||
); | ||
|
||
if (!empty($this->strategy['response_type'])) $params['response_type'] = $this->strategy['response_type']; | ||
|
||
$this->clientGet($url, $params); | ||
} | ||
|
||
/** | ||
* Internal callback, after Foursquare's OAuth | ||
*/ | ||
public function int_callback(){ | ||
if (array_key_exists('code', $_GET) && !empty($_GET['code'])){ | ||
$url = 'https://foursquare.com/oauth2/access_token'; | ||
$params = array( | ||
'client_id' =>$this->strategy['client_id'], | ||
'client_secret' => $this->strategy['client_secret'], | ||
'grant_type' => 'authorization_code', | ||
'redirect_uri'=> $this->strategy['redirect_uri'], | ||
'code' => trim($_GET['code']) | ||
); | ||
$response = $this->serverGet($url, $params, null, $headers); | ||
|
||
$results = json_decode($response); | ||
|
||
if (!empty($results) && isset($results->access_token)){ | ||
$info = $this->user_info($results->access_token); | ||
$user = $info->response->user; | ||
$this->auth = array( | ||
'provider' => 'Foursquare', | ||
'uid' => $user->id, | ||
'info' => array( | ||
'name' => $user->firstName.$user->lastName, | ||
'email' => $user->contact->email, | ||
'first_name' => $user->firstName, | ||
'last_name' => $user->lastName, | ||
'location' => $user->homeCity, | ||
//Cope with new api results | ||
'photo' => $user->photo->prefix.'original'.$user->photo->suffix | ||
), | ||
'credentials' => array( | ||
'token' => $results->access_token, | ||
), | ||
'raw' => $info | ||
); | ||
|
||
$this->callback(); | ||
} | ||
else{ | ||
$error = array( | ||
'provider' => 'Foursquare', | ||
'code' => 'access_token_error', | ||
'message' => 'Failed when attempting to obtain access token', | ||
'raw' => $headers | ||
); | ||
|
||
$this->errorCallback($error); | ||
} | ||
} | ||
else{ | ||
$error = array( | ||
'provider' => 'Foursquare', | ||
'code' => $_GET['error'], | ||
'message' => $_GET['error_description'], | ||
'raw' => $_GET | ||
); | ||
|
||
$this->errorCallback($error); | ||
} | ||
} | ||
|
||
/** | ||
* Queries Facebook Graph API for user info | ||
* | ||
* @param string $access_token | ||
* @return array Parsed JSON results | ||
*/ | ||
private function user_info($oauth_token){ | ||
$info = $this->serverGet('https://api.foursquare.com/v2/users/self', | ||
array('oauth_token' => $oauth_token,'v'=>date("Ymd",time())) , null, $headers); | ||
if (!empty($info)){ | ||
$res = json_decode($info); | ||
if($res->meta->code == 200) { | ||
return $res; | ||
} | ||
else { | ||
$error = array( | ||
'provider' => 'Foursquare', | ||
'code' => 'user_info_error', | ||
'message' => 'Failed when attempting to get user information.', | ||
'raw' => array( | ||
'response' => $info, | ||
'headers' => $headers | ||
) | ||
); | ||
$this->errorCallback($error); | ||
} | ||
} | ||
else{ | ||
$error = array( | ||
'provider' => 'Foursquare', | ||
'code' => 'user_info_error', | ||
'message' => 'Failed when attempting to query for user information', | ||
'raw' => array( | ||
'response' => $info, | ||
'headers' => $headers | ||
) | ||
); | ||
|
||
$this->errorCallback($error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
Opauth-Facebook | ||
============= | ||
[Opauth][1] strategy for Foursquare authentication. | ||
|
||
Implemented based on https://developer.foursquare.com/overview/auth | ||
|
||
Getting started | ||
---------------- | ||
1. Install Opauth-Foursquare: | ||
```bash | ||
cd path_to_opauth/Strategy | ||
git clone git://github.com/uzyn/opauth-facebook.git Facebook | ||
``` | ||
|
||
2. Create Facebook application at https://developers.facebook.com/apps/ | ||
- Remember to enter App Domains | ||
- "Website with Facebook Login" must be checked, but for "Site URL", you can enter any landing URL. | ||
|
||
3. Configure Opauth-Facebook strategy with at least `App ID` and `App Secret`. | ||
|
||
4. Direct user to `http://path_to_opauth/facebook` to authenticate | ||
|
||
Strategy configuration | ||
---------------------- | ||
|
||
Required parameters: | ||
|
||
```php | ||
<?php | ||
'Facebook' => array( | ||
'app_id' => 'YOUR APP ID', | ||
'app_secret' => 'YOUR APP SECRET' | ||
) | ||
``` | ||
|
||
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 | ||
Copyright © 2012 U-Zyn Chua (http://uzyn.com) | ||
|
||
[1]: https://github.com/uzyn/opauth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "opauth/forsquare", | ||
"description": "Foursquare strategy for Opauth", | ||
"keywords": ["authentication","auth","foursquare"], | ||
"homepage": "http://opauth.org", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Masato Sogame (Pocket7878)", | ||
"email": "poketo7878@gmail.com", | ||
"homepage": "http://poketo7878.dip.jp" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.2.0", | ||
"opauth/opauth": ">=0.2.0" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"": "." | ||
} | ||
} | ||
} |