Skip to content

Commit e1eb4f4

Browse files
Add main files
1 parent 1b7912f commit e1eb4f4

File tree

7 files changed

+1012
-1
lines changed

7 files changed

+1012
-1
lines changed

README.md

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,173 @@
1-
"# third-party"
1+
# Effectra ThirdParty Library
2+
3+
Effectra\ThirdParty is a PHP library that provides OAuth configuration and functionality for various third-party platforms such as LinkedIn, GitHub, Facebook, and Google. It simplifies the process of integrating with these platforms and accessing user data through OAuth authentication.
4+
5+
## Features
6+
7+
- Simplified OAuth configuration and authentication for third-party platforms.
8+
- Easy retrieval of access tokens and user information.
9+
- Supports multiple popular platforms like LinkedIn, GitHub, Facebook, and Google.
10+
11+
## Installation
12+
13+
You can install the Effectra\ThirdParty library via Composer. Run the following command in your project directory:
14+
15+
```shell
16+
composer require effectra/third-party
17+
```
18+
19+
## Usage
20+
21+
### LinkedIn
22+
23+
To use the LinkedIn OAuth functionality, follow these steps:
24+
25+
1. Create an instance of the `LinkedIn` class by providing your LinkedIn client ID, client secret, and optional redirect URL and scopes.
26+
27+
```php
28+
use Effectra\ThirdParty\LinkedIn;
29+
30+
$linkedin = new LinkedIn('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', 'YOUR_REDIRECT_URL', ['r_liteprofile', 'r_emailaddress']);
31+
```
32+
33+
2. Generate the authorization URL to redirect the user for authentication:
34+
35+
```php
36+
$authUrl = $linkedin->getAuthURL();
37+
```
38+
39+
3. Redirect the user to the generated authorization URL. After successful authentication, LinkedIn will redirect the user back to the specified redirect URL with an authorization code.
40+
41+
4. Exchange the authorization code for an access token:
42+
43+
```php
44+
$code = $_GET['code']; // The authorization code obtained from the LinkedIn redirect
45+
$accessToken = $linkedin->getAccessToken($code);
46+
```
47+
48+
5. Use the access token to retrieve user information:
49+
50+
```php
51+
$user = $linkedin->getUser($accessToken);
52+
```
53+
54+
### GitHub
55+
56+
To use the GitHub OAuth functionality, follow these steps:
57+
58+
1. Create an instance of the `GitHub` class by providing your GitHub client ID, client secret, and optional redirect URL and scopes.
59+
60+
```php
61+
use Effectra\ThirdParty\GitHub;
62+
63+
$github = new GitHub('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', 'YOUR_REDIRECT_URL', ['user']);
64+
```
65+
66+
2. Generate the authorization URL to redirect the user for authentication:
67+
68+
```php
69+
$authUrl = $github->getAuthURL();
70+
```
71+
72+
3. Redirect the user to the generated authorization URL. After successful authentication, GitHub will redirect the user back to the specified redirect URL with an authorization code.
73+
74+
4. Exchange the authorization code for an access token:
75+
76+
```php
77+
$code = $_GET['code']; // The authorization code obtained from the GitHub redirect
78+
$accessToken = $github->getAccessToken($code);
79+
```
80+
81+
5. Use the access token to retrieve user information:
82+
83+
```php
84+
$user = $github->getUser($accessToken);
85+
```
86+
87+
### Facebook
88+
89+
To use the Facebook OAuth functionality, follow these steps:
90+
91+
1. Create an instance of the `Facebook` class by providing your Facebook client ID, client secret, and optional redirect URL and scopes.
92+
93+
```php
94+
use Effectra\ThirdParty\Facebook;
95+
96+
$facebook = new Facebook('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', 'YOUR_REDIRECT_URL', ['email']);
97+
```
98+
99+
2. Generate the authorization URL to redirect the user for authentication:
100+
101+
```php
102+
$authUrl = $facebook->getAuthURL();
103+
```
104+
105+
3. Redirect the user to the generated authorization URL. After successful authentication, Facebook will redirect the user back to the specified redirect URL with an authorization code.
106+
107+
4. Exchange the authorization code for an access token:
108+
109+
```php
110+
$code = $_GET['code']; // The authorization code obtained from the Facebook redirect
111+
$accessToken = $facebook->getAccessToken($code);
112+
```
113+
114+
5
115+
116+
. Use the access token to retrieve user information:
117+
118+
```php
119+
$user = $facebook->getUser($accessToken);
120+
```
121+
122+
### Google
123+
124+
To use the Google OAuth functionality, follow these steps:
125+
126+
1. Create an instance of the `Google` class by providing your Google client ID, client secret, and optional redirect URL and scopes.
127+
128+
```php
129+
use Effectra\ThirdParty\Google;
130+
131+
$google = new Google('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', 'YOUR_REDIRECT_URL', ['profile', 'email']);
132+
```
133+
134+
2. Generate the authorization URL to redirect the user for authentication:
135+
136+
```php
137+
$authUrl = $google->getAuthURL();
138+
```
139+
140+
3. Redirect the user to the generated authorization URL. After successful authentication, Google will redirect the user back to the specified redirect URL with an authorization code.
141+
142+
4. Exchange the authorization code for an access token:
143+
144+
```php
145+
$code = $_GET['code']; // The authorization code obtained from the Google redirect
146+
$accessToken = $google->getAccessToken($code);
147+
```
148+
149+
5. Use the access token to retrieve user information:
150+
151+
```php
152+
$user = $google->getUser($accessToken);
153+
```
154+
155+
## License
156+
157+
This library is open source and available under the [MIT License](LICENSE).
158+
159+
## Contribution
160+
161+
Contributions are welcome! If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.
162+
163+
## Credits
164+
165+
This library is developed and maintained by Effectra. You can find more information about us on our website: [www.effectra.com](https://www.effectra.com/)
166+
167+
## Contact
168+
169+
For any inquiries or questions, you can reach us at [info@effectra.com](mailto:info@effectra.com)
170+
171+
---
172+
173+
Feel free to modify and customize this README file according to your specific needs.

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "effectra/third-party",
3+
"description": "The Effectra ThirdParty package.",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Effectra\\ThirdParty\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Mohammed Taha",
14+
"email": "bmt.mohammedtaha@gmail.com"
15+
}
16+
],
17+
"require": {
18+
"php": "^8.0.2",
19+
"guzzlehttp/guzzle": "7.7.x-dev"
20+
},
21+
"minimum-stability": "dev"
22+
}

src/Facebook.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace Effectra\ThirdParty;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\ClientException;
7+
8+
/**
9+
* Represents Facebook OAuth configuration and functionality.
10+
*/
11+
class Facebook extends OAuthConfig
12+
{
13+
/**
14+
* @var Client The HTTP client.
15+
*/
16+
protected Client $client;
17+
18+
/**
19+
* The authorization URL for Facebook OAuth.
20+
*/
21+
const AUTH_URL = 'https://www.facebook.com/v12.0/dialog/oauth';
22+
23+
/**
24+
* The token URL for Facebook OAuth.
25+
*/
26+
const TOKEN_URL = 'https://graph.facebook.com/v12.0/oauth/access_token';
27+
28+
/**
29+
* The user info URL for Facebook OAuth.
30+
*/
31+
const USER_URL = 'https://graph.facebook.com/me?fields=id,name,email';
32+
33+
/**
34+
* The default scopes for Facebook OAuth.
35+
*
36+
* @var array
37+
*/
38+
public array $scopes = [
39+
'email',
40+
];
41+
42+
/**
43+
* Facebook constructor.
44+
*
45+
* @param string $client_id The client ID.
46+
* @param string $client_secret The client secret.
47+
* @param string $redirect_url The redirect URL.
48+
* @param array $scopes The array of scopes.
49+
*/
50+
public function __construct(string $client_id, string $client_secret, string $redirect_url = '', array $scopes = [])
51+
{
52+
if (empty($scopes)) {
53+
$scopes = $this->scopes;
54+
}
55+
parent::__construct($client_id, $client_secret, $redirect_url, $scopes);
56+
$this->client = new Client();
57+
}
58+
59+
/**
60+
* Get the Facebook OAuth configuration as an array.
61+
*
62+
* @return array The Facebook OAuth configuration array.
63+
*/
64+
public function getConfig(): array
65+
{
66+
return [
67+
'client_id' => $this->getClientId(),
68+
'client_secret' => $this->getClientSecret(),
69+
'redirect_uri' => $this->getRedirectURL(),
70+
'scope' => $this->withScopes($this->scopes)->getScopesString(),
71+
'state' => $this->generateToken(15),
72+
'response_type' => 'code',
73+
'grant_type' => 'authorization_code',
74+
];
75+
}
76+
77+
/**
78+
* Get the Facebook OAuth authorization URL.
79+
*
80+
* @return string The authorization URL.
81+
*/
82+
public function getAuthURL(): string
83+
{
84+
$params = $this->onlyConfig([
85+
'response_type',
86+
'client_id',
87+
'redirect_uri',
88+
'scope',
89+
'state',
90+
]);
91+
92+
return $this->buildUrl(self::AUTH_URL, $params);
93+
}
94+
95+
/**
96+
* Get the access token using the authorization code.
97+
*
98+
* @param string $code The authorization code.
99+
* @return string The access token.
100+
*/
101+
public function getAccessToken(string $code): string
102+
{
103+
try {
104+
$params = array_merge(
105+
['code' => $code],
106+
$this->onlyConfig([
107+
'client_id',
108+
'client_secret',
109+
'redirect_uri',
110+
'grant_type',
111+
])
112+
);
113+
114+
$response = $this->client->post(self::TOKEN_URL, ['form_params' => $params]);
115+
116+
$data = json_decode($response->getBody(), true);
117+
118+
return $data['access_token'] ?? '';
119+
} catch (ClientException $e) {
120+
return '';
121+
}
122+
}
123+
124+
/**
125+
* Get the user information using the access token.
126+
*
127+
* @param string $token The access token.
128+
* @return array|null The user information array, or null if an error occurred.
129+
*/
130+
public function getUser(string $token): ?array
131+
{
132+
try {
133+
$decoded = urlencode($token);
134+
$url = self::USER_URL . '?' . http_build_query(['access_token' => $decoded]);
135+
136+
$response = $this->client->get($url);
137+
138+
return json_decode($response->getBody(), true);
139+
140+
} catch (ClientException $e) {
141+
return null;
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)