reorganize and cleanup php server code

This commit is contained in:
2026-02-06 13:12:38 +01:00
parent bf2f18f847
commit a6785f26db
3103 changed files with 494 additions and 351462 deletions
@@ -0,0 +1,81 @@
<?php
namespace Vanguard\Services\Auth\TwoFactor;
trait Authenticatable
{
/**
* Get the e-mail address used for two-factor authentication.
*
* @return string
*/
public function getEmailForTwoFactorAuth()
{
return $this->email;
}
/**
* Get the country code used for two-factor authentication.
*
* @return string
*/
public function getAuthCountryCode()
{
return $this->two_factor_country_code;
}
/**
* Get the phone number used for two-factor authentication.
*
* @return string
*/
public function getAuthPhoneNumber()
{
return $this->two_factor_phone;
}
/**
* Set the country code and phone number used for two-factor authentication.
*
* @param $countryCode
* @param string $phoneNumber
*/
public function setAuthPhoneInformation($countryCode, $phoneNumber)
{
$this->two_factor_country_code = $countryCode;
$this->two_factor_phone = $phoneNumber;
}
/**
* Get the two-factor provider options in array format.
*
* @return array
*/
public function getTwoFactorAuthProviderOptions()
{
return json_decode($this->two_factor_options, true) ?: [];
}
/**
* Set the two-factor provider options in array format.
*
* @param array $options
* @return void
*/
public function setTwoFactorAuthProviderOptions(array $options)
{
$this->two_factor_options = json_encode($options);
}
/**
* Determine if the user is using two-factor authentication.
*
* @return bool
*/
public function getUsingTwoFactorAuthAttribute()
{
$options = $this->getTwoFactorAuthProviderOptions();
return isset($options['id']);
}
}
@@ -0,0 +1,107 @@
<?php
namespace Vanguard\Services\Auth\TwoFactor;
use Exception;
use GuzzleHttp\Client as HttpClient;
use Vanguard\Services\Auth\TwoFactor\Contracts\Provider;
use Vanguard\Services\Auth\TwoFactor\Contracts\Authenticatable as TwoFactorAuthenticatable;
class Authy implements Provider
{
/**
* Determine if the given user has two-factor authentication enabled.
*
* @param TwoFactorAuthenticatable $user
* @return bool
*/
public function isEnabled(TwoFactorAuthenticatable $user)
{
$options = $user->getTwoFactorAuthProviderOptions();
return isset($options['enabled']) && $options['enabled'] === true;
}
/**
* Register the given user with the provider.
*
* @param TwoFactorAuthenticatable $user
*/
public function register(TwoFactorAuthenticatable $user)
{
$key = config('services.authy.key');
$response = json_decode((new HttpClient)->post('https://api.authy.com/protected/json/users/new?api_key='.$key, [
'form_params' => [
'user' => [
'email' => $user->getEmailForTwoFactorAuth(),
'cellphone' => preg_replace('/[^0-9]/', '', $user->getAuthPhoneNumber()),
'country_code' => $user->getAuthCountryCode(),
],
],
])->getBody(), true);
$user->setTwoFactorAuthProviderOptions([
'id' => $response['user']['id'],
]);
}
/**
* {@inheritdoc}
*/
public function sendTwoFactorVerificationToken(TwoFactorAuthenticatable $user)
{
$key = config('services.authy.key');
$options = $user->getTwoFactorAuthProviderOptions();
$response = json_decode((new HttpClient)->get(
'https://api.authy.com/protected/json/sms/'.$options['id'].'?force=true&api_key='.$key
)->getBody(), true);
return $response['success'] === true;
}
/**
* Determine if the given token is valid for the given user.
*
* @param TwoFactorAuthenticatable $user
* @param string $token
* @return bool
*/
public function tokenIsValid(TwoFactorAuthenticatable $user, $token)
{
try {
$key = config('services.authy.key');
$options = $user->getTwoFactorAuthProviderOptions();
$response = json_decode((new HttpClient)->get(
'https://api.authy.com/protected/json/verify/'.$token.'/'.$options['id'].'?force=true&api_key='.$key
)->getBody(), true);
return $response['token'] === 'is valid';
} catch (Exception $e) {
return false;
}
}
/**
* Delete the given user from the provider.
*
* @param TwoFactorAuthenticatable $user
* @return bool
*/
public function delete(TwoFactorAuthenticatable $user)
{
$key = config('services.authy.key');
$options = $user->getTwoFactorAuthProviderOptions();
(new HttpClient)->post(
'https://api.authy.com/protected/json/users/delete/'.$options['id'].'?api_key='.$key
);
$user->setTwoFactorAuthProviderOptions([]);
}
}
@@ -0,0 +1,27 @@
<?php
namespace Vanguard\Services\Auth\TwoFactor;
use Illuminate\Support\ServiceProvider;
class AuthyServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind('authy', Authy::class);
}
/**
* @return array
*/
public function provides()
{
return ['authy'];
}
}
@@ -0,0 +1,52 @@
<?php
namespace Vanguard\Services\Auth\TwoFactor\Contracts;
use Illuminate\Contracts\Auth\Authenticatable as BaseAuthenticatable;
interface Authenticatable extends BaseAuthenticatable
{
/**
* Get the e-mail address used for two-factor authentication.
*
* @return string
*/
public function getEmailForTwoFactorAuth();
/**
* Get the country code used for two-factor authentication.
*
* @return string
*/
public function getAuthCountryCode();
/**
* Get the phone number used for two-factor authentication.
*
* @return string
*/
public function getAuthPhoneNumber();
/**
* Set the country code and phone number used for two-factor authentication.
*
* @param integer $countryCode
* @param integer $phoneNumber
*/
public function setAuthPhoneInformation($countryCode, $phoneNumber);
/**
* Get the two-factor provider options in array format.
*
* @return array
*/
public function getTwoFactorAuthProviderOptions();
/**
* Set the two-factor provider options in array format.
*
* @param array $options
* @return void
*/
public function setTwoFactorAuthProviderOptions(array $options);
}
@@ -0,0 +1,47 @@
<?php
namespace Vanguard\Services\Auth\TwoFactor\Contracts;
use Vanguard\Services\Auth\TwoFactor\Contracts\Authenticatable as TwoFactorAuthenticatable;
interface Provider
{
/**
* Determine if the given user has two-factor authentication enabled.
*
* @param Authenticatable $user
* @return bool
*/
public function isEnabled(TwoFactorAuthenticatable $user);
/**
* Register the given user with the provider.
*
* @param Authenticatable $user
*/
public function register(TwoFactorAuthenticatable $user);
/**
* Sends an SMS with a phone verification token.
* @param Authenticatable $user
* @return mixed
*/
public function sendTwoFactorVerificationToken(TwoFactorAuthenticatable $user);
/**
* Determine if the given token is valid for the given user.
*
* @param Authenticatable $user
* @param string $token
* @return bool
*/
public function tokenIsValid(TwoFactorAuthenticatable $user, $token);
/**
* Delete the given user from the provider.
*
* @param Authenticatable $user
* @return bool
*/
public function delete(TwoFactorAuthenticatable $user);
}
@@ -0,0 +1,18 @@
<?php
namespace Vanguard\Services\Auth\TwoFactor;
class Facade extends \Illuminate\Support\Facades\Facade
{
/**
* Get the registered name of the component.
*
* @return string
*
* @throws \RuntimeException
*/
protected static function getFacadeAccessor()
{
return 'authy';
}
}