83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
class Auths extends BaseController {
|
|
|
|
public function loginDPS() {
|
|
$file = "tokens/pbmc_dps.txt";
|
|
//$TM_url = "https://staging-eklinik.pbmcgroup.com/api/token/create";
|
|
$TM_url = "http://10.10.12.90:8001/api/token/create";
|
|
$secret = "o7lf5DUxSuPKtDjlbqc2VuZD9WjQ5qAZ";
|
|
$header = [ 'typ' => 'JWT', 'alg' => 'HS256' ];
|
|
$payload = [
|
|
'iat' => time(),
|
|
'data'=> [
|
|
'username'=>"pbmc_bali",
|
|
'password'=>"J8e29XjLmDCFuQnk"
|
|
]
|
|
];
|
|
$jwt = $this->createJWT($header, $payload, $secret);
|
|
$this->token2file($TM_url, $jwt, $file);
|
|
}
|
|
|
|
public function loginSBY() {
|
|
$TM_url = "http://10.10.4.123:8001/api/token/create";
|
|
$file = "tokens/pbmc_sby.txt";
|
|
$secret = "o7lf5DUxSuPKtDjlbqc2VuZD9WjQ5qAZ";
|
|
$header = [ 'typ' => 'JWT', 'alg' => 'HS256' ];
|
|
$payload = [
|
|
'iat' => time(),
|
|
'data'=> [
|
|
'username'=>"pbmc_surabaya",
|
|
'password'=>"pgcWfdwX3qEt9zaC"
|
|
]
|
|
];
|
|
$jwt = $this->createJWT($header, $payload, $secret);
|
|
$this->token2file($TM_url, $jwt, $file);
|
|
}
|
|
|
|
private function base64UrlEncode($data) {
|
|
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
|
|
}
|
|
|
|
private function createJWT($header, $payload, $secret) {
|
|
$encodedHeader = $this->base64UrlEncode(json_encode($header));
|
|
$encodedPayload = $this->base64UrlEncode(json_encode($payload));
|
|
|
|
$signature = hash_hmac('sha256', "$encodedHeader.$encodedPayload", $secret, true);
|
|
$encodedSignature = $this->base64UrlEncode($signature);
|
|
|
|
// Combine to create the JWT
|
|
$jwt = "$encodedHeader.$encodedPayload.$encodedSignature";
|
|
return $jwt;
|
|
}
|
|
|
|
private function token2file($TM_url, $jwt, $file ) {
|
|
$client = \Config\Services::curlrequest();
|
|
try {
|
|
$response = $client->request('GET', $TM_url, [
|
|
"headers" => [
|
|
"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();
|
|
}
|
|
}
|
|
|
|
} |