85 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			85 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Srgssr;
 | 
						|
 | 
						|
use Illuminate\Support\Facades\Http;
 | 
						|
use App\Models\Endpoint;
 | 
						|
 | 
						|
 | 
						|
class Auth
 | 
						|
{
 | 
						|
 | 
						|
    protected $key;
 | 
						|
    protected $secret;
 | 
						|
 | 
						|
    /**
 | 
						|
     * OAuth for SRG-SSR API.
 | 
						|
     * Gets token needed for calls to protected SRG-SSR APIs.
 | 
						|
     * https://developer.srgssr.ch/getting-started/how-update-srg-ssr-oauth-security
 | 
						|
     * @param string $key ClientId
 | 
						|
     * @param string $secret CleintSecret
 | 
						|
     * @return array|null
 | 
						|
     */
 | 
						|
    public static function accessToken($key, $secret) : ?array
 | 
						|
    {
 | 
						|
 | 
						|
        $authKey = base64_encode("{$key}:{$secret}");
 | 
						|
 | 
						|
        $response = Http::withHeaders([
 | 
						|
            'Authorization' => 'Basic '.$authKey,
 | 
						|
            'Cache-Control' => 'no-cache',
 | 
						|
            'Content-Length' => 0,
 | 
						|
            'Postman-Token' => 'XX264e32-2de0-f1e3-f3f8-eab014bbXX76'
 | 
						|
        ])->post('https://api.srgssr.ch/oauth/v1/accesstoken?grant_type=client_credentials');
 | 
						|
 | 
						|
        if($response->ok()){
 | 
						|
            return $response->json();
 | 
						|
        }
 | 
						|
 | 
						|
        return null;
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Oauth renewal.
 | 
						|
     * Retrieve new token and save to database.
 | 
						|
     * @param string $endpoint
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function renewal(string $endpoint) : string
 | 
						|
    {
 | 
						|
 | 
						|
        $settings = Endpoint::firstWhere('uri', $endpoint)->settings;
 | 
						|
        $token = self::accessToken($settings['key'], $settings['secret']);
 | 
						|
 | 
						|
        Endpoint::updateOrCreate(
 | 
						|
            ['uri' => $endpoint.'#oauth'],
 | 
						|
            ['settings' => $token]
 | 
						|
        );
 | 
						|
 | 
						|
        return $token['access_token'];
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Retrive Token.
 | 
						|
     * Retrive token from database or renew if is expired.
 | 
						|
     * @return string
 | 
						|
     * @param string $endpoint
 | 
						|
     */
 | 
						|
    public static function token($endpoint) : string
 | 
						|
    {
 | 
						|
 | 
						|
        $oauth = Endpoint::firstWhere('uri', $endpoint.'#oauth');
 | 
						|
        $settings = $oauth->settings;
 | 
						|
        $expiryDate = strtotime($oauth->updated_at) + $settings['expires_in'];
 | 
						|
 | 
						|
        if($expiryDate - time() <= 0){
 | 
						|
            $settings['access_token'] = self::renewal($endpoint);
 | 
						|
        }
 | 
						|
 | 
						|
        return $settings['access_token'];
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
}
 |