Skip to content

Commit

Permalink
Merge pull request #987 from pantheon-systems/fix/better_auth_message
Browse files Browse the repository at this point in the history
Improved on auth login message when more than one machine token has been saved
  • Loading branch information
TeslaDethray committed Mar 16, 2016
2 parents cb83bac + 6153c35 commit 7fcf922
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project starting with the 0.6.0 release will be docu
## MASTER
### Changed
- WP-CLI function `import` has been removed from the command blacklist. [See the documentation for more information.](https://github.com/pantheon-systems/documentation/blob/master/source/docs/guides/create-a-wordpress-site-from-the-commandline-with-terminus-and-wp-cli.md) (#979)
- When running `terminus auth login` and more than one machine token is present, Terminus will now tell you how to use them instead of giving the same error message received when no tokens are present. (#987)

### Fixed
- Fixed unidentified index email warning which appeared when logging in via saved machine token by email. (#983)
Expand Down
30 changes: 19 additions & 11 deletions php/Terminus/Commands/AuthCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class AuthCommand extends TerminusCommand {
* : dump call information when logging in.
*/
public function login($args, $assoc_args) {
$auth = new Auth();
$auth = new Auth();
$tokens = $auth->getAllSavedTokenEmails();
if (!empty($args)) {
$email = array_shift($args);
}
Expand Down Expand Up @@ -68,28 +69,35 @@ public function login($args, $assoc_args) {
);
$auth->logInViaMachineToken(['email' => $_SERVER['TERMINUS_USER']]);
$this->log()->info('Logging in via machine token');
} elseif (!isset($email)
&& $only_token = $auth->getOnlySavedToken()
) {
} elseif (!isset($email) && (count($tokens) === 1)) {
// Try to log in using a machine token, if there is only one saved token.
$email = array_shift($tokens);
$auth->logInViaMachineToken(compact('email'));
$this->log()->info(
'Found a machine token for "{email}".',
['email' => $only_token['email'],]
compact('email')
);
$auth->logInViaMachineToken($only_token);
$auth->logInViaMachineToken(compact('email'));
$this->log()->info('Logging in via machine token');
} else if (isset($email) && isset($assoc_args['password'])) {
// Log in via username and password, if present.
$password = $assoc_args['password'];
$auth->logInViaUsernameAndPassword(
$email,
$assoc_args['password']
);
} else {
$this->log()->info(
"Please visit the Dashboard to generate a machine token:\n{url}",
['url' => $auth->getMachineTokenCreationUrl()]
);
exit(1);
$message = "visit the dashboard to generate a machine token:\n{url}";
$context = ['url' => $auth->getMachineTokenCreationUrl()];
if (count($tokens) > 1) {
$msg = "Tokens were saved for the following email addresses:\n";
$msg .= "{tokens}\n You may log in via `terminus auth login <email>`";
$message = "$msg, or you may $message";
$context['tokens'] = implode("\n", $tokens);
} else {
$message = "Please $message";
}
$this->failure($message, $context);
}
if (!isset($email)) {
$user = Session::getUser();
Expand Down
8 changes: 5 additions & 3 deletions php/Terminus/Commands/TerminusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ public function output() {
* @throws TerminusException
*/
public function ensureLogin() {
$auth = new Auth();
$auth = new Auth();
$tokens = $auth->getAllSavedTokenEmails();
if (!$auth->loggedIn()) {
if ($token = $auth->getOnlySavedToken()) {
$auth->logInViaMachineToken($token);
if (count($tokens) === 1) {
$email = array_shift($token);
$auth->logInViaMachineToken(compact('email'));
} else if (isset($_SERVER['TERMINUS_MACHINE_TOKEN'])
&& $token = $_SERVER['TERMINUS_MACHINE_TOKEN']
) {
Expand Down
24 changes: 10 additions & 14 deletions php/Terminus/Models/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public function __construct($attributes = null, array $options = array()) {
parent::__construct($attributes, $options);
}

/**
* Gets all email addresses for which there are saved machine tokens
*
* @return string[]
*/
public function getAllSavedTokenEmails() {
$emails = $this->tokens_cache->getAllSavedTokenEmails();
return $emails;
}

/**
* Generates the URL string for where to create a machine token
*
Expand All @@ -44,20 +54,6 @@ public function getMachineTokenCreationUrl() {
return $url;
}

/**
* Gets the only saved token or returns false
*
* @return bool|string
*/
public function getOnlySavedToken() {
$emails = $this->tokens_cache->getAllSavedTokenEmails();
if (count($emails) == 1) {
$email = array_shift($emails);
return $this->tokens_cache->findByEmail($email);
}
return false;
}

/**
* Checks to see if the current user is logged in
*
Expand Down
21 changes: 9 additions & 12 deletions tests/unit_tests/models/test-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,21 @@ public function testConstruct() {
$this->assertTrue(strpos(get_class($this->auth), 'Auth') !== false);
}

public function testGetAllSavedTokenEmails() {
$tokens_cache = new TokensCache();
$tokens_dir = $tokens_cache->getCacheDir();
$token_count = count(scandir($tokens_dir)) - 2;
$tokens = $this->auth->getAllSavedTokenEmails();
$this->assertEquals(count($tokens), $token_count);
$this->assertInternalType('array', $tokens);
}

public function testGetMachineTokenCreationUrl() {
$url = $this->auth->getMachineTokenCreationUrl();
$this->assertInternalType('string', $url);
$this->assertInternalType('integer', strpos($url, 'machine-token/create'));
}

public function testGetOnlySavedToken() {
$tokens_cache = new TokensCache();
$tokens_dir = $tokens_cache->getCacheDir();
$token_count = count(scandir($tokens_dir)) - 2;
$only_token = $this->auth->getOnlySavedToken();
if ($token_count != 1) {
$this->assertFalse($only_token);
} else {
$this->assertInternalType('array', $only_token);
}
}

public function testLoggedIn() {
$this->assertTrue($this->auth->loggedIn());
}
Expand Down

0 comments on commit 7fcf922

Please sign in to comment.