שגיאת כניסה בעת ניסיון להשתמש ב-Discourse כספק זהויות (SSO, DiscourseConnect)

Got it: the HMAC-SHA256 signature must be computed from the base64 encoded data, not from the url encoded data. So the correct code is:

import secrets
import base64
import urllib.parse
import hmac
import hashlib

forum_url = 'https://forum.embeetle.com'
target_url = 'https://embeetle.com/#account'
sso_secret = b'JCLSVcqbAnEPXz2p2xBY'

nonce = secrets.token_urlsafe()
payload = f'nonce={nonce}&return_sso_url={target_url}'
payload_base64 = base64.b64encode(payload.encode('utf-8')).decode()
payload_for_url = urllib.parse.quote(payload_base64)

signature = hmac.new(
    sso_secret, payload_base64.encode('utf-8'), hashlib.sha256
).hexdigest()

print(f'{forum_url}/session/sso_provider?sso={payload_for_url}&sig={signature}')

Now for the rest of the work: handling the redirect, checking the nonce, etc. To be continued …