<?php
declare(strict_types=1);
namespace PpPhp\Generated\SocialAuth;

final class GoogleProvider extends AbstractProvider
{
    public function id(): string { return 'google'; }
    public function authorizationUrl(array $t): string
    {
        return $this->query('https://accounts.google.com/o/oauth2/v2/auth', ['client_id'=>$this->value('client_id','GOOGLE_CLIENT_ID'),'redirect_uri'=>$this->callbackUri(),'response_type'=>'code','scope'=>'openid email profile','state'=>$t['state'],'nonce'=>$t['nonce'],'code_challenge'=>rtrim(strtr(base64_encode(hash('sha256',$t['verifier'],true)),'+/','-_'),'='),'code_challenge_method'=>'S256','prompt'=>'select_account']);
    }
    public function identity(array $r, array $t): array
    {
        $this->failOnProviderError($r); if (empty($r['code'])) throw new \RuntimeException('Google did not return an authorization code.');
        $tokens = $this->token('https://oauth2.googleapis.com/token', ['code'=>$r['code'],'client_id'=>$this->value('client_id','GOOGLE_CLIENT_ID'),'client_secret'=>$this->value('client_secret','GOOGLE_CLIENT_SECRET'),'redirect_uri'=>$this->callbackUri(),'grant_type'=>'authorization_code','code_verifier'=>$t['verifier']]);
        $c = JwtVerifier::verify((string)($tokens['id_token'] ?? ''),'https://www.googleapis.com/oauth2/v3/certs',['https://accounts.google.com','accounts.google.com'],$this->value('client_id','GOOGLE_CLIENT_ID'),$t['nonce']);
        return ['provider'=>'google','subject'=>(string)$c['sub'],'email'=>(string)($c['email'] ?? ''),'email_verified'=>$this->verified($c['email_verified'] ?? false),'name'=>(string)($c['name'] ?? ''),'picture'=>(string)($c['picture'] ?? ''),'raw'=>$c];
    }
}
