📚 Core - Lógica Principal

GovBrConfig

class govbr_auth.core.config.GovBrConfig(*, client_id: str, client_secret: str, redirect_uri: str, cript_verifier_secret: str, govbr_auth_url: str, govbr_token_url: str, scope: str = 'openid profile email', response_type: str = 'code', code_challenge_method: str = 'S256', jwt_secret: str | None = None, jwt_algorithm: str = 'HS256', jwt_expire_minutes: int = 30, prefix: str | None = None, authorize_endpoint: str | None = None, authenticate_endpoint: str | None = None, use_fake: bool = False)[source]

Bases: BaseModel

Configuração para integração com o Gov.br. Esta classe contém as informações necessárias para autenticação e autorização com o Gov.br, incluindo URLs, credenciais e parâmetros de configuração.

Parameters:
  • client_id – ID do cliente registrado na plataforma Gov.br.

  • client_secret – Segredo do cliente registrado na plataforma Gov.br.

  • redirect_uri – URI de redirecionamento após a autenticação.

  • cript_verifier_secret – Segredo usado para criptografar o código de verificação.

  • govbr_auth_url – URL de autorização da plataforma Gov.br.

  • govbr_token_url – URL de token da plataforma Gov.br.

  • scope – Escopo de acesso solicitado (padrão: “openid profile email”).

  • response_type – Tipo de resposta esperado (padrão: “code”).

  • code_challenge_method – Método de desafio de código (padrão: “S256”).

  • jwt_secret – Segredo para assinatura de JWT (opcional).

  • jwt_algorithm – Algoritmo de assinatura JWT (padrão: “HS256”).

  • jwt_expire_minutes – Tempo de expiração do JWT em minutos (padrão: 30).

  • prefix – Prefixo para as rotas de autenticação (padrão: None).

  • authorize_endpoint – Endpoint de autorização (padrão: None).

  • authenticate_endpoint – Endpoint de autenticação (padrão: None).

  • use_fake – Ativa explicitamente o modo fake via configuração (padrão: False).

Raises:
  • ValueError – Se as variáveis obrigatórias não estiverem presentes no ambiente.

  • ValueError – Se a chave de criptografia não for válida.

client_id: str
client_secret: str
redirect_uri: str
cript_verifier_secret: str
govbr_auth_url: str
govbr_token_url: str
scope: str
response_type: str
code_challenge_method: str
jwt_secret: str | None
jwt_algorithm: str
jwt_expire_minutes: int
prefix: str | None
authorize_endpoint: str | None
authenticate_endpoint: str | None
use_fake: bool
classmethod validate_endpoint(v)[source]
classmethod validate_fernet_key(v)[source]
classmethod from_env()[source]
model_config = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

GovBrAuthorize

class govbr_auth.core.govbr.GovBrAuthorize(config: GovBrConfig)[source]

Bases: object

__init__(config: GovBrConfig)[source]
build_authorize_url() dict[source]

Build the Gov.br authorization URL with PKCE parameters.

Returns:

Dict with the authorization URL.

Raises:

GovBrException – If URL generation fails.

build_authorize_url_sync() dict[source]

GovBrIntegration

class govbr_auth.core.govbr.GovBrIntegration(config: GovBrConfig)[source]

Bases: object

__init__(config: GovBrConfig)[source]
jwt_payload_decode(id_token: str, verify: bool = True) dict[source]

Decode and verify the JWT id_token.

For Gov.br production/staging, the token signature is verified using the JWKS endpoint (RSA public keys). For local/fake environments, verification uses the configured jwt_secret (HS256) or falls back to unverified decode.

Parameters:
  • id_token – The JWT id_token string.

  • verify – Whether to verify the signature (default: True).

Returns:

Decoded token payload as dict.

Raises:

GovBrAuthenticationError – If verification fails.

async async_exchange_code_for_token(code: str, state: str) dict[source]
exchange_code_for_token_sync(code: str, state: str) dict[source]

Exceções

class govbr_auth.core.govbr.GovBrException[source]

Bases: Exception

Custom exception for Gov.br related errors.

class govbr_auth.core.govbr.GovBrAuthenticationError[source]

Bases: GovBrException

Custom exception for Gov.br authentication errors.