@danhachuel/thunderbolt 0.5.1 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app/main.py CHANGED
@@ -7199,7 +7199,7 @@ def _render_media_provider_card(settings: dict[str, Any], cards: list[dict[str,
7199
7199
  state = create_state()
7200
7200
  st.session_state[f"canva_verifier_{card_id}"] = verifier
7201
7201
  st.session_state[f"canva_state_{card_id}"] = state
7202
- st.session_state[f"canva_authorization_url_{card_id}"] = authorization_url(client_id, redirect_uri, "design:content:read design:content:write", state, challenge)
7202
+ st.session_state[f"canva_authorization_url_{card_id}"] = authorization_url(client_id, redirect_uri, state=state, code_challenge=challenge)
7203
7203
  authorization_link = str(st.session_state.get(f"canva_authorization_url_{card_id}") or "")
7204
7204
  if authorization_link:
7205
7205
  st.markdown(f"[Abrir autorização Canva]({authorization_link})")
@@ -11,27 +11,58 @@ import requests
11
11
 
12
12
  AUTHORIZE_URL = "https://www.canva.com/api/oauth/authorize"
13
13
  TOKEN_URL = "https://api.canva.com/rest/v1/oauth/token"
14
+ _PKCE_MIN_LENGTH = 43
15
+ _PKCE_MAX_LENGTH = 128
16
+ CANVA_OAUTH_SCOPES = (
17
+ "asset:read",
18
+ "asset:write",
19
+ "design:meta:read",
20
+ "design:content:read",
21
+ "design:content:write",
22
+ )
14
23
 
15
24
 
16
25
  def _b64(value: bytes) -> str:
17
26
  return base64.urlsafe_b64encode(value).rstrip(b"=").decode("ascii")
18
27
 
19
28
 
20
- def create_pkce_pair() -> tuple[str, str]:
21
- verifier = _b64(secrets.token_bytes(64))
29
+ def generate_code_verifier() -> str:
30
+ """Create a RFC 7636 verifier using a cryptographically secure source."""
31
+ verifier = _b64(secrets.token_bytes(32))
32
+ if not _PKCE_MIN_LENGTH <= len(verifier) <= _PKCE_MAX_LENGTH:
33
+ raise RuntimeError("Falha ao gerar um code_verifier PKCE válido.")
34
+ return verifier
35
+
36
+
37
+ def generate_code_challenge(code_verifier: str) -> str:
38
+ """Derive the S256 Base64URL-without-padding PKCE challenge."""
39
+ verifier = str(code_verifier or "")
40
+ if not _PKCE_MIN_LENGTH <= len(verifier) <= _PKCE_MAX_LENGTH:
41
+ raise ValueError("code_verifier PKCE inválido.")
22
42
  challenge = _b64(hashlib.sha256(verifier.encode("ascii")).digest())
23
- return verifier, challenge
43
+ if challenge == "<CODE_CHALLENGE>":
44
+ raise RuntimeError("Foi detectado um placeholder de code_challenge.")
45
+ return challenge
46
+
47
+
48
+ def create_pkce_pair() -> tuple[str, str]:
49
+ verifier = generate_code_verifier()
50
+ return verifier, generate_code_challenge(verifier)
24
51
 
25
52
 
26
53
  def create_state() -> str:
27
54
  return _b64(secrets.token_bytes(48))
28
55
 
29
56
 
30
- def authorization_url(client_id: str, redirect_uri: str, scope: str, state: str, code_challenge: str) -> str:
57
+ def authorization_url(client_id: str, redirect_uri: str, scope: str | list[str] | tuple[str, ...] | None = None, state: str = "", code_challenge: str = "") -> str:
58
+ if not code_challenge or code_challenge == "<CODE_CHALLENGE>":
59
+ raise ValueError("Não é permitido iniciar Canva com code_challenge placeholder.")
60
+ scope_value = CANVA_OAUTH_SCOPES if scope is None else scope
61
+ scope_string = " ".join(scope_value) if isinstance(scope_value, (list, tuple)) else " ".join(str(scope_value or "").split())
31
62
  params = {
32
63
  "code_challenge": code_challenge,
33
- "code_challenge_method": "S256",
34
- "scope": " ".join(str(scope or "").split()),
64
+ "code_challenge_method": "s256",
65
+ "scope": scope_string,
35
66
  "response_type": "code",
36
67
  "client_id": str(client_id).strip(),
37
68
  "state": str(state).strip(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danhachuel/thunderbolt",
3
- "version": "0.5.01",
3
+ "version": "0.5.3",
4
4
  "description": "Thunderbolt — interface local para operação de canais faceless e motor MoneyPrinterTurbo",
5
5
  "license": "MIT",
6
6
  "main": "scripts/cli.mjs",