Repository: deis/deis

Issue:
Feature request: ability to see who you are logged in as
We use multiple accounts for our rollout of deis and it would be useful to be able to easier tell who you are logged in as.



Discussion:
Please note that we may go towards token-based authentication at some point which may make this feature request moot. Other users have asked about using some form of external authentication (https://github.com/deis/deis/issues/1578) or some form of token-based authentication (#1488). We may want to shake things out at the authentication level before we see if we want to include this.

I mean a command like

``` console
$ deis whoami
I think you are $USERNAME
```

We could write out the username to ~/.deis/client.json after each login.


Resolving patch:
diff --git a/client/deis.py b/client/deis.py
--- a/client/deis.py
+++ b/client/deis.py
@@ -684,6 +684,7 @@ def auth(self, args):
         auth:cancel            remove the current account
         auth:login             authenticate against a controller
         auth:logout            clear the current user session
+        auth:whoami            display the authenticated user
 
         Use `deis help [command]` to learn more.
         """
@@ -803,6 +804,7 @@ def auth_login(self, args):
         response = self._session.post(url, data=payload, allow_redirects=False)
         if response.status_code == requests.codes.found:  # @UndefinedVariable
             self._settings['controller'] = controller
+            self._settings['username'] = username
             self._settings.save()
             self._logger.info("Logged in as {}".format(username))
             return username
@@ -824,9 +826,23 @@ def auth_logout(self, args):
         self._session.cookies.clear()
         self._session.cookies.save()
         self._settings['controller'] = None
+        self._settings['username'] = None
         self._settings.save()
         self._logger.info('Logged out')
 
+    def auth_whoami(self, args):
+        """
+        Displays the currently logged in user.
+
+        Usage: deis auth:whoami
+        """
+        user = self._settings.get('username')
+        if user:
+            self._logger.info(user)
+        else:
+            self._logger.info(
+                'Not logged in. Use `deis login` or `deis register` to get started.')
+
     def builds(self, args):
         """
         Valid commands for builds:
@@ -2130,6 +2146,7 @@ def shortcuts(self, args):
     ('sharing:list', 'perms:list'),
     ('sharing:add', 'perms:create'),
     ('sharing:remove', 'perms:delete'),
+    ('whoami', 'auth:whoami'),
 ])
 
 


Test patch:
diff --git a/tests/auth_test.go b/tests/auth_test.go
--- a/tests/auth_test.go
+++ b/tests/auth_test.go
@@ -19,6 +19,7 @@ func TestAuth(t *testing.T) {
 	authRegisterTest(t, params)
 	authLogoutTest(t, params)
 	authLoginTest(t, params)
+	authWhoamiTest(t, params)
 	authCancel(t, params)
 }
 
@@ -48,3 +49,7 @@ func authRegisterTest(t *testing.T, params *utils.DeisTestConfig) {
 	utils.Execute(t, cmd, params, false, "")
 	utils.Execute(t, cmd, params, true, "Registration failed")
 }
+
+func authWhoamiTest(t *testing.T, params *utils.DeisTestConfig) {
+	utils.Execute(t, "auth:whoami", params, true, params.UserName)
+}