add login method
This commit is contained in:
parent
a3bdd913f7
commit
7c5bf89e46
@ -4,8 +4,10 @@ namespace App\Controllers;
|
|||||||
class Auths extends BaseController {
|
class Auths extends BaseController {
|
||||||
|
|
||||||
public function login() {
|
public function login() {
|
||||||
//$TM_url = "https://api-transmedic1.transmedic.co.id/api/token/create";
|
$TM_url = "https://api-transmedic1.transmedic.co.id/api/token/create";
|
||||||
$TM_url = "http://rest.local/tests";
|
//$TM_url = "http://cmod-rest.local/tests";
|
||||||
|
$fileSBY = "tokens/pbmc_sby.txt";
|
||||||
|
$fileDPS = "tokens/pbmc_dps.txt";
|
||||||
|
|
||||||
$secret = "o7lf5DUxSuPKtDjlbqc2VuZD9WjQ5qAZ";
|
$secret = "o7lf5DUxSuPKtDjlbqc2VuZD9WjQ5qAZ";
|
||||||
|
|
||||||
@ -13,65 +15,85 @@ class Auths extends BaseController {
|
|||||||
//$credDPS = [ "username"=>"pbmc_bali", "password"=>"J8e29XjLmDCFuQnk" ];
|
//$credDPS = [ "username"=>"pbmc_bali", "password"=>"J8e29XjLmDCFuQnk" ];
|
||||||
|
|
||||||
$header = [
|
$header = [
|
||||||
'alg' => 'HS256', // HMAC SHA-256 algorithm
|
'typ' => 'JWT',
|
||||||
'typ' => 'JWT'
|
'alg' => 'HS256'
|
||||||
];
|
];
|
||||||
|
|
||||||
$payload = [
|
$payloadSBY = [
|
||||||
'iat' => time(),
|
'iat' => time(),
|
||||||
'username'=>"pbmc_surabaya", 'password'=>"pgcWfdwX3qEt9zaC"
|
'data'=> [
|
||||||
|
'username'=>"pbmc_surabaya",
|
||||||
|
'password'=>"pgcWfdwX3qEt9zaC"
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
$payloadDPS = [
|
||||||
|
'iat' => time(),
|
||||||
|
'data'=> [
|
||||||
|
'username'=>"pbmc_bali",
|
||||||
|
'password'=>"J8e29XjLmDCFuQnk"
|
||||||
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
/*
|
|
||||||
function base64UrlEncode($data) {
|
function base64UrlEncode($data) {
|
||||||
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
|
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
|
||||||
}
|
}
|
||||||
|
|
||||||
$encodedHeader = base64UrlEncode(json_encode($header));
|
function createJWT($header, $payload, $secret) {
|
||||||
$encodedPayload = base64UrlEncode(json_encode($payload));
|
$encodedHeader = base64UrlEncode(json_encode($header));
|
||||||
|
$encodedPayload = base64UrlEncode(json_encode($payload));
|
||||||
|
|
||||||
// Create the signature
|
$signature = hash_hmac('sha256', "$encodedHeader.$encodedPayload", $secret, true);
|
||||||
$signature = hash_hmac('sha256', "$encodedHeader.$encodedPayload", $secret, true);
|
$encodedSignature = base64UrlEncode($signature);
|
||||||
$encodedSignature = base64UrlEncode($signature);
|
|
||||||
|
|
||||||
// Combine to create the JWT
|
|
||||||
$jwt = "$encodedHeader.$encodedPayload.$encodedSignature";
|
|
||||||
|
|
||||||
|
// Combine to create the JWT
|
||||||
|
$jwt = "$encodedHeader.$encodedPayload.$encodedSignature";
|
||||||
|
return $jwt;
|
||||||
|
}
|
||||||
|
|
||||||
*/
|
function token2file($TM_url, $jwt, $file ) {
|
||||||
$header = http_build_query($header);
|
$client = \Config\Services::curlrequest();
|
||||||
$payload = http_build_query($payload);
|
try {
|
||||||
$signature = hash_hmac('sha256', "$header.$payload", $secret, true);
|
$response = $client->request('GET', $TM_url, [
|
||||||
$jwt = "$header.$payload.$signature<br/>";
|
"headers" => [
|
||||||
echo "JWT: $jwt\n";
|
"AppCode" => "2",
|
||||||
|
"Accept" => "application/json",
|
||||||
|
"Authorization" => "Bearer $jwt"
|
||||||
|
],
|
||||||
|
"body" => '',
|
||||||
|
"verify" => false
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($response->getStatusCode() == 200) {
|
||||||
|
$body = $response->getBody();
|
||||||
|
$data = json_decode($body, true);
|
||||||
|
$token = $data['data']['token'];
|
||||||
|
if (file_put_contents($file, $token) !== false) {
|
||||||
|
echo "Response saved to: " . $file . "<br/>";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
print_r($response);
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
echo "Error: " . $e->getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$jwtSBY = createJWT($header, $payloadSBY, $secret);
|
||||||
|
$jwtDPS = createJWT($header, $payloadDPS, $secret);
|
||||||
|
|
||||||
|
token2file($TM_url, $jwtSBY, $fileSBY);
|
||||||
|
token2file($TM_url, $jwtDPS, $fileDPS);
|
||||||
|
|
||||||
$client = \Config\Services::curlrequest();
|
|
||||||
try {
|
|
||||||
$response = $client->request('GET', $TM_url, [
|
|
||||||
"headers" => [
|
|
||||||
"Accept" => "application/json",
|
|
||||||
"Authorization" => "Bearer $jwt",
|
|
||||||
"AppCode" => "2"
|
|
||||||
],
|
|
||||||
"body" => '',
|
|
||||||
"verify" => false
|
|
||||||
]);
|
|
||||||
|
|
||||||
if ($response->getStatusCode() == 200) {
|
|
||||||
print_r($response);
|
|
||||||
} else {
|
|
||||||
print_r($response);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
echo "Error: " . $e->getMessage();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tests() {
|
public function tests() {
|
||||||
echo "<pre>";
|
// $token = "4469|eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOm51bGwsImF1ZCI6ImFwaS10cmFuc21lZGljMS50cmFuc21lZGljLmNvLmlkIiwiaWF0IjoxNzM4NTU2MTkxLCJleHAiOjE3Mzg2NDI1OTEsInN1YiI6bnVsbCwianRpIjoiMzRlOTk0MzctMTNmZC00OGUzLTg1NzEtMDYwYmJlMTUxNjM1IiwiZGF0YSI6eyJ1c2VybmFtZSI6InBibWNfc3VyYWJheWEiLCJwYXNzd29yZCI6InBnY1dmZHdYM3FFdDl6YUMifX0.hSrs1fkdwi0d7IAsCRiD20QqeQsFUxGd_06NKcjxcWw";
|
||||||
print_r($this);
|
$filePath = 'tokens/pbmc_sby.txt';
|
||||||
echo "</pre>";
|
$fileContents = file_get_contents($filePath);
|
||||||
|
echo $fileContents;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
1
public/tokens/pbmc_dps.txt
Normal file
1
public/tokens/pbmc_dps.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
4484|eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOm51bGwsImF1ZCI6ImFwaS10cmFuc21lZGljMS50cmFuc21lZGljLmNvLmlkIiwiaWF0IjoxNzM4NTYxMTQwLCJleHAiOjE3Mzg2NDc1NDAsInN1YiI6bnVsbCwianRpIjoiN2Q3YTA3MzItMzg3Ny00ZmVjLThjOWYtMWVmYWZmODgwNmU4IiwiZGF0YSI6eyJ1c2VybmFtZSI6InBibWNfYmFsaSIsInBhc3N3b3JkIjoiSjhlMjlYakxtRENGdVFuayJ9fQ.HQAuZIBYDpYgPdO8U69m5O8n254DrularsnulFfVzTQ
|
||||||
@ -0,0 +1 @@
|
|||||||
|
4483|eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOm51bGwsImF1ZCI6ImFwaS10cmFuc21lZGljMS50cmFuc21lZGljLmNvLmlkIiwiaWF0IjoxNzM4NTYxMTM5LCJleHAiOjE3Mzg2NDc1MzksInN1YiI6bnVsbCwianRpIjoiYTgxMWIzZjktNTRlNC00MzI0LWIyOGUtN2IwZmZjOTQ3YWY3IiwiZGF0YSI6eyJ1c2VybmFtZSI6InBibWNfc3VyYWJheWEiLCJwYXNzd29yZCI6InBnY1dmZHdYM3FFdDl6YUMifX0.M6dbVUv0SxFReWtFNUa2JHPt85Qaq2zO0es7B-YmNaM
|
||||||
Loading…
x
Reference in New Issue
Block a user