Skip to content

Commit 0732eaa

Browse files
authored
Merge branch 'main' into fix/script-add
2 parents f0756ac + c7a49fd commit 0732eaa

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/viur_cli/scriptor/cli.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from viur.scriptor import Modules
1313
from ..cli import cli
1414
from ..cli import scriptor_config
15+
from .login import ensure_login
1516

1617
# Global modules instance that will be initialized when needed
1718
_modules = None
@@ -60,6 +61,14 @@ def setup():
6061
"""
6162

6263
base_url = scriptor_config.get("base_url")
64+
65+
try:
66+
click.echo("This Feature is only avalible on viur-core 3.8.19 or higher.")
67+
res = ensure_login("", host=base_url)
68+
if res:
69+
return
70+
except KeyboardInterrupt:
71+
pass
6372
try:
6473
session = requests.session()
6574
skey = session.get(base_url + "/json/skey")
@@ -95,11 +104,11 @@ def check_session(ctx: click.Context):
95104

96105
response = s.get(base_url + "/vi/user/view/self", cookies=scriptor_config.get("cookies", {}))
97106
if not response.ok:
98-
click.echo("Invalid session, please run `viur script setup` again.")
107+
click.echo("Invalid session, please run `viur script setup` again. okay ?")
99108
ctx.invoke(setup)
100109
ctx.close()
101110

102-
111+
103112
@script.command()
104113
@click.option('--force', default=False, help='Force replace files from server in local working directory')
105114
@click.pass_context

src/viur_cli/scriptor/login.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python3
2+
import contextvars
3+
import json
4+
import urllib.parse
5+
import webbrowser
6+
from pathlib import Path
7+
from wsgiref.simple_server import make_server
8+
from ..cli import scriptor_config
9+
import requests
10+
11+
PORT = 60_000
12+
13+
14+
15+
def callback_app(environ: dict, start_response):
16+
qs = urllib.parse.parse_qs(environ["QUERY_STRING"])
17+
callback_app.cookie.set(qs["cookie"][0])
18+
callback_app.app.set(qs["app"][0])
19+
start_response("200 OK", [('Content-Type', 'text/plain; charset=utf-8')])
20+
return [b"This was successful. You can now close this site."]
21+
22+
23+
callback_app.cookie = contextvars.ContextVar("Cookie")
24+
callback_app.app = contextvars.ContextVar("App")
25+
26+
27+
def ensure_login(
28+
app: str,
29+
*,
30+
open_browser: bool = True,
31+
host: str = None,
32+
) -> bool:
33+
34+
35+
with make_server('', PORT, callback_app) as httpd:
36+
sa = httpd.socket.getsockname()
37+
print("Serving HTTP on", sa[0], "port", sa[1], "...")
38+
39+
if host is None:
40+
host = f"https://{app}.appspot.com"
41+
url = f'{host}/vi/user/get_cookie_for_app?redirect_to=http://localhost:{PORT}'
42+
if open_browser:
43+
webbrowser.open(url)
44+
else:
45+
print("Open this URL in your browser:")
46+
print(url)
47+
48+
httpd.handle_request() # serve one request, then exit
49+
50+
cookie_str: str = callback_app.cookie.get()
51+
key, value = cookie_str.split(";", 1)[0].split("=")
52+
scriptor_config.get("cookies",{})[key] = value
53+
scriptor_config.save()
54+
55+
return True
56+
57+
58+
59+

0 commit comments

Comments
 (0)