Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
2.1 kB
2
Indexable
Never
<?php

namespace App\Providers;

use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Http;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;

class SadayanaServiceProvider extends AbstractProvider
{
    protected $scopes = ['openid'];

    protected function getAuthUrl($state)
    {
        return $this->buildAuthUrlFromBase(config('services.sadayana.base_uri') . '/oauth/authorize', $state);
    }

    protected function getTokenUrl()
    {
        return config('services.sadayana.base_uri') . '/oauth/token';
    }

    public function getAccessToken($code)
    {
        $response = Http::post($this->getTokenUrl(), $this->getTokenFields($code))->json();
        return $response['access_token'];
    }

    protected function getTokenFields($code)
    {
        return parent::getTokenFields($code);
    }

    protected function getUserByToken($token)
    {
        $token = Http::withToken($token)->post(config('services.sadayana.base_uri') . '/api/usertoken')->json();

        return $token;
    }

    protected function formatScopes(array $scopes, $scopeSeparator)
    {
        return implode($scopeSeparator, $scopes);
    }

    protected function mapUserToObject(array $user)
    {
        return (new User)->setRaw($user)->map([
            'name' => $user['nickname'],
            'email' => $user['email'],
            'nickname' => $user['nickname'],
            'username' => $user['username'],
            'avatar' => !empty($user['basic']['picture_link']) ? $user['basic']['picture_link'] : null,
            'basic' => $user['basic']
        ]);
    }

    public function redirect()
    {
        $state = null;

        if ($this->usesState()) {
            $this->request->session()->put('state', $state = $this->getState());
        }

        if ($this->usesPKCE()) {
            $this->request->session()->put('code_verifier', $this->getCodeVerifier());
        }

        return new RedirectResponse($this->getAuthUrl($state));
    }
}
Leave a Comment