Skip to content

Commit

Permalink
Stub out some Patreon integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosuav committed Nov 15, 2024
1 parent 3c49f7c commit e8a9c14
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions httpstatic/chan_integrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ on("submit", ".token", e => {
ws_sync.send({cmd: "settoken", token: e.match.elements.token.value, platform: e.match.dataset.platform});
e.match.elements.token.value = "";
});

on("click", "#patreonlogin", e => {
e.preventDefault();
ws_sync.send({cmd: "patreonlogin"});
});

export function sockmsg_patreonlogin(msg) {
window.open(msg.uri, "login", "width=525, height=900");
}
20 changes: 20 additions & 0 deletions modules/http/chan_integrations.pike
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ that looks something like: `8e7d24cf-66b4-4695-a651-3e744df5a861`<br>Paste it he
Once this is complete, Fourth Wall events will begin showing up in [Alerts](alertbox#fourthwall) and
anywhere else they end up getting added.
## Enabling Patreon notifications
[Link your Patreon account](:#patreonlogin)
$$loginprompt||$$
";

Expand Down Expand Up @@ -226,4 +230,20 @@ __async__ mapping get_chan_state(object channel, string grp, string|void id) {
]);
}

//Note that this message comes to the bot that's active as of when you click the button,
//and the eventual redirect from Patreon will come to the bot that's active at that time.
//If there's a bot handover during that time, the login will have to be restarted.
@"is_mod": mapping wscmd_patreonlogin(object channel, mapping(string:mixed) conn, mapping(string:mixed) msg) {
string tok = String.string2hex(random_string(8));
G->G->patreon_csrf_states[tok] = (["timestamp": time(), "channel": channel->login]);
object uri = Standards.URI("https://www.patreon.com/oauth2/authorize");
uri->set_query_variables(([
"response_type": "code",
"client_id": G->G->instance_config->patreon_clientid,
"redirect_uri": "https://" + G->G->instance_config->local_address + "/patreon", //Or should it always go to mustardmine.com?
"state": tok,
]));
return (["cmd": "patreonlogin", "uri": (string)uri]);
}

protected void create(string name) {::create(name);}
32 changes: 32 additions & 0 deletions modules/http/patreon.pike
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//Small stub page to handle Patreon redirects
//Once it's checked the CSRF state, it will redirect the user to the relevant channel page.
inherit annotated;
inherit http_websocket;

@retain: mapping patreon_csrf_states = ([]);

__async__ mapping(string:mixed)|string http_request(Protocols.HTTP.Server.Request req) {
mapping state = m_delete(patreon_csrf_states, req->variables->state);
if (!state) return "Unable to login, please close this window and try again";
object res = await(Protocols.HTTP.Promise.post_url("https://www.patreon.com/api/oauth2/token",
Protocols.HTTP.Promise.Arguments((["headers": ([
"Content-Type": "application/x-www-form-urlencoded",
]), "data": Protocols.HTTP.http_encode_query(([
"code": req->variables->code,
"grant_type": "authorization_code",
"client_id": G->G->instance_config->patreon_clientid,
"client_secret": G->G->instance_config->patreon_clientsecret,
"redirect_uri": "https://" + G->G->instance_config->local_address + "/patreon",
]))]))
));
mapping auth = Standards.JSON.decode_utf8(res->get());
res = await(Protocols.HTTP.Promise.get_url("https://www.patreon.com/api/oauth2/v2/identity",
Protocols.HTTP.Promise.Arguments((["headers": ([
"Authorization": "Bearer " + auth->access_token,
])]))
));
werror("Identity: %O\n", Standards.JSON.decode_utf8(res->get()));
return "Testing";
}

protected void create(string name) {::create(name);}

0 comments on commit e8a9c14

Please sign in to comment.