Advertisement
Guest User

Untitled

a guest
Jan 5th, 2021
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Discord REST API</title>
  5.         <meta charset="UTF-8" />
  6.     </head>
  7.  
  8.     <body>
  9. <?php
  10.     use Elliptic\EdDSA;
  11.  
  12.     class Interaction
  13.     {
  14.         public static function verifyKey($rawBody, $signature, $timestamp, $client_public_key)
  15.         {
  16.             $ec = new EdDSA('ed25519');
  17.             $key = $ec->keyFromPublic($client_public_key, 'hex');
  18.  
  19.             $message = array_merge(unpack('C*', $timestamp), unpack('C*', $rawBody));
  20.             return $key->verify($message, $signature) == TRUE;
  21.         }
  22.     }
  23.    
  24.     abstract class InteractionResponseFlags
  25.     {
  26.         const EPHEMERAL = 1 << 6;
  27.     }
  28.    
  29.     abstract class InteractionResponseType
  30.     {
  31.         const PONG = 1;
  32.         const ACKNOWLEDGE = 2;
  33.         const CHANNEL_MESSAGE = 3;
  34.         const CHANNEL_MESSAGE_WITH_SOURCE = 4;
  35.         const ACKNOWLEDGE_WITH_SOURCE = 5;
  36.     }
  37.  
  38.     abstract class InteractionType
  39.     {
  40.         const PING = 1;
  41.         const APPLICATION_COMMAND = 2;
  42.     }
  43.  
  44.  
  45.     if(isset($_POST["type"]))
  46.     {
  47.         if($_POST["type"] === 1)
  48.         {          
  49.             $CLIENT_PUBLIC_KEY = getenv('CLIENT_PUBLIC_KEY');
  50.  
  51.             $signature = $_SERVER['HTTP_X_SIGNATURE_ED25519'];
  52.             $timestamp = $_SERVER['HTTP_X_SIGNATURE_TIMESTAMP'];
  53.             $postData = file_get_contents('php://input');
  54.  
  55.             if(Interaction::verifyKey($postData, $signature, $timestamp, $CLIENT_PUBLIC_KEY))
  56.             {
  57.                 echo json_encode(array(
  58.                 'type' => InteractionResponseType::PONG
  59.                 ));
  60.             }
  61.             else
  62.             {
  63.                 http_response_code(401);
  64.                 echo "Not verified";
  65.             }
  66.            
  67.             http_response_code(200);           
  68.             echo json_encode(array("type" => 1));
  69.         }
  70.     }
  71. ?>
  72.     </body>
  73.  
  74. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement