youthweb / php-youthweb-api

🔌 PHP Youthweb API is an object-oriented wrapper for PHP of the Youthweb API.
GNU General Public License v3.0
3 stars 1 forks source link

Oauth2-Provider #9

Closed Art4 closed 7 years ago

Art4 commented 7 years ago

WIP

Todo:

Das folgende kleine Script zeigt einen Login-Button für Youthweb. Wenn darauf geklickt wird und der App Zugriff gegeben wird, zeigt sie den Namen und die Email-Adresse des Users an.

Installation der benötigten Komponenten über Composer:

php composer.phar require youthweb/php-youthweb-api:dev-master
php composer.phar require cache/filesystem-adapter
<?php

// Config
$client_id = 'id';
$client_secret = 'secret';
$redirect_url = 'http://example.org/login-button.php';

require 'vendor/autoload.php';

use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Cache\Adapter\Filesystem\FilesystemCachePool;
use Youthweb\Api\Exception\UnauthorizedException;

$filesystemAdapter = new Local(__DIR__.'/');
$filesystem        = new Filesystem($filesystemAdapter);

$pool = new FilesystemCachePool($filesystem, 'cache');

$client = new Youthweb\Api\Client([
    'client_id'     => $client_id,
    'client_secret' => $client_secret,
    'redirect_url'  => $redirect_url,
    'scope'         => ['user:email'],
], [
    'cache_provider' => $pool,
]);

echo '<h1>Mit Youthweb einloggen</h1>';
echo '<form method="get" action="a.php">
<input name="go" value="Login" type="submit" />
</form>';

if ( isset($_GET['go']) )
{
    try
    {
        $client->authorize();
    }
    catch ( UnauthorizedException $e )
    {
        header('Location: '.$e->getUrl());
        exit;
    }

    $me = $client->getResource('users')->showMe();

    printf('<p>Hallo %s %s!</p>', $me->get('data.attributes.first_name'), $me->get('data.attributes.last_name'));
    printf('<p>Deine Email-Adresse: %s', $me->get('data.attributes.email'));
}
elseif ( isset($_GET['code']) )
{
    $client->authorize([
        'code' => $_GET['code'],
        'state' => $_GET['state'],
    ]);

    header('Location: '.$redirect_url.'?go=Login');
    exit;
}

die;
coveralls commented 7 years ago

Coverage Status

Coverage decreased (-17.0%) to 83.007% when pulling e34a0fb52842989fc8e59793cbc7df3a92e51d0e on oauth2 into fc8620512d1165f2f54b6b80fb0410b59d185554 on develop.

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-3.1%) to 96.855% when pulling 1e82cc02c17faae8b1a1664a6fa22bf14f786224 on oauth2 into fc8620512d1165f2f54b6b80fb0410b59d185554 on develop.

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-3.1%) to 96.855% when pulling ad7c53b03ecd79272cbe6fa06f0d1ff13708049d on oauth2 into fc8620512d1165f2f54b6b80fb0410b59d185554 on develop.

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-1.9%) to 98.113% when pulling 6b91449765d52ac5b084a0c55d1d7e4c5174c899 on oauth2 into fc8620512d1165f2f54b6b80fb0410b59d185554 on develop.

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.6%) to 99.371% when pulling f41d2ee208e770892fcbf8859933705c07bb25e9 on oauth2 into fc8620512d1165f2f54b6b80fb0410b59d185554 on develop.

coveralls commented 7 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling e0bb8e8f6d2f61c8592e3fd45065af362b22f1ec on oauth2 into fc8620512d1165f2f54b6b80fb0410b59d185554 on develop.

coveralls commented 7 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling f2e4ce8b7d37ea651fc9dce9698009079f47a3ac on oauth2 into fc8620512d1165f2f54b6b80fb0410b59d185554 on develop.

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.6%) to 99.429% when pulling 49a6786630508abc58be6553e70f42dc9eda4d7e on oauth2 into fc8620512d1165f2f54b6b80fb0410b59d185554 on develop.

coveralls commented 7 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling ee49918473cb5c8fd9f67d0259153d5a4e9405fd on oauth2 into fc8620512d1165f2f54b6b80fb0410b59d185554 on develop.