Skip to content

Commit

Permalink
modified: src/config/config.php
Browse files Browse the repository at this point in the history
	new file:   src/providers/FacebookProvider.php
	modified:   src/providers/GoogleProvider.php
  • Loading branch information
juancristobalgd1 committed Nov 30, 2023
1 parent 2134b0c commit 29fd878
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
return
[
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect_uri' => env('GOOGLE_REDIRECT'),
'redirect_uri' => env('GOOGLE_REDIRECT'),
],

'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect_uri' => env('FACEBOOK_REDIRECT'),
'redirect_uri' => env('FACEBOOK_REDIRECT'),
],

'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect_uri' => env('GITHUB_REDIRECT'),
'redirect_uri' => env('GITHUB_REDIRECT'),
],

'slack' => [
'client_id' => env('SLACK_CLIENT_ID'),
'client_id' => env('SLACK_CLIENT_ID'),
'client_secret' => env('SLACK_CLIENT_SECRET'),
'redirect_uri' => env('SLACK_REDIRECT'),
'redirect_uri' => env('SLACK_REDIRECT'),
],
];
130 changes: 130 additions & 0 deletions src/providers/FacebookProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Axm\Socialite\Providers;

use Axm\Http\Curl;

class FacebookProvider
{
/**
* @var string
*/
const VERSION = 'v3.3';

/**
* Base URL for Facebook API.
*/
const API_BASE_URL = 'https://graph.facebook.com';

/**
* URL for authorization.
*/
const AUTHORIZE_URL = 'https://www.facebook.com/' . self::VERSION . '/dialog/oauth';

/**
* URL for obtaining access token.
*/
const ACCESS_TOKEN_URL = self::API_BASE_URL . self::VERSION . '/oauth/access_token';

/**
* URL for fetching user information.
*/
const USER_INFO_URL = self::API_BASE_URL . self::VERSION . '/me';

/**
* URL for Facebook API documentation.
*/
const API_DOCUMENTATION_URL = 'https://developers.facebook.com/docs/graph-api/overview';

/**
* @var array
*/
private $config;

public function __construct(array $config = [])
{
$this->config = $config;
}

/**
* Get the URL for authorization.
* @return string Authorization URL.
*/
public function redirect()
{
$params = [
'client_id' => $this->config['client_id'],
'redirect_uri' => $this->config['redirect_uri'],
'scope' => 'email', // Adjust the scope as needed
'response_type' => 'code',
];

$authorizeUrl = self::AUTHORIZE_URL . '?' . http_build_query($params);
$this->makeRedirect($authorizeUrl);
}

/**
* Get the access token using the provided authorization code.
*
* @return string Access token.
* @throws \RuntimeException If unable to obtain access token.
*/
public function user()
{
$code = $this->getCode();
if (!empty($code)) {
try {
$params = $this->getParams($code);

$curl = new Curl();
$response = $curl->get(self::ACCESS_TOKEN_URL . '?' . http_build_query($params));
$data = json_decode($response['response'], true);

$userInfoUrl = self::USER_INFO_URL . '?access_token=' . $data['access_token'];
$userInfoResponse = $curl->get($userInfoUrl);
$userInfo = json_decode($userInfoResponse['response'], true);

$curl->close();
} catch (\Throwable $e) {
throw new \RuntimeException('Failed to obtain access token: ' . $e->getMessage());
}

return (object)$userInfo;
}
}

/**
* @return array
*/
public function getParams($code): array
{
return [
'code' => $code,
'client_id' => $this->config['client_id'],
'client_secret' => $this->config['client_secret'],
'redirect_uri' => $this->config['redirect_uri'],
'grant_type' => 'authorization_code',
];
}

/**
* @return mixed
*/
public function getCode()
{
return app()
->request
->get('code') ?? null;
}

/**
* @param string $url
* @return [type]
*/
public function makeRedirect(string $url)
{
if (!headers_sent()) {
app()->response->redirect($url);
}
}
}
9 changes: 5 additions & 4 deletions src/providers/GoogleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Axm\Socialite\Providers;

use Axm\Http\Curl;

/**
* GoogleProvider - A provider for Google OAuth2 authentication.
Expand Down Expand Up @@ -31,7 +32,7 @@ class GoogleProvider
/**
* URL for Google API documentation.
*/
const API_DOCUMENTATION_URL = 'https://developers.google.com/identity/protocols/OAuth2';
const API_DOCUMENTATION_URL = 'https://console.cloud.google.com/apis';

/**
* Configuration array for the provider.
Expand Down Expand Up @@ -76,9 +77,10 @@ public function user()
$code = $this->getCode();
if (!empty($code)) {
try {

$params = $this->getParams($code);

$curl = new \Axm\Http\Curl();
$curl = new Curl();
$response = $curl->post(self::ACCESS_TOKEN_URL, $params);
$data = json_decode($response['response'], true);

Expand Down Expand Up @@ -127,10 +129,9 @@ public function getCode()
*/
public function makeRedirect(string $url)
{
if (!headers_sent()) {
if (!headers_sent())
app()
->response
->redirect($url);
}
}
}

0 comments on commit 29fd878

Please sign in to comment.