Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
dre1080 committed Jun 8, 2012
2 parents e2dfd5a + 1945f52 commit cf89cc3
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 47 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# NinjAuth

Use the Auth, OAuth and OAuth2 packages to authenticate users with an array of third-party services in a totally integrated and abstracted fashion.
Use the OAuth and OAuth2 packages to authenticate users with an array of third-party services in a totally integrated and abstracted fashion. Users can currently be managed by SimpleAuth or Sentry.

Implementation requires only one controller and one database table (two if you count users).

NinjAuth comes from the company behind the project: [HappyNinjas](http://happyninjas.com/).
The name "NinjAuth" comes from the company behind the project: [HappyNinjas](http://happyninjas.com/).

## Supported Strategies

Expand All @@ -20,7 +20,7 @@ NinjAuth comes from the company behind the project: [HappyNinjas](http://happyni
## Installation

# Create users if this table does not exist already
$ oil g migration create_users username:varchar[50] password:string group:int email:string last_login:integer login_hash:string profile_fields:text
$ oil g migration create_users username:varchar[50] password:string group:int email:string last_login:integer login_hash:string profile_fields:text created_at:int
$ oil refine migrate

# Run migrations in the package to create "authentications" table
Expand Down
2 changes: 2 additions & 0 deletions classes/adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace NinjAuth;

class AuthException extends \FuelException {}

/**
* NinjAuth Adapter
*
Expand Down
4 changes: 2 additions & 2 deletions classes/adapter/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function create_user(array $user)

return $user_id ?: false;
}
catch (SentryUserException $e)
catch (Sentry\SentryUserException $e)
{
Session::set_flash('ninjauth.error', $e->getMessage());
throw new AuthException($e->getMessage());
}

return false;
Expand Down
8 changes: 7 additions & 1 deletion classes/adapter/simpleauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function create_user(array $user)
isset($user['password']) ? $user['password'] : \Str::random(),

// Email address
isset($user['username']) ? $user['username'] : null,
isset($user['email']) ? $user['email'] : null,

// Which group are they?
\Config::get('ninjauth.default_group'),
Expand All @@ -59,4 +59,10 @@ public function create_user(array $user)

return false;
}

public function can_auto_login(array $user)
{
// To automatically register with SimpleAuth you only need one or the other
return isset($user['username']) and isset($user['email']) and isset($user['password']);
}
}
67 changes: 38 additions & 29 deletions classes/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Input;
use Response;
use Session;
use View;

/**
* NinjAuth Controller
Expand Down Expand Up @@ -42,37 +43,45 @@ public function action_session($provider)

public function action_callback($provider)
{
// Whatever happens, we're sending somebody somewhere
$status = Strategy::forge($provider)->login_or_register();

// Stuff should go with each type of response
switch ($status)
try
{
case 'linked':
$message = 'You have linked '.$provider.' to your account.';
$url = static::$linked_redirect;
break;

case 'logged_in':
$message = 'You have logged in.';
$url = static::$login_redirect;
break;

case 'registered':
$message = 'You have logged in with your new account.';
$url = static::$registered_redirect;
break;

case 'register':
$message = 'Please fill in any missing details and add a password.';
$url = static::$register_redirect;
break;

default:
throw new Exception('Strategy::login_or_register() has come up with a result that we dont know how to handle.');
// Whatever happens, we're sending somebody somewhere
$status = Strategy::forge($provider)->login_or_register();

// Stuff should go with each type of response
switch ($status)
{
case 'linked':
$message = 'You have linked '.$provider.' to your account.';
$url = static::$linked_redirect;
break;

case 'logged_in':
$message = 'You have logged in.';
$url = static::$login_redirect;
break;

case 'registered':
$message = 'You have logged in with your new account.';
$url = static::$registered_redirect;
break;

case 'register':
$message = 'Please fill in any missing details and add a password.';
$url = static::$register_redirect;
break;

default:
exit('Strategy::login_or_register() has come up with a result that we dont know how to handle.');
}

Response::redirect($url);
}

Response::redirect($url);
catch (AuthException $e)
{
exit($e->getMessage());
}
}

public function action_register()
Expand Down Expand Up @@ -114,7 +123,7 @@ public function action_register()
}
}

$this->response->body = \View::forge('register', array(
return View::forge('register', array(
'user' => (object) compact('username', 'full_name', 'email', 'password')
));
}
Expand Down
4 changes: 2 additions & 2 deletions classes/strategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function login_or_register()
else
{
$auth = Model_Authentication::find_one_by_user_id($user_id);
throw new Exception(sprintf('This user is already linked to "%s".', $auth->provider));
throw new AuthException(sprintf('This user is already linked to "%s".', $auth->provider));
}
}

Expand All @@ -152,7 +152,7 @@ public function login_or_register()
return 'logged_in';
}

throw new Exception('Force login failed');
throw new AuthException('This user could not be logged in.');
}

// Not an existing user of any type, so we need to create a user somehow
Expand Down
20 changes: 13 additions & 7 deletions classes/strategy/oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace NinjAuth;

use Arr;
use Cookie;
use Uri;
use Input;
use Request;

class Strategy_OAuth extends Strategy {

public $provider;
Expand All @@ -14,10 +20,10 @@ public function authenticate()
// Load the provider
$provider = \OAuth\Provider::forge($this->provider);

if ( ! $callback = \Arr::get($this->config, 'callback'))
if ( ! $callback = Arr::get($this->config, 'callback'))
{
// Turn /whatever/controller/session/facebook into /whatever/controller/callback/facebook
$callback = \Uri::create(str_replace('/session/', '/callback/', \Request::active()->route->path));
$callback = Uri::create(str_replace('/session/', '/callback/', Request::active()->route->path));
}

// Add the callback URL to the consumer
Expand All @@ -27,7 +33,7 @@ public function authenticate()
$token = $provider->request_token($consumer);

// Store the token
\Cookie::set('oauth_token', base64_encode(serialize($token)));
Cookie::set('oauth_token', base64_encode(serialize($token)));

return $provider->authorize_url($token, array(
'oauth_callback' => $callback,
Expand All @@ -43,23 +49,23 @@ public function callback()
// Load the provider
$this->provider = \OAuth\Provider::forge($this->provider);

if ($token = \Cookie::get('oauth_token'))
if (($token = Cookie::get('oauth_token')))
{
// Get the token from storage
$this->token = unserialize(base64_decode($token));
}

if ($this->token AND $this->token->access_token !== \Input::get_post('oauth_token'))
if ($this->token AND $this->token->access_token !== Input::param('oauth_token'))
{
// Delete the token, it is not valid
\Cookie::delete('oauth_token');
Cookie::delete('oauth_token');

// Send the user back to the beginning
exit('invalid token after coming back to site');
}

// Get the verifier
$verifier = \Input::get_post('oauth_verifier');
$verifier = Input::param('oauth_verifier');

// Store the verifier in the token
$this->token->verifier($verifier);
Expand Down
2 changes: 1 addition & 1 deletion config/ninjauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* NinjAuth can use different adapters, so it will work with 'auth', 'sentry' or 'warden'.
*/
'adapter' => 'auth',
'adapter' => 'SimpleAuth',

/**
* Providers
Expand Down
4 changes: 2 additions & 2 deletions views/register.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php echo Form::open(null, array('id' => 'register')); ?>

<?php if (Session::get_flash('ninjauth.error')): ?>
<span class="error"><?php echo Session::get_flash('ninjauth.error'); ?></span>
<?php if (isset($error)): ?>
<span class="error"><?php echo $error; ?></span>
<?php endif; ?>

<p>
Expand Down

0 comments on commit cf89cc3

Please sign in to comment.