Token.php in OAuth2 Server 8
File
src/Entity/Token.php
View source
<?php
namespace Drupal\oauth2_server\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\oauth2_server\TokenInterface;
class Token extends ContentEntityBase implements TokenInterface {
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['token_id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Code ID'))
->setDescription(t('Primary key: numeric token id.'));
$fields['client_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('OAuth2 Server Client'))
->setDescription(t('The OAuth2 Client of the client.'))
->setSettings([
'target_type' => 'oauth2_server_client',
]);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User'))
->setDescription(t('The user of the resource owner.'))
->setSettings([
'target_type' => 'user',
]);
$fields['type'] = BaseFieldDefinition::create('string')
->setLabel(t('Type'))
->setDescription(t('The type of the token (access, refresh).'))
->setTranslatable(FALSE)
->setSettings([
'not null' => TRUE,
'max_length' => 255,
'text_processing' => 0,
]);
$fields['token'] = BaseFieldDefinition::create('string')
->setLabel(t('Token'))
->setDescription(t('The token.'))
->setTranslatable(FALSE)
->setSettings([
'not null' => TRUE,
'max_length' => 255,
'text_processing' => 0,
]);
$fields['scopes'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Scopes'))
->setDescription(t('The scopes of the token.'))
->setSettings([
'target_type' => 'oauth2_server_scope',
])
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$fields['expires'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Expires'))
->setDescription(t('The Unix timestamp when the token expires.'));
$fields['last_access'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Last Access'))
->setDescription(t('The Unix timestamp when the token was last used.'));
return $fields;
}
public function getUser() {
if ($uid = $this->uid
->getValue()) {
return $this
->entityTypeManager()
->getStorage('user')
->load($uid[0]['target_id']);
}
return FALSE;
}
public function getClient() {
if ($client_id = $this->client_id
->getValue()) {
return $this
->entityTypeManager()
->getStorage('oauth2_server_client')
->load($client_id[0]['target_id']);
}
return FALSE;
}
}
Classes
Name |
Description |
Token |
Defines the OAuth2 server token entity. |