|
35 | 35 | (function() { |
36 | 36 | const token = "%s"; |
37 | 37 | const data = JSON.stringify({token: token, provider: "github"}); |
| 38 | + const msg = "authorization:github:success:" + data; |
38 | 39 |
|
39 | | - // Sveltia/Decap CMS sends "authorizing:github" to the popup; |
40 | | - // the popup replies with the token and closes itself. |
| 40 | + function sendToken(origin) { |
| 41 | + window.opener.postMessage(msg, origin || "*"); |
| 42 | + window.close(); |
| 43 | + } |
| 44 | +
|
| 45 | + // Listen for "authorizing:github" from the parent (Sveltia CMS protocol) |
41 | 46 | window.addEventListener("message", function(e) { |
42 | 47 | if (e.data === "authorizing:github") { |
43 | | - window.opener.postMessage( |
44 | | - "authorization:github:success:" + data, |
45 | | - e.origin |
46 | | - ); |
47 | | - window.close(); |
| 48 | + sendToken(e.origin); |
48 | 49 | } |
49 | 50 | }, false); |
| 51 | +
|
| 52 | + // Also notify the parent we're ready (Decap CMS protocol), |
| 53 | + // which triggers it to send "authorizing:github" back to us. |
| 54 | + window.opener.postMessage("authorizing:github", "*"); |
50 | 55 | })(); |
51 | 56 | </script></body></html> |
52 | 57 | """ |
@@ -94,7 +99,15 @@ async def callback(code: str) -> ASGIResponse: |
94 | 99 | headers={"Accept": "application/json"}, |
95 | 100 | ) |
96 | 101 | resp.raise_for_status() |
97 | | - token = resp.json()["access_token"] |
| 102 | + body = resp.json() |
| 103 | + token = body.get("access_token") |
| 104 | + if not token: |
| 105 | + error = body.get("error_description", body.get("error", "unknown error")) |
| 106 | + return ASGIResponse( |
| 107 | + body=f"OAuth token exchange failed: {error}".encode(), |
| 108 | + media_type="text/plain", |
| 109 | + status_code=400, |
| 110 | + ) |
98 | 111 | return ASGIResponse(body=(CALLBACK_HTML % token).encode(), media_type="text/html") |
99 | 112 |
|
100 | 113 |
|
|
0 commit comments