diff --git a/README.md b/README.md index 131b52d..7931a21 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +adding multiple login to auth token library # Laravel 4 Auth token Hooks into the laravel auth module and provides an auth token upon success. This token is really only secure in https environment. This main purpose for this module was to provide an auth token to javascript web app which could be used to identify users on api calls. diff --git a/src/Tappleby/AuthToken/AuthTokenDriver.php b/src/Tappleby/AuthToken/AuthTokenDriver.php index 40fc922..f3f3e31 100644 --- a/src/Tappleby/AuthToken/AuthTokenDriver.php +++ b/src/Tappleby/AuthToken/AuthTokenDriver.php @@ -72,11 +72,11 @@ public function validate($authTokenPayload) { * @param array $credentials * @return bool|AuthToken */ - public function attempt(array $credentials) { + public function attempt(array $credentials,$deviceIdentifier=null) { $user = $this->users->retrieveByCredentials($credentials); if($user instanceof UserInterface && $this->users->validateCredentials($user, $credentials)) { - return $this->create($user); + return $this->create($user,$deviceIdentifier); } return false; @@ -88,9 +88,9 @@ public function attempt(array $credentials) { * @param UserInterface $user * @return bool|AuthToken */ - public function create(UserInterface $user) { - $this->tokens->purge($user); - return $this->tokens->create($user); + public function create(UserInterface $user,$deviceIdentifier=null) { + $this->tokens->purge($user,$deviceIdentifier); + return $this->tokens->create($user,$deviceIdentifier); } /** @@ -112,4 +112,7 @@ public function user(AuthToken $token) { public function publicToken(AuthToken $token) { return $this->tokens->serializeToken($token); } + function load($user){ + + } } \ No newline at end of file diff --git a/src/Tappleby/AuthToken/AuthTokenProviderInterface.php b/src/Tappleby/AuthToken/AuthTokenProviderInterface.php index e4f42df..b914ecb 100644 --- a/src/Tappleby/AuthToken/AuthTokenProviderInterface.php +++ b/src/Tappleby/AuthToken/AuthTokenProviderInterface.php @@ -25,7 +25,7 @@ interface AuthTokenProviderInterface { * @param \Illuminate\Auth\UserInterface $user * @return \TAppleby\AuthToken\AuthToken|false */ - public function create(UserInterface $user); + public function create(UserInterface $user,$deviceIdentifier=null); /** diff --git a/src/Tappleby/AuthToken/AuthTokenServiceProvider.php b/src/Tappleby/AuthToken/AuthTokenServiceProvider.php index b3e645a..5c3b5c4 100644 --- a/src/Tappleby/AuthToken/AuthTokenServiceProvider.php +++ b/src/Tappleby/AuthToken/AuthTokenServiceProvider.php @@ -1,61 +1,61 @@ -package('tappleby/laravel-auth-token'); - $this->app['router']->filter('auth.token', 'tappleby.auth.token.filter'); - } - - - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $app = $this->app; - - $app->bindShared('tappleby.auth.token', function ($app) { - return new AuthTokenManager($app); - }); - - $app->bindShared('tappleby.auth.token.filter', function ($app) { - $driver = $app['tappleby.auth.token']->driver(); - $events = $app['events']; - - return new AuthTokenFilter($driver, $events); - }); - - $app->bind('Tappleby\AuthToken\AuthTokenController', function ($app) { - $driver = $app['tappleby.auth.token']->driver(); - $credsFormatter = $app['config']->get('laravel-auth-token::format_credentials', null); - $events = $app['events']; - - return new AuthTokenController($driver, $credsFormatter, $events); - }); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array('tappleby.auth.token', 'tappleby.auth.token.filter'); - } - +package('tappleby/laravel-auth-token'); + $this->app['router']->filter('auth.token', 'tappleby.auth.token.filter'); + } + + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $app = $this->app; + + $app->bindShared('tappleby.auth.token', function ($app) { + return new AuthTokenManager($app); + }); + + $app->bindShared('tappleby.auth.token.filter', function ($app) { + $driver = $app['tappleby.auth.token']->driver(); + $events = $app['events']; + + return new AuthTokenFilter($driver, $events); + }); + + $app->bind('Tappleby\AuthToken\AuthTokenController', function ($app) { + $driver = $app['tappleby.auth.token']->driver(); + $credsFormatter = $app['config']->get('laravel-auth-token::format_credentials', null); + $events = $app['events']; + + return new AuthTokenController($driver, $credsFormatter, $events); + }); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('tappleby.auth.token', 'tappleby.auth.token.filter'); + } + } \ No newline at end of file diff --git a/src/Tappleby/AuthToken/DatabaseAuthTokenProvider.php b/src/Tappleby/AuthToken/DatabaseAuthTokenProvider.php index 746bc72..ab3c195 100644 --- a/src/Tappleby/AuthToken/DatabaseAuthTokenProvider.php +++ b/src/Tappleby/AuthToken/DatabaseAuthTokenProvider.php @@ -54,7 +54,7 @@ protected function db() { * @param \Illuminate\Auth\UserInterface $user * @return \TAppleby\AuthToken\AuthToken|false */ - public function create(UserInterface $user) + public function create(UserInterface $user,$deviceIdentifier=null) { if($user == null || $user->getAuthIdentifier() == null) { return false; @@ -65,7 +65,7 @@ public function create(UserInterface $user) $t = new \DateTime; $insertData = array_merge($token->toArray(), array( - 'created_at' => $t, 'updated_at' => $t + 'created_at' => $t, 'updated_at' => $t,'device_identifier'=>$deviceIdentifier )); $this->db()->insert($insertData); @@ -108,13 +108,15 @@ public function find($serializedAuthToken) * @param mixed|\Illuminate\Auth\UserInterface $identifier * @return bool */ - public function purge($identifier) + public function purge($identifier,$deviceIdentifier=null) { if($identifier instanceof UserInterface) { $identifier = $identifier->getAuthIdentifier(); } - $res = $this->db()->where('auth_identifier', $identifier)->delete(); + $query = $this->db()->where('auth_identifier', $identifier); + $query=$query->where('device_identifier',$deviceIdentifier); + $res = $query->delete(); return $res > 0; } diff --git a/src/migrations/2015_06_01_120729_add_device_identifier_field_to_auth_token.php b/src/migrations/2015_06_01_120729_add_device_identifier_field_to_auth_token.php new file mode 100644 index 0000000..87a3cba --- /dev/null +++ b/src/migrations/2015_06_01_120729_add_device_identifier_field_to_auth_token.php @@ -0,0 +1,31 @@ +string('device_identifier')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } + +} diff --git a/tests/TAppleby/AuthToken/AuthTokenDriverTest.php b/tests/TAppleby/AuthToken/AuthTokenDriverTest.php index 2594b23..1b7df47 100644 --- a/tests/TAppleby/AuthToken/AuthTokenDriverTest.php +++ b/tests/TAppleby/AuthToken/AuthTokenDriverTest.php @@ -19,6 +19,7 @@ public function setUp() { } public function testValidateReturnsFalseNullToken() { + $tokens = m::mock('Tappleby\AuthToken\AuthTokenProviderInterface'); $users = m::mock('Illuminate\Auth\UserProviderInterface'); @@ -41,11 +42,11 @@ public function testValidateReturnsFalseInvalidToken() { public function testFilterReturnsFalseValidTokenMissingUser() { $tokens = m::mock('Tappleby\AuthToken\AuthTokenProviderInterface'); + $users = m::mock('Illuminate\Auth\UserProviderInterface'); $tokens->shouldReceive('find')->once()->andReturn( new \Tappleby\AuthToken\AuthToken(1, 'public', 'private') ); - $users->shouldReceive('retrieveByID')->once()->andReturnNull(); - + $users->shouldReceive('retrieveById')->once()->andReturnNull(); $driver = new \Tappleby\AuthToken\AuthTokenDriver($tokens, $users); $this->assertFalse( $driver->validate('good_token') ); @@ -58,7 +59,7 @@ public function testValidateReturnsUsers() { $tokens->shouldReceive('find')->once()->andReturn( new \Tappleby\AuthToken\AuthToken(1, 'public', 'private') ); $user = m::mock('StdClass'); - $users->shouldReceive('retrieveByID')->once()->andReturn( $user ); + $users->shouldReceive('retrieveById')->once()->andReturn( $user ); $driver = new \Tappleby\AuthToken\AuthTokenDriver($tokens, $users); @@ -73,7 +74,7 @@ public function testUserFromAuthToken() { $authToken = m::mock('Tappleby\AuthToken\AuthToken'); $user = m::mock('StdClass'); - $users->shouldReceive('retrieveByID')->once()->andReturn( $user ); + $users->shouldReceive('retrieveById')->once()->andReturn( $user ); $authToken->shouldReceive('getAuthIdentifier')->once()->andReturn(1); $driver = new \Tappleby\AuthToken\AuthTokenDriver($tokens, $users); diff --git a/tests/TAppleby/AuthToken/DatabaseAuthTokenProviderTest.php b/tests/TAppleby/AuthToken/DatabaseAuthTokenProviderTest.php index dceea16..806375a 100644 --- a/tests/TAppleby/AuthToken/DatabaseAuthTokenProviderTest.php +++ b/tests/TAppleby/AuthToken/DatabaseAuthTokenProviderTest.php @@ -165,10 +165,11 @@ public function testPurgeGetsIdentifierFromUser() { $provider = $this->getProvider( $enc ); $provider->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($query = m::mock('StdClass')); - $query->shouldReceive('where')->once()->with('auth_identifier', 1)->andReturn($query); + $query->shouldReceive('where')->once()->ordered()->withArgs(['auth_identifier', 1])->andReturn($query); + $query->shouldReceive('where')->once()->ordered()->withArgs(['device_identifier', 'android 1'])->andReturn($query); $query->shouldReceive('delete')->once()->andReturn(0); - $provider->purge( $user ); + $provider->purge( $user,'android 1' ); } public function testPurgeReturnsFalseWhenNoTokensDeleted() { @@ -176,10 +177,11 @@ public function testPurgeReturnsFalseWhenNoTokensDeleted() { $provider = $this->getProvider( $enc ); $provider->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($query = m::mock('StdClass')); - $query->shouldReceive('where')->once()->with('auth_identifier', 1)->andReturn($query); + $query->shouldReceive('where')->once()->ordered()->withArgs(['auth_identifier', 1])->andReturn($query); + $query->shouldReceive('where')->once()->ordered()->withArgs(['device_identifier', 'android 1'])->andReturn($query); $query->shouldReceive('delete')->once()->andReturn(0); - $this->assertFalse( $provider->purge(1) ); + $this->assertFalse( $provider->purge(1,'android 1') ); } public function testPurgeReturnsTrueWhenTokensDeleted() { @@ -187,9 +189,10 @@ public function testPurgeReturnsTrueWhenTokensDeleted() { $provider = $this->getProvider( $enc ); $provider->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($query = m::mock('StdClass')); - $query->shouldReceive('where')->once()->with('auth_identifier', 1)->andReturn($query); + $query->shouldReceive('where')->once()->ordered()->withArgs(['auth_identifier', 1])->andReturn($query); + $query->shouldReceive('where')->once()->ordered()->withArgs(['device_identifier', 'android 1'])->andReturn($query); $query->shouldReceive('delete')->once()->andReturn(5); - $this->assertTrue( $provider->purge(1) ); + $this->assertTrue( $provider->purge(1,'android 1') ); } } \ No newline at end of file