I am building a service which handle content from different websites. Each website has their own users.
So I need to authenticate the website which is accessing my API and I need to authenticate the user who is logging in from that website.
Since Yii2 can not handle 2 parallel user identities and on this scenario I can not use roles. I decided to build it in the following way:
Handling Website
I have a table called "Website", website contains 2 fields access_token and expiration_token.
From the any of the websites (which are a Yii basic installations) they do:
$data = array();
$data['api_key'] = 'xxxxx';
$data['api_secret'] = 'zzzzzz';
$client = new Client(['baseUrl' => 'https://website.api/index.php?r=v1']);
$response = $client->post('website/get-access-token', $data)->send();
echo "<pre>";
var_dump($response->content);
echo "</pre>";
if token is expired they can do another request to entry point 'website/refresh-access-token'
Then every request GET or POST they have to send this access_token, which is saved in SESSION (Token right now expires every 7 days).
This is not handled through any HTTP validation, just straight forward request to api for token.
Handling Users
For this part when a user authenticate using email/password or social networks, I do a request to User Controller, where I have set Bearer Tokens:
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => CompositeAuth::className(),
'authMethods' => [
HttpBearerAuth::className()
]
];
return $behaviors;
}
This part is a work in process, so it is suppose Website will do a request to login entry point:
$client = new Client(['baseUrl' => 'https://website.api/index.php?r=v1']);
$response = $client->post('user/login', $data)->send();
echo "<pre>";
var_dump($response->content);
echo "</pre>";
This is suppose to get access_token and also check for token expiration date.
My questions are:
Is this a good idea to handle API Requests for my particular scenario? I think there are many calls to get simple data. Is it needed to use token for Getting data?
If I want to use Bearer Tokens for Handling content and User Access how I would do that?