This commit is contained in:
mike cullerton 2022-07-05 14:31:37 -04:00
parent 8480ecb8fb
commit 7e0c06aaa5
2 changed files with 36 additions and 22 deletions

View File

@ -1,12 +1,20 @@
"""Authentication_service."""
from keycloak import KeycloakOpenID
from keycloak.uma_permissions import AuthStatus # type: ignore
from typing import Optional
class AuthenticationService:
"""AuthenticationService."""
@staticmethod
def get_keycloak_openid(server_url, client_id, realm_name, client_secret_key):
def get_keycloak_openid(
server_url: str,
client_id: str,
realm_name: str,
client_secret_key: str
) -> KeycloakOpenID:
"""Get_keycloak_openid."""
keycloak_openid = KeycloakOpenID(
server_url=server_url,
@ -16,14 +24,14 @@ class AuthenticationService:
)
return keycloak_openid
@staticmethod
def get_keycloak_token(keycloak_openid, user, password):
"""Get_keycloak_token."""
token = keycloak_openid.token(user, password)
return token
# @staticmethod
# def get_keycloak_token(keycloak_openid, user, password):
# """Get_keycloak_token."""
# token = keycloak_openid.token(user, password)
# return token
@staticmethod
def get_permission_by_token(keycloak_openid, token):
def get_permission_by_token(keycloak_openid: KeycloakOpenID, token: dict) -> Optional[list[dict]]:
"""Get_permission_by_token."""
# Get permissions by token
# KEYCLOAK_PUBLIC_KEY = keycloak_openid.public_key()
@ -37,15 +45,18 @@ class AuthenticationService:
return permissions
@staticmethod
def get_uma_permissions_by_token(keycloak_openid, token):
def get_uma_permissions_by_token(keycloak_openid: KeycloakOpenID, token: dict) -> Optional[list[dict]]:
"""Get_uma_permissions_by_token."""
permissions = keycloak_openid.uma_permissions(token["access_token"])
return permissions
@staticmethod
def get_uma_permissions_by_token_for_resource_and_scope(
keycloak_openid, token, resource, scope
):
keycloak_openid: KeycloakOpenID,
token: dict,
resource: str,
scope: str
) -> Optional[list[dict]]:
"""Get_uma_permissions_by_token_for_resource_and_scope."""
permissions = keycloak_openid.uma_permissions(
token["access_token"], permissions=f"{resource}#{scope}"
@ -54,8 +65,11 @@ class AuthenticationService:
@staticmethod
def get_auth_status_for_resource_and_scope_by_token(
keycloak_openid, token, resource, scope
):
keycloak_openid: KeycloakOpenID,
token: dict,
resource: str,
scope: str
) -> AuthStatus:
"""Get_auth_status_for_resource_and_scope_by_token."""
auth_status = keycloak_openid.has_uma_access(
token["access_token"], f"{resource}#{scope}"

View File

@ -1,7 +1,7 @@
"""Test_authentication."""
from keycloak.authorization import Authorization
from keycloak.keycloak_openid import KeycloakOpenID
from keycloak.uma_permissions import AuthStatus
from keycloak.authorization import Authorization # type: ignore
from keycloak.keycloak_openid import KeycloakOpenID # type: ignore
from keycloak.uma_permissions import AuthStatus # type: ignore
from spiffworkflow_backend.services.authentication_service import AuthenticationService
@ -17,7 +17,7 @@ resource = "View Account Resource"
scope = "account:view"
def test_get_keycloak_openid_client():
def test_get_keycloak_openid_client() -> None:
"""Test_get_keycloak_openid_client."""
keycloak_openid_client = AuthenticationService.get_keycloak_openid(
server_url, client_id, realm_name, client_secret_key
@ -26,7 +26,7 @@ def test_get_keycloak_openid_client():
assert isinstance(keycloak_openid_client.authorization, Authorization)
def test_get_keycloak_token():
def test_get_keycloak_token() -> None:
"""Test_get_keycloak_token."""
keycloak_openid = AuthenticationService.get_keycloak_openid(
server_url, client_id, realm_name, client_secret_key
@ -40,7 +40,7 @@ def test_get_keycloak_token():
assert token["token_type"] == "Bearer"
def test_get_permission_by_token():
def test_get_permission_by_token() -> None:
"""Test_get_permission_by_token."""
keycloak_openid = AuthenticationService.get_keycloak_openid(
server_url, client_id, realm_name, client_secret_key
@ -50,12 +50,12 @@ def test_get_permission_by_token():
)
token = keycloak_openid.token(user, password)
AuthenticationService.get_permission_by_token(keycloak_openid, token)
permissions = AuthenticationService.get_permission_by_token(keycloak_openid, token)
# TODO: permissions comes back as None. Is this right?
print("test_get_permission_by_token")
def test_get_uma_permissions_by_token():
def test_get_uma_permissions_by_token() -> None:
"""Test_get_uma_permissions_by_token."""
keycloak_openid = AuthenticationService.get_keycloak_openid(
server_url, client_id, realm_name, client_secret_key
@ -75,7 +75,7 @@ def test_get_uma_permissions_by_token():
assert permission["scopes"][0] == "account:view"
def test_get_uma_permissions_by_token_for_resource_and_scope():
def test_get_uma_permissions_by_token_for_resource_and_scope() -> None:
"""Test_get_uma_permissions_by_token_for_resource_and_scope."""
keycloak_openid = AuthenticationService.get_keycloak_openid(
server_url, client_id, realm_name, client_secret_key
@ -98,7 +98,7 @@ def test_get_uma_permissions_by_token_for_resource_and_scope():
assert permission["scopes"][0] == scope
def test_get_auth_status_for_resource_and_scope_by_token():
def test_get_auth_status_for_resource_and_scope_by_token() -> None:
"""Test_get_auth_status_for_resource_and_scope_by_token."""
keycloak_openid = AuthenticationService.get_keycloak_openid(
server_url, client_id, realm_name, client_secret_key