PHP code example of rafalniewinski / silex-steamauth

1. Go to this page and download the library: Download rafalniewinski/silex-steamauth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rafalniewinski / silex-steamauth example snippets


$app->register(new SteamAuth\SteamAuthServiceProvider(), array(
    'steam_auth.host' => 'https://domain.tld' // your service domain to configure OpenID
));

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'admin' => array(
        'pattern' => '^/admin/',
        'steam_auth' => array('check_path' => '/admin/login_check'), // Only this line is different - see below
        'users' => array(
            'admin' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
        ),
    ),
));

$app['security.token_storage']->getToken()->getUser()->getSteamID();

use SteamAuth\SteamAuthUserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Doctrine\DBAL\Connection;

class UserProvider implements SteamAuthUserProviderInterface
{
    private $conn;

    public function __construct(Connection $conn)
    {
        $this->conn = $conn;
    }

    public function loadUserBySteamId($steamid)
    {
        $stmt = $this->conn->executeQuery('SELECT * FROM users WHERE steamid = ?', array($steamid));

        if (!$user = $stmt->fetch())
        {
            //User never previously logged in from this steam accout - probably you should create new account now
            throw new UsernameNotFoundException(sprintf('steamid "%s" does not exist.', $steamid));
        }

        return new SteamAuthUser($user['steamid'], explode(',', $user['Roles']));
    }

    public function loadUserByUsername($steamid)
    {
        //Retain original method operating for compatibility
        return $this->loadUserBySteamId($steamid);
    }

    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof SteamAuthUser)
        {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
        }

        return $this->loadUserBySteamId($user->getSteamID());
    }

    public function supportsClass($class)
    {
        return $class === 'SteamAuth\SteamAuthUser';
    }

}