You are on page 1of 4

Example Service:

Aim => To get a user detail

let dataParam = {
"folderId": fldrId,
};
Create a object for service calling
this.commonserveice.getFoldersSingle(dataParam).subscribe({
next: (response) => {

Then encrypt this data


First convert the data to base 64 encode then convert to json encode this will generate the
request data
Then this json encode data will be encrypted using , this will generate the request token
CryptoJS.HmacSHA256(requestParam, environment.apiHashingKey).toString();

This will hit the api


Backend
—--------
When it will hit the api api will check the data
function hashRequestMatch($request)
{
$matchFlag = false;
$reqParams = $request->REQUEST_DATA;
$reqToken = $request->REQUEST_TOKEN;
$reqHash = hash_hmac('sha256', $reqParams, "22CSMTOOL2022");
if ($reqHash == $reqToken) {
$matchFlag = true;
}
return $matchFlag;
}

If the request token and generated token matched then will allow to enter the api.
public function getUserDetail()
{
$allData = json_decode(file_get_contents('php://input'));
if (hashRequestMatch($allData)) {
$request = json_decode(base64_decode($allData->REQUEST_DATA));
//decrypt data with base64
$requestc = (array)$request;
try {
$validate = Validator::make($requestc, [
'userId' => 'required|numeric|min:1',
]);
if ($validate->fails()) {
$respArr = array(
'status' => 400,
'message' => $validate->errors(),
'result' => ''
);
} else {
$userId = $request->userId;

DB::beginTransaction();
$userData = DB::table('dms_user_master as
a')->select('a.userId', 'a.userFullName', 'a.userMailId', 'a.roleId',
'a.desgId', 'a.deletedFlag', 'b.roleId as rid', 'b.roleName', 'c.desgId as
sid', 'c.desgName')->join('dms_role_master as b', 'a.roleId', '=',
'b.roleId')->join('dms_desg_master as c', 'c.desgId', '=',
'a.desgId')->where('a.userId', $userId)->where('a.deletedFlag',
'0')->get()->toArray();
if (!empty($userData)) {
$res = array(
'userId' => $userData[0]->userId,
'userFullName' => $userData[0]->userFullName,
'userMailId' => $userData[0]->userMailId,
'roleId' => $userData[0]->roleId,
'desgId' => $userData[0]->desgId,
'deletedFlag' => $userData[0]->deletedFlag,
'rid' => $userData[0]->rid,
'roleName' => $userData[0]->roleName,
'sid' => $userData[0]->sid,
'desgName' => $userData[0]->desgName
);
$respArr = array(
'status' => 200,
'message' => 'Data Found Successfully',
'result' => $res
);
} else {
$respArr = array(
'status' => 400,
'message' => 'No User Found',
'result' => ''
);
}
DB::commit();
}
} catch (\Exception $e) {
DB::rollBack();
$respArr = array(
'status' => 500,
'message' => $e->getMessage(),
'result' => ''
);
}
} else {
$rtnSts = 500;
$respArr = array("status" => $rtnSts, "message" => ' ',
"result" => ' ');
}

return $respArr;
}

This respArr (array format) will be converted using same mechanism


function generateResponseHash($respArr)
{

$base64EncodedArr = base64_encode(json_encode($respArr));
$hasedData = hash_hmac('sha256', $base64EncodedArr,
‘22CSMTOOL2022’);
$responseData = array('RESPONSE_DATA' => $base64EncodedArr,
'RESPONSE_TOKEN' => $hasedData);
echo json_encode($responseData);
}

You might also like