I have an REST service on my webserver, written in php. I was wondering, what would be the best authentication (besides basic http access authentication). I\'ve heared of token-
It can be done either way, and values in a GET request aren't really any more visible than values in a POST request. If anybody can "see" (i.e. intercept) the request, he can see everything you're sending. In the end an HTTP request is just a bunch of HTTP headers possibly followed by a body. The URL is send in the first GET /foo/bar HTTP/1.1
line, other values are just send in different, following lines.
So it's up to you where you expect your authentication token to be send. You can require it to be a query parameter that is appended to every request:
GET /foo/bar?user=123456&token=abcde...
To really use the HTTP protocol as intended though, you should use the Authorization
HTTP header:
Authorization: MyScheme 123456:abcde...
The content of this header is entirely up to you. It usually specifies an authorization method like Basic
, followed by whatever you want to require for authentication. This can simply be the username and password, a hash of them, an opaque token the client has obtained at some point or anything else really.
I'd recommend a token system or a request signing system, with the latter being very much preferred. In a request signing system, the client has to obtain a token from you. It then sends a hash of this token and certain characteristics of the request to authenticate the request, e.g. sha1(Token + Timestamp + Request URL + Request Body)
. Your server can validate this without the client having to send the token in plain text on each request.
How do I make the token only valid for a specific time?
You save the token server-side with an expiration timestamp and check against it.
Here's a question about token-based authentication. I think the most common token-based authentication today is OAuth. But to answer your questions:
On a GET: Is the token send visible? (isn't that unsafe?)
You can pass your tokens through HTTP headers so they are not so easily seen. OAuth allows this. Note that the tokens are still visible, they're just not in the GET
query parameters.
How do I make the token only valid for a specific time?
Since you control (create) the tokens, you can set expiry dates for each token. On every request of your API, you should just check your token storage (e.g. Database) if the given token is still valid. If it is not, then you can abort the request (maybe return a HTTP 401 error).
You can use fire-base php JWT (JSON Web Token) for token based authentication.
1)Install php jwt by running composer command composer require firebase/php-jwt
require_once('vendor/autoload.php');
use \Firebase\JWT\JWT;
define('SECRET_KEY','Your-Secret-Key') // secret key can be a random string and keep in secret from anyone
define('ALGORITHM','HS512')
After that Generate your token
$tokenId = base64_encode(mcrypt_create_iv(32));
$issuedAt = time();
$notBefore = $issuedAt + 10; //Adding 10 seconds
$expire = $notBefore + 7200; // Adding 60 seconds
$serverName = 'http://localhost/php-json/'; /// set your domain name
/*
* Create the token as an array
*/
$data = [
'iat' => $issuedAt, // Issued at: time when the token was generated
'jti' => $tokenId, // Json Token Id: an unique identifier for the token
'iss' => $serverName, // Issuer
'nbf' => $notBefore, // Not before
'exp' => $expire, // Expire
'data' => [ // Data related to the logged user you can set your required data
'id' => "set your current logged user-id", // id from the users table
'name' => "logged user name", // name
]
];
$secretKey = base64_decode(SECRET_KEY);
/// Here we will transform this array into JWT:
$jwt = JWT::encode(
$data, //Data to be encoded in the JWT
$secretKey, // The signing key
ALGORITHM
);
$unencodedArray = ['jwt' => $jwt];
provide this token to your user "$jwt" . On each request user need to send token value with each request to validate user.
try {
$secretKey = base64_decode(SECRET_KEY);
$DecodedDataArray = JWT::decode($_REQUEST['tokVal'], $secretKey, array(ALGORITHM));
echo "{'status' : 'success' ,'data':".json_encode($DecodedDataArray)." }";die();
} catch (Exception $e) {
echo "{'status' : 'fail' ,'msg':'Unauthorized'}";die();
}
You can get step by step full configurations for php token based authentication