2018-01-23 22:13:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import time
|
2018-01-25 11:41:24 +00:00
|
|
|
|
2018-07-03 10:32:54 +00:00
|
|
|
import jwt
|
|
|
|
|
2019-07-09 07:57:44 +00:00
|
|
|
from .helper import DashboardTestCase, JObj, JLeaf
|
2018-01-23 22:13:57 +00:00
|
|
|
|
2018-01-26 12:11:21 +00:00
|
|
|
|
2018-02-28 22:14:35 +00:00
|
|
|
class AuthTest(DashboardTestCase):
|
2018-04-24 16:32:54 +00:00
|
|
|
|
|
|
|
AUTO_AUTHENTICATE = False
|
|
|
|
|
2018-01-26 12:12:16 +00:00
|
|
|
def setUp(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self.reset_session()
|
2018-01-26 12:12:16 +00:00
|
|
|
|
2018-07-03 10:32:54 +00:00
|
|
|
def _validate_jwt_token(self, token, username, permissions):
|
|
|
|
payload = jwt.decode(token, verify=False)
|
|
|
|
self.assertIn('username', payload)
|
|
|
|
self.assertEqual(payload['username'], username)
|
|
|
|
|
|
|
|
for scope, perms in permissions.items():
|
2018-06-01 11:15:49 +00:00
|
|
|
self.assertIsNotNone(scope)
|
|
|
|
self.assertIn('read', perms)
|
|
|
|
self.assertIn('update', perms)
|
|
|
|
self.assertIn('create', perms)
|
|
|
|
self.assertIn('delete', perms)
|
2018-07-03 10:32:54 +00:00
|
|
|
|
|
|
|
def test_a_set_login_credentials(self):
|
|
|
|
self.create_user('admin2', 'admin2', ['administrator'])
|
|
|
|
self._post("/api/auth", {'username': 'admin2', 'password': 'admin2'})
|
|
|
|
self.assertStatus(201)
|
|
|
|
data = self.jsonBody()
|
|
|
|
self._validate_jwt_token(data['token'], "admin2", data['permissions'])
|
2018-04-24 16:32:54 +00:00
|
|
|
self.delete_user('admin2')
|
2018-01-26 12:12:16 +00:00
|
|
|
|
2018-01-23 22:13:57 +00:00
|
|
|
def test_login_valid(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
|
|
|
|
self.assertStatus(201)
|
2018-06-01 11:15:49 +00:00
|
|
|
data = self.jsonBody()
|
2019-07-09 07:57:44 +00:00
|
|
|
self.assertSchema(data, JObj(sub_elems={
|
|
|
|
'token': JLeaf(str),
|
|
|
|
'username': JLeaf(str),
|
|
|
|
'permissions': JObj(sub_elems={}, allow_unknown=True),
|
|
|
|
'sso': JLeaf(bool)
|
|
|
|
}, allow_unknown=False))
|
2018-07-03 10:32:54 +00:00
|
|
|
self._validate_jwt_token(data['token'], "admin", data['permissions'])
|
2018-01-23 22:13:57 +00:00
|
|
|
|
|
|
|
def test_login_invalid(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self._post("/api/auth", {'username': 'admin', 'password': 'inval'})
|
2018-06-04 20:26:44 +00:00
|
|
|
self.assertStatus(400)
|
|
|
|
self.assertJsonBody({
|
|
|
|
"component": "auth",
|
|
|
|
"code": "invalid_credentials",
|
|
|
|
"detail": "Invalid credentials"
|
|
|
|
})
|
2018-01-23 22:13:57 +00:00
|
|
|
|
2018-09-17 11:03:37 +00:00
|
|
|
def test_login_without_password(self):
|
|
|
|
self.create_user('admin2', '', ['administrator'])
|
|
|
|
self._post("/api/auth", {'username': 'admin2', 'password': ''})
|
|
|
|
self.assertStatus(400)
|
|
|
|
self.assertJsonBody({
|
|
|
|
"component": "auth",
|
|
|
|
"code": "invalid_credentials",
|
|
|
|
"detail": "Invalid credentials"
|
|
|
|
})
|
|
|
|
self.delete_user('admin2')
|
|
|
|
|
2018-01-23 22:13:57 +00:00
|
|
|
def test_logout(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
|
2018-07-03 10:32:54 +00:00
|
|
|
self.assertStatus(201)
|
|
|
|
data = self.jsonBody()
|
|
|
|
self._validate_jwt_token(data['token'], "admin", data['permissions'])
|
|
|
|
self.set_jwt_token(data['token'])
|
2018-06-11 09:29:08 +00:00
|
|
|
self._post("/api/auth/logout")
|
|
|
|
self.assertStatus(200)
|
|
|
|
self.assertJsonBody({
|
|
|
|
"redirect_url": "#/login"
|
|
|
|
})
|
2018-02-08 08:24:01 +00:00
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(401)
|
2018-07-03 10:32:54 +00:00
|
|
|
self.set_jwt_token(None)
|
2018-01-23 22:13:57 +00:00
|
|
|
|
2018-07-03 10:32:54 +00:00
|
|
|
def test_token_ttl(self):
|
|
|
|
self._ceph_cmd(['dashboard', 'set-jwt-token-ttl', '5'])
|
2018-02-08 08:24:01 +00:00
|
|
|
self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
|
|
|
|
self.assertStatus(201)
|
2018-07-03 10:32:54 +00:00
|
|
|
self.set_jwt_token(self.jsonBody()['token'])
|
2018-02-08 08:24:01 +00:00
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(200)
|
2018-07-03 10:32:54 +00:00
|
|
|
time.sleep(6)
|
2018-02-08 08:24:01 +00:00
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(401)
|
2018-07-03 10:32:54 +00:00
|
|
|
self._ceph_cmd(['dashboard', 'set-jwt-token-ttl', '28800'])
|
|
|
|
self.set_jwt_token(None)
|
|
|
|
|
|
|
|
def test_remove_from_blacklist(self):
|
|
|
|
self._ceph_cmd(['dashboard', 'set-jwt-token-ttl', '5'])
|
|
|
|
self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
|
|
|
|
self.assertStatus(201)
|
|
|
|
self.set_jwt_token(self.jsonBody()['token'])
|
|
|
|
# the following call adds the token to the blacklist
|
2018-06-11 09:29:08 +00:00
|
|
|
self._post("/api/auth/logout")
|
|
|
|
self.assertStatus(200)
|
2018-07-03 10:32:54 +00:00
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(401)
|
|
|
|
time.sleep(6)
|
|
|
|
self._ceph_cmd(['dashboard', 'set-jwt-token-ttl', '28800'])
|
|
|
|
self.set_jwt_token(None)
|
|
|
|
self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
|
|
|
|
self.assertStatus(201)
|
|
|
|
self.set_jwt_token(self.jsonBody()['token'])
|
|
|
|
# the following call removes expired tokens from the blacklist
|
2018-06-11 09:29:08 +00:00
|
|
|
self._post("/api/auth/logout")
|
|
|
|
self.assertStatus(200)
|
2018-01-23 22:13:57 +00:00
|
|
|
|
|
|
|
def test_unauthorized(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(401)
|
2018-07-03 10:32:54 +00:00
|
|
|
|
|
|
|
def test_invalidate_token_by_admin(self):
|
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(401)
|
|
|
|
self.create_user('user', 'user', ['read-only'])
|
|
|
|
time.sleep(1)
|
|
|
|
self._post("/api/auth", {'username': 'user', 'password': 'user'})
|
|
|
|
self.assertStatus(201)
|
|
|
|
self.set_jwt_token(self.jsonBody()['token'])
|
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(200)
|
|
|
|
time.sleep(1)
|
|
|
|
self._ceph_cmd(['dashboard', 'ac-user-set-password', 'user', 'user2'])
|
|
|
|
time.sleep(1)
|
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(401)
|
|
|
|
self.set_jwt_token(None)
|
|
|
|
self._post("/api/auth", {'username': 'user', 'password': 'user2'})
|
|
|
|
self.assertStatus(201)
|
|
|
|
self.set_jwt_token(self.jsonBody()['token'])
|
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(200)
|
|
|
|
self.delete_user("user")
|
2019-07-09 07:57:44 +00:00
|
|
|
|
|
|
|
def test_check_token(self):
|
|
|
|
self.login("admin", "admin")
|
|
|
|
self._post("/api/auth/check", {"token": self.jsonBody()["token"]})
|
|
|
|
self.assertStatus(200)
|
|
|
|
data = self.jsonBody()
|
|
|
|
self.assertSchema(data, JObj(sub_elems={
|
|
|
|
"username": JLeaf(str),
|
|
|
|
"permissions": JObj(sub_elems={}, allow_unknown=True),
|
|
|
|
"sso": JLeaf(bool)
|
|
|
|
}, allow_unknown=False))
|
|
|
|
self.logout()
|
|
|
|
|
|
|
|
def test_check_wo_token(self):
|
|
|
|
self.login("admin", "admin")
|
|
|
|
self._post("/api/auth/check", {"token": ""})
|
|
|
|
self.assertStatus(200)
|
|
|
|
data = self.jsonBody()
|
|
|
|
self.assertSchema(data, JObj(sub_elems={
|
|
|
|
"login_url": JLeaf(str)
|
|
|
|
}, allow_unknown=False))
|
|
|
|
self.logout()
|