D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
tattooscyy
/
api2
/
app
/
Http
/
Controllers
/
Filename :
IosController.php
back
Copy
<?php namespace App\Http\Controllers; use App\Models\User; use Kissdigitalcom\AppleSignIn\ClientSecret; use Illuminate\Support\Str; use JWTAuth; use Illuminate\Http\Request; class IosController extends Controller { private $clientIdWeb = 'com.web.tattooscalculator'; private $clientIdApp = 'com.app.tattooscalculator'; private $teamId = 'TTB65U3XQ6'; private $keyId = '6G7D3T3WP5'; private $redirectUrl = 'https://example-app.com/redirect'; private $certPath; public function __construct(){ $this->certPath = public_path() . '/AuthKey_6G7D3T3WP5.p8'; } public function getIndex(){ } public function getString($client){ $clientSecretString = $this->code($client); echo $clientSecretString; } public function getAnswer(Request $request, $client){ $clientSecretString = $this->code($client); if($client == 'web'){ $client_id = $this->clientIdWeb; }else{ $client_id = $this->clientIdApp; } if($request->code){ $response = $this->http('https://appleid.apple.com/auth/token', [ 'grant_type' => 'authorization_code', 'code' => $request->code, 'redirect_uri' => $this->redirectUrl, 'client_id' => $client_id, 'client_secret' => $clientSecretString, ]); $arr = json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $response->id_token)[1])))); $user = User::where('ios_sub', $arr->sub)->first(); if (!$user) { $name_arr = explode('@', $arr->email); $user = User::create([ 'ios_sub' => $arr->sub, 'name' => $name_arr[0], 'email' => isset($arr->email) ? $arr->email : Str::random(10).'@tattooscalculator.com', 'password' => bcrypt(11111111) ]); } if(!$user->ios_sub){ $user->ios_sub = $arr->sub; $user->save(); } $token = JWTAuth::fromUser($user); $response_answer = [ 'token' => $token, 'token_type' => 'Bearer', 'user' => $user, ]; return response()->json($response_answer); }else{ return response()->json(['message'=>'send code in request']); } } private function code($client){ if($client == 'web'){ $clientId = $this->clientIdWeb; }elseif($client == 'app'){ $clientId = $this->clientIdApp; }else{ return response()->json(['message'=>'error, choose web or app']); } $teamId = $this->teamId; $keyId = $this->keyId; $certPath = $this->certPath; $clientSecret = new ClientSecret($clientId, $teamId, $keyId, $certPath); return $clientSecret->generate(); } private function http($url, $params=false){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if($params) curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Accept: application/json', 'User-Agent: curl', # Apple requires a user agent header at the token endpoint ]); $response = curl_exec($ch); return json_decode($response); } }