I am trying to implement authentication in rest API with Https Bearer Auth
I don't have a table of registered users but a table with other apps that have "access_token" permission to access my API.
Is this correct and safe? Will "access_token" be the same forever? and put it directly in the code?
controller.php
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => CompositeAuth::className(),
'authMethods' => [
HttpBearerAuth::className(),
]];
return $behaviors;
}
application components
'components' => [
'user' => [
'identityClass' => 'common\models\ApiAccess',
'enableSession' => false,
'loginUrl' => null,
],
],
common/models/ApiAccess (instead common/models/User)
....
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
and in this table i have fields: id
, app_name
, access_token
.
client side
in client side i use yiisoft/yii2-httpclient
and i have this code.
$client = new Client();
$response = $client->createRequest()
->setMethod('GET')
->setUrl('http://etc/etc')
->addHeaders(['Authorization' => 'Bearer 56265883-****-****-****-************'])
->send();
if ($response->isOk) {
code.....
}