Untitled

 avatar
unknown
php
a year ago
1.9 kB
10
Indexable
<?php


protected function getJWKs()
    {
        try{
            $response = $this->client->get(env('GOVBR_JWK_URL'));
            if ($response->getStatusCode() !== 200) {
                Log::error('Falha ao obter JWKs', ['status_code' => $response->getStatusCode()]);
                throw new \Exception('Falha ao obter JWKs.');
            }
            $jwkSet = json_decode($response->getBody(), true);
            return JWK::parseKeySet($jwkSet);
        } catch (\Exception $e) {
            Log::error('Erro ao obter JWKs', ['message' => $e->getMessage()]);
            throw new \Exception('Erro ao obter JWKs.');
        }
    }

    public function validateToken(string $token)
    {
        $jwkKeys = $this->getJWKs();
        $algorithms = new \stdClass();
        $algorithms->algs = ['RS256']; // Lista de algoritmos permitidos
        try {
            $decodedToken = JWT::decode($token, $jwkKeys, $algorithms);
            return (array)$decodedToken;
        } catch (\Exception $e) {
            Log::error('Falha na validação do token', ['message' => $e->getMessage()]);
            throw new \Exception('Token inválido.');
        }
    }

    public function extractAccessTokenClaims(string $accessToken)
    {
        try{
            return $this->validateToken($accessToken);
        } catch(\Exception $e) {
            Log::error('Erro ao extrair claims do Access Token', ['message' => $e->getMessage()]);
            throw new \Exception('Erro ao extrair claims do Access Token.');
        }
    }

    public function extractIdTokenClaims(string $idToken)
    {
        try{
            return $this->validateToken($idToken);
        } catch (\Exception $e) {
            Log::error('Erro ao extrair claims do ID Token', ['message' => $e->getMessage()]);
            throw new \Exception('Erro ao extrair claims do ID Token.');
        }
    }
Editor is loading...
Leave a Comment