From 3ad1d22f46794b853a59df989b1329860d42e051 Mon Sep 17 00:00:00 2001 From: Bastard Operator Date: Thu, 25 Nov 2021 15:53:32 +0100 Subject: [PATCH] Add API to get Mastodon new Authorization OAUTH URL --- public/api/signup/mastodon/get_client.php | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 public/api/signup/mastodon/get_client.php diff --git a/public/api/signup/mastodon/get_client.php b/public/api/signup/mastodon/get_client.php new file mode 100644 index 0000000..1b5610b --- /dev/null +++ b/public/api/signup/mastodon/get_client.php @@ -0,0 +1,54 @@ + + 'error', + 'message' => lr('error.instance_not_exists', + 'Instance does not exist or is incorrect.') + ])); +} + +$app_name = conf('app_name', 'RealFan'); +$site_name = conf('site_name', 'http://127.0.0.1:8080'); +$redirect_uris = conf('site_name', 'http://127.0.0.1:8080'); +$scopes = 'read write'; + +# create the Authorization App +$ch = curl_init($instance.'/api/v1/apps'); +curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ + 'client_name' => $app_name, + 'redirect_uris' => $redirect_uris, + 'website' => $site_name, + 'scopes' => $scopes, +])); +curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +$result = curl_exec($ch); +curl_close($ch); + + +# result must be JSON +$result = @json_decode($result); +if ($result === null) { + http_response_code(500); + die(json_encode([ + 'status' => 'error', + 'message' => lr('error.instance_failed_or_not_mastodon', + 'Instance failed to create an authorization App. Is it an instance?') + ])); +} + +# create the URL for OAUTH +$query_string = http_build_query([ + 'client_id' => $result->client_id ?? null, + 'redirect_uris' => $redirect_uris, + 'response_type' => 'code', + 'scope' => $scopes +]); +echo json_encode([ + 'status' => 'ok', + 'message' => $instance.'/oauth/autorize?'.$query_string +]);