File size: 1,372 Bytes
8145bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Check credential shapes without contacting a provider or echoing a submitted secret."""

import re

STRIPE_WRITE_REFUSAL = (
    "This is a Stripe secret key, which can change your account. "
    "Create a restricted key with read access and paste that."
)
STRIPE_SHAPE_REFUSAL = (
    "That does not look like a Stripe key. A restricted key starts with rk_."
)
SHOPIFY_SHAPE_REFUSAL = (
    "That does not look like a Shopify Admin API token. It starts with shpat_."
)

_STRIPE_RESTRICTED = re.compile(r"rk_(?:live|test)_[A-Za-z0-9]+")
# Assumption, unverified: Admin tokens use shpat_, shpca_ or shppa_ and exactly 32 hex characters.
_SHOPIFY_ADMIN = re.compile(r"shp(?:at|ca|pa)_[0-9a-fA-F]{32}")


def check(etype, fields) -> str | None:
    """Return a fixed refusal for Stripe or Shopify, or None for an accepted shape."""
    value = fields.get("api_key") if isinstance(fields, dict) else None
    token = value if isinstance(value, str) else ""
    if etype == "stripe":
        if token.startswith("sk_"):
            return STRIPE_WRITE_REFUSAL
        # A restricted prefix cannot show permissions. Store time must not call Stripe.
        if not _STRIPE_RESTRICTED.fullmatch(token):
            return STRIPE_SHAPE_REFUSAL
    elif etype == "shopify":
        if not _SHOPIFY_ADMIN.fullmatch(token):
            return SHOPIFY_SHAPE_REFUSAL
    return None