Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\lib;
- /**
- * Class MinecraftAuth
- * @description Integrate minecraft authentification in php project
- */
- class MinecraftAuth{
- const AUTHSERVER = "https://authserver.mojang.com/";
- /**
- * Function for call mojang authenfication and verify credentials
- *
- * @access public
- * @param string $user
- * @param string $pass
- * @return string
- */
- public function yggdrasil_authenticate($user, $pass){
- if (!(MinecraftAuth::str_contains($user, "@") && MinecraftAuth::str_contains($user, "."))){
- return "Veuillez utiliser votre adresse email pour vous connecter. Pour ce faire, vous devez avoir préalablement migré votre compte Minecraft.net auprès de Mojang.com";
- }
- $curl = curl_init(MinecraftAuth::AUTHSERVER."authenticate");
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json'
- ));
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array(
- "agent" =>
- array(
- "name" => "Minecraft",
- "version" => 1),
- "username" => $user,
- "password" => $pass
- )));
- $response = curl_exec($curl);
- curl_close($curl);
- $responsejson = json_decode($response, true);
- if (isset($responsejson["errorMessage"])){
- return "Vos informations d'authentification sont incorrectes. Veuillez vérifier votre adresse email ainsi que votre mot de passe.";
- }
- else if (isset($responsejson["selectedProfile"]) === false){
- return "Il est impératif que vous ayez une license minecraft associée à votre compte pour pouvoir vous connecter.";
- }
- else{
- return $responsejson["selectedProfile"];
- }
- }
- /**
- * Function for test whether a string contains a specific character
- *
- * @access public
- * @param string $haystack
- * @param string $needle
- * @return bool
- */
- public function str_contains($haystack, $needle){
- return strpos($haystack, $needle) !== false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement