Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Log In/Out and CORS functionality for the Vue dev Server #2617

Merged
merged 15 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/api/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ def __init__(self, services):
async def enable(self):
self.app_svc.application.router.add_static('/assets', 'magma/dist/assets/', append_version=True)
# unauthorized GUI endpoints
self.app_svc.application.router.add_route('*', '/', self.landing)
self.app_svc.application.router.add_route('*', '/enter', self.validate_login)
self.app_svc.application.router.add_route('*', '/logout', self.logout)
self.app_svc.application.router.add_route('GET', '/', self.landing)
self.app_svc.application.router.add_route('POST', '/enter', self.validate_login)
self.app_svc.application.router.add_route('POST', '/logout', self.logout)
self.app_svc.application.router.add_route('GET', '/login', self.login)
self.app_svc.application.router.add_route('POST', '/login', self.login)
# unauthorized API endpoints
self.app_svc.application.router.add_route('*', '/file/download', self.download_file)
self.app_svc.application.router.add_route('POST', '/file/upload', self.upload_file)
Expand All @@ -50,7 +51,6 @@ async def login(self, request):
async def validate_login(self, request):
return await self.auth_svc.login_user(request)

@template('login.html')
async def logout(self, request):
await self.auth_svc.logout_user(request)

Expand Down
14 changes: 14 additions & 0 deletions app/service/app_svc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from datetime import datetime, timezone
from importlib import import_module

import aiohttp_cors
import aiohttp_jinja2
import jinja2
import yaml
Expand Down Expand Up @@ -92,6 +93,19 @@ async def resume_operations(self):
for op in await self.get_service('data_svc').locate('operations', match=dict(finish=None)):
self.loop.create_task(op.run(self.get_services()))

async def enable_cors(self):
cors = aiohttp_cors.setup(self.application, defaults={
"http://localhost:3000": aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
)
})
for route in list(self.application.router.routes()):
if route._method != '*':
cors.add(route)


async def load_plugins(self, plugins):
def trim(p):
if p.startswith('.'):
Expand Down
2 changes: 1 addition & 1 deletion app/service/auth_svc.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async def create_user(self, username, password, group):
@staticmethod
async def logout_user(request):
await forget(request, web.Response())
raise web.HTTPFound('/login')
raise web.HTTPFound('/')

async def login_user(self, request):
"""Log a user in and save the session
Expand Down
4 changes: 2 additions & 2 deletions app/service/login_handlers/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, services):
self._ldap_config = self.get_config('ldap')

async def handle_login(self, request, **kwargs):
data = await request.post()
data = await request.json()
username = data.get('username')
password = data.get('password')
if username and password:
Expand All @@ -32,7 +32,7 @@ async def handle_login(self, request, **kwargs):
raise Exception('Auth service not available.')
await auth_svc.handle_successful_login(request, username)
self.log.debug('%s failed login attempt: ', username)
raise web.HTTPFound('/login')
raise web.HTTPUnauthorized

async def handle_login_redirect(self, request, **kwargs):
"""Handle login redirect.
Expand Down
1 change: 0 additions & 1 deletion magma
Submodule magma deleted from 9e60cb
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
aiohttp-jinja2==1.5.0
aiohttp==3.8.1
aiohttp-cors==0.7.0
aiohttp_session==2.9.0
aiohttp-security==0.4.0
aiohttp-apispec==2.2.3
Expand Down
8 changes: 5 additions & 3 deletions server.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ async def start_server():
await web.TCPSite(runner, BaseWorld.get_config('host'), BaseWorld.get_config('port')).start()


def run_tasks(services, start_vue_server=False):
def run_tasks(services, run_vue_server=False):
loop = asyncio.get_event_loop()
loop.create_task(app_svc.validate_requirements())
loop.run_until_complete(data_svc.restore_state())
loop.run_until_complete(knowledge_svc.restore_state())
loop.run_until_complete(RestApi(services).enable())
loop.run_until_complete(app_svc.register_contacts())
loop.run_until_complete(app_svc.load_plugins(args.plugins))
if run_vue_server:
loop.run_until_complete(app_svc.enable_cors())
argaudreau marked this conversation as resolved.
Show resolved Hide resolved
loop.run_until_complete(data_svc.load_data(loop.run_until_complete(data_svc.locate('plugins', dict(enabled=True)))))
loop.run_until_complete(app_svc.load_plugin_expansions(loop.run_until_complete(data_svc.locate('plugins', dict(enabled=True)))))
loop.run_until_complete(auth_svc.set_login_handlers(services))
Expand All @@ -68,7 +70,7 @@ def run_tasks(services, start_vue_server=False):
loop.create_task(learning_svc.build_model())
loop.create_task(app_svc.watch_ability_files())
loop.run_until_complete(start_server())
if start_vue_server:
if run_vue_server:
loop.run_until_complete(start_vue_dev_server())
try:
logging.info('All systems ready.')
Expand Down Expand Up @@ -162,4 +164,4 @@ def list_str(values):
asyncio.get_event_loop().run_until_complete(data_svc.destroy())
asyncio.get_event_loop().run_until_complete(knowledge_svc.destroy())

run_tasks(services=app_svc.get_services(), start_vue_server=args.uidev)
run_tasks(services=app_svc.get_services(), run_vue_server=args.uidev)