Generate JW Token
A token is requested via the tokens resource. For that, call the POST route /application/tokens with username and password in the HTTP request body:
{
"username": "jdoe",
"password": "2r5iq|!O%{o@?ApR1q%8"
}
Please note: As the password is converted into plain text, a secure connection (HTTPS) should be used.
The response text is returned in the following JSON format:
{
"tokens": [
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.ey..."
]
}
The token is valid for 600 seconds by default. If you want to create a token with a longer period of validity, specify this in the parameter lifetime in the HTTP request body. The permitted maximum duration is 3600 seconds. After the period of validity has expired a new token has to be created.
{
"username": "jdoe",
"password": "2r5iq|!O%{o@?ApR1q%8",
"lifetime": 1200
}
The following requests transmit the token into a X-JobRouter-Authorization header. Example:
X-Jobrouter-Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.ey...
Example
// Client has been initialized already
try {
// Authenticate user
$response = $client->post('application/tokens', [
'json' => [
'username' => 'jdoe',
'password' => '2r5iq|!O%{o@?ApR1q%8',
],
]);
// Retrieve token
$tokenData = json_decode($response->getBody(), true);
$token = $tokenData['tokens'][0];
echo "Token: " . $token;
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
$data = '{
"username": "jdoe",
"password": "2r5iq|!O%{o@?ApR1q%8"
}';
$headers = [
'Accept: application/json',
'Content-Type: application/json',
];
$url = 'http://example.org/jobrouter/api/rest/v2/application/tokens;
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_POST, 1);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlHandle, CURLOPT_COOKIEJAR, 'path/to/cooke/file.cookie');
$response = curl_exec($curlHandle);
$code = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
if ($code === 201) {
$response = json_decode($response, true);
$token = $response['tokens'][0];
} else {
echo 'Error during Authentication: ' . $code;
echo var_export($response, true);
}
curl_close();