php - How to create a Logout function in Processmaker 3.0 using the Rest API? -
i developing 1 rest api in process-maker 3.0. in user can login using password oauth2.0 authorization.
we access token , oauthcredential.json automatically updated. when user logged in credentials (client_id, client_secret, username , password) cookie sets. , directs rest endpoints suggesting in link: http://wiki.processmaker.com/3.0/calling_rest_endpoints
when cookies not set or cleared should redirect login page or when user click on logout button redirect login page.
code login page
'<html><head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <form action="check_login.php" method="post"> client id<br> <input type="text" name="client_id" value="" width=32 /><br> client secret<br> <input type="text" name="client_secret" value="" width=32 /><br> username<br> <input type="text" name="username" value="" width=20 /><br> password<br> <input type="text" name="password" value="" width=20 /><br> <input type="submit" value="login"/> </form> </body> </html>
after successful login goes checklogin.php page
<?php $clientid=isset($_post['client_id']); $clientsecret=isset($_post['clientsecret']); $username=isset($_post['username']); $password=isset($_post['password']); //change server address , workspace match system: $pmserver = "http://127.0.0.1/api/1.0/workflow"; $pmworkspace = 'workflow'; function pmrestlogin($clientid, $clientsecret, $username, $password) { global $pmserver, $pmworkspace; $postparams = array( 'grant_type' => 'password', 'scope' => '*', //set 'view_process' if not changing process 'client_id' => $clientid, 'client_secret' => $clientsecret, 'username' => $username, 'password' => $password ); echo "after function"; $ch = curl_init("$pmserver/oauth2/token"); curl_setopt($ch, curlopt_timeout, 30); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $postparams); curl_setopt($ch, curlopt_returntransfer, true); $otoken = json_decode(curl_exec($ch)); $httpstatus = curl_getinfo($ch, curlinfo_http_code); curl_close($ch); if ($httpstatus != 200) { print "error in http status code: $httpstatus\n"; return null; } else if (isset($otoken->error)) { print "error logging $pmserver:\n" . "error: {$otoken->error}\n" . "description: {$otoken->error_description}\n"; } else { //at point $otoken->access_token can used call rest endpoints. //if planning use access_token later, either save access_token //and refresh_token cookies or save them file in secure location. //if saving them cookies: setcookie("access_token", $otoken->access_token, time() + 60*5); setcookie("refresh_token", $otoken->refresh_token); //refresh token doesn't expire setcookie("client_id", $clientid); setcookie("client_secret", $clientsecret); echo "saving cred in file"; //if saving file: file_put_contents("oauthcredentials.json", json_encode($otoken)); //include path in filename if not located in same directory: } return $otoken; } $otoken = pmrestlogin($_post['client_id'], $_post['client_secret'],$_post['username'], $_post['password']); if (isset($otoken) , isset($otoken->access_token)) { //can call rest endpoints using $otoken->access_token // $oret = pmrestrequest("get", "/api/1.0/workflow/users", null, $otoken- >access_token); header("location: cases.php"); } ?>
after login goes cases.php `
<?php $pmserver = "http://127.0.0.1"; //set processmaker address $accesstoken = isset($_cookie['access_token']) ? $_cookie['access_token'] : getaccesstoken(); /*check cookie expired or not*/ if (empty($accesstoken) , isset($_cookie['access_token'])) $accesstoken = $_cookie['access_token']; if (empty($accesstoken)) { //if access token has expired //to check if pm login session has expired: !isset($_cookie['phpsessid']) header("location: formlogin.php"); //change match login method die(); } /***************************/ $ch = curl_init($pmserver . "/api/1.0/workflow/users"); curl_setopt($ch, curlopt_httpheader, array("authorization: bearer " . $accesstoken)); curl_setopt($ch, curlopt_returntransfer, true); $ausers = json_decode(curl_exec($ch)); $statuscode = curl_getinfo($ch, curlinfo_http_code); curl_close($ch); if ($statuscode != 200) { /*if (isset ($ausers) , isset($ausers->error)) print "error code: {$ausers->error->code}\nmessage: {$ausers->error->message}\n"; else print "error: http status code: $statuscode\n";*/ header("location: formlogin.php"); //change match login method die(); } else { foreach ($ausers $ouser) { if ($ouser->usr_status == "active") { print "{$ouser->usr_firstname} {$ouser->usr_lastname} ({$ouser->usr_username})\n"; } } } ?> <body> <div data-role="page"> <div data-role="header" data-position="fixed"> <h1>my cases</h1> </div> <div role="main" class="ui-content"> <ul data-role="listview" data-inset="false" data-divider-theme="a"> <li data-role="list-divider">home</li> <li><a href="todo-list.html">inbox</a></li> <li><a href="jlogin.html">logout</a></li> </ul> </div> <div data-role="footer" data-position="fixed"> </div> </div> </body> </html>`
in cases.php when session id not set should redirect formlogin.php functionality not work properly.
thanks in advance.
i advise against using http codes checking if logged in. reason being: if there error code, example, 404 or else did not work properly, user didn't wish log out, should able handle in application ui.
instead, suggest using session variable store access token , when user clicks on logout button, destroy variable , redirect them login page.
here example of application built using processmaker rest api allows login via oauth 2 authorization code grant type , handles logging out. difference between application , yours mine spa written in angularjs , yours in php. concepts same though.
Comments
Post a Comment