2017 © Pedro Peláez
 

symfony-bundle api-authentication-bundle

Symfony2 api authentication bundle

image

sergic/api-authentication-bundle

Symfony2 api authentication bundle

  • Monday, October 28, 2013
  • by Sergic
  • Repository
  • 1 Watchers
  • 0 Stars
  • 27 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

INBApiAuthenticationBundle

Api authentication bundle for Symfony2, (*1)

Installation

1. Add this bundle to your composer.json:

{
    "require": {
        "sergic/api-authentication-bundle": "0.1.*@dev"
    }
}

Now tell composer to download the bundle by running the command:, (*2)

``` bash $ php composer.phar update sergic/api-authentication-bundle, (*3)


### 2. Add this bundle to your application's kernel: ``` php // application/ApplicationKernel.php public function registerBundles() { return array( // ... new INB\Bundle\ApiAuthenticationBundle\INBApiAuthenticationBundle(), // ... ); }

Step 3: Create your User class

with token parameter, (*4)

``` php // src/Acme/AcmeBundle/Entity/User.php /** * User Api token * * @var string */ protected $token;, (*5)

public function __construct() { $this->token = null; }, (*6)

/** * {@inheritdoc} */ public function getToken() { return $this->token; }, (*7)

/** * {@inheritdoc} */ public function setToken($token) { $this->token = $token;, (*8)

return $this;

}, (*9)

/** * {@inheritdoc} */ public function generateApiToken() { $this->token = md5(md5(time() + rand(11111, 99999)));, (*10)

return $this;

}, (*11)


### Step 4: Create your User repository class and init it as a service ``` php // src/Acme/AcmeBundle/Repositroy/UserRepository.php use Ft\Bundle\CoreBundle\Doctrine\ORM\EntityRepository; class UserRepository extends EntityRepository { /** * Constructor. * * @param EncoderFactoryInterface $encoderFactory * @param EntityManager $em The EntityManager to use. * @param ClassMetadata $class The class descriptor. */ public function __construct($em, ClassMetadata $class, EncoderFactoryInterface $encoderFactory = null) { parent::__construct($em, $class); $this->encoderFactory = $encoderFactory; } }

``` yml, (*12)

src/Acme/AcmeBundle/Resources/config/services.yml

parameters: acme.user.class: Acme\AcmeBundle\Entity\User acme.repository.user.class: Acme\AcmeBundle\Repositroy\UserRepository services: user_metadata: class: Doctrine\ORM\Mapping\ClassMetadata factory-method: getClassMetadata arguments: [%acme.user.class%] acme.repository.user: class: %acme.repository.user.class% arguments: [@doctrine.orm.entity_manager, @user_metadata, @security.encoder_factory], (*13)


### Step 4: Configure your application's security.yml ``` yaml # app/config/security.yml security: providers: api_provider: id: inb_api_auth.provider firewalls: api: provider: api_provider pattern: ^(/[^\/]+)/api|^/api access_denied_url: ^(/[^\/]+)/api|^/api/unauthorized api: { lifetime: '30D' } stateless: true anonymous: true encoders: Acme\AcmeBundle\Entity\User: sha512 access_control: - { path: ^(/[^\/]+)/api/*|^/api/*, roles: [ROLE_USER] }

The Versions

28/10 2013

dev-master

9999999-dev

Symfony2 api authentication bundle

  Sources   Download

The Requires

 

api authentication rest symfony2 bundle