Kaynağa Gözat

Undo auth stuff

master
Roderic Day 5 yıl önce
ebeveyn
işleme
31342b768d
3 değiştirilmiş dosya ile 31 ekleme ve 38 silme
  1. +7
    -6
      pico.js
  2. +2
    -21
      pico.py
  3. +22
    -11
      test.py

+ 7
- 6
pico.js Dosyayı Görüntüle

@@ -20,14 +20,13 @@ const Login = {
connect(username)
},
sendLogout: (e) => {
localStorage.removeItem('username')
State.websocket.send(JSON.stringify({action: 'logout'}))
State.posts = []
},
view() {
return m('.login',
m('form', {onsubmit: Login.sendLogin},
m('input', {oncreate: autoFocus, name: 'username', autocomplete: 'off'}),
m('input', {oncreate: autoFocus, name: 'username', autocomplete: 'off', value: localStorage.username}),
m('button', 'Login'),
),
State.kind === 'error' && m('.error', State.info),
@@ -70,12 +69,14 @@ const Main = {
m.mount(document.body, Main)

const connect = (username) => {
const wsUrl = location.href
.replace('http', 'ws')
.replace(/:\/\//, `://${username}@`)
const wsUrl = location.href.replace('http', 'ws')

State.websocket = new WebSocket(wsUrl)

State.websocket.onopen = (e) => {
State.websocket.send(JSON.stringify({action: 'login', username}))
}

State.websocket.onmessage = (e) => {
const message = JSON.parse(e.data)
if(message.kind === 'post') {
@@ -99,7 +100,7 @@ const connect = (username) => {

State.websocket.onclose = (e) => {
if(!e.wasClean) {
setTimeout(connect, 100, username)
setTimeout(connect, 1000, username)
}
m.redraw()
}

+ 2
- 21
pico.py Dosyayı Görüntüle

@@ -7,10 +7,6 @@ import functools
from pathlib import Path

import websockets
from websockets.headers import (
build_www_authenticate_basic,
parse_authorization_basic,
)


rooms = collections.defaultdict(set)
@@ -18,13 +14,6 @@ rooms = collections.defaultdict(set)

class PicoProtocol(websockets.WebSocketServerProtocol):

def http_unauthorized(self, message, realm='pico'):
return (
http.HTTPStatus.UNAUTHORIZED,
[("WWW-Authenticate", build_www_authenticate_basic(realm))],
message.encode(),
)

def serve_file(self, path):
document = Path(__file__, '..', Path(path).name).resolve()
if not document.is_file():
@@ -45,11 +34,6 @@ class PicoProtocol(websockets.WebSocketServerProtocol):
async def process_request(self, path, request_headers):
if request_headers.get('Upgrade') != 'websocket':
return self.serve_file(path)
try:
authorization = request_headers['Authorization']
self.username, password = parse_authorization_basic(authorization)
except Exception as error:
return self.http_unauthorized('No username found')
return await super().process_request(path, request_headers)


@@ -82,11 +66,7 @@ async def core(ws, path, server_name):
sockets = rooms[path]

while True:
if ws in sockets:
data = await recv_json(ws)
else:
data = {'action': 'login'}

data = await recv_json(ws)
ts = datetime.datetime.now().isoformat()
reply = functools.partial(send_json, websocket=ws, ts=ts)
error = functools.partial(reply, kind='error')
@@ -96,6 +76,7 @@ async def core(ws, path, server_name):
await error(info='Message without action is invalid')

elif data['action'] == 'login':
ws.username = data['username']
if not ws.username:
await error(info='Username not allowed')
break

+ 22
- 11
test.py Dosyayı Görüntüle

@@ -68,7 +68,8 @@ async def _test(*clients):


def test_happy_path():
client = _make_client('ws://TestUser@localhost:8642/A', 0.1, Script()
client = _make_client('ws://localhost:8642/A', 0.1, Script()
+ {'action': 'login', 'username': 'TestUser'}
- {'kind': 'update', 'users': ['TestUser'], 'info': 'Welcome to /A', 'username': 'TestUser'}
+ {'action': 'post', 'text': 'Hello World!'}
- {'kind': 'post', 'source': 'TestUser', 'text': 'Hello World!'}
@@ -77,14 +78,17 @@ def test_happy_path():


def test_name_taken():
client1 = _make_client('ws://A@localhost:8642/', 0.10, Script()
client1 = _make_client('ws://localhost:8642/', 0.10, Script()
+ {'action': 'login', 'username': 'A'}
- {'kind': 'update', 'users': ['A'], 'info': 'Welcome to /', 'username': 'A'}
- {'kind': 'update', 'users': ['A', 'B'], 'info': 'B joined'}
)
client2 = _make_client('ws://A@localhost:8642/', 0.11, Script()
client2 = _make_client('ws://localhost:8642/', 0.11, Script()
+ {'action': 'login', 'username': 'A'}
- {'kind': 'error', 'info': 'Username taken'}
)
client3 = _make_client('ws://B@localhost:8642/', 0.12, Script()
client3 = _make_client('ws://localhost:8642/', 0.12, Script()
+ {'action': 'login', 'username': 'B'}
- {'kind': 'update', 'users': ['A', 'B'], 'info': 'Welcome to /', 'username': 'B'}
- {'kind': 'update', 'users': ['B'], 'info': 'A left'}
)
@@ -92,14 +96,16 @@ def test_name_taken():


def test_interact():
client1 = _make_client('ws://Alice@localhost:8642/', 0.10, Script()
client1 = _make_client('ws://localhost:8642/', 0.10, Script()
+ {'action': 'login', 'username': 'Alice'}
- {'kind': 'update', 'users': ['Alice'], 'info': 'Welcome to /', 'username': 'Alice'}
- {'kind': 'update', 'users': ['Alice', 'Bob'], 'info': 'Bob joined'}
+ {'action': 'post', 'text': 'Hey Bob!'}
- {'kind': 'post', 'source': 'Alice', 'text': 'Hey Bob!'}
- {'kind': 'post', 'source': 'Bob', 'text': 'Howdy!'}
)
client2 = _make_client('ws://Bob@localhost:8642/', 0.11, Script()
client2 = _make_client('ws://localhost:8642/', 0.11, Script()
+ {'action': 'login', 'username': 'Bob'}
- {'kind': 'update', 'users': ['Alice', 'Bob'], 'info': 'Welcome to /', 'username': 'Bob'}
- {'kind': 'post', 'source': 'Alice', 'text': 'Hey Bob!'}
+ {'action': 'post', 'text': 'Howdy!'}
@@ -111,7 +117,8 @@ def test_interact():


def test_party():
client1 = _make_client('ws://Norman@localhost:8642/', 0.10, Script()
client1 = _make_client('ws://localhost:8642/', 0.10, Script()
+ {'action': 'login', 'username': 'Norman'}
- {'kind': 'update', 'users': ['Norman'], 'info': 'Welcome to /', 'username': 'Norman'}
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Ray joined'}
- {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Emma joined'}
@@ -119,14 +126,16 @@ def test_party():
- {'kind': 'post', 'source': 'Norman', 'text': 'なに?'}
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Emma left'}
)
client2 = _make_client('ws://Ray@localhost:8642/', 0.11, Script()
client2 = _make_client('ws://localhost:8642/', 0.11, Script()
+ {'action': 'login', 'username': 'Ray'}
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Welcome to /', 'username': 'Ray'}
- {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Emma joined'}
- {'kind': 'post', 'source': 'Norman', 'text': 'なに?'}
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Emma left'}
- {'kind': 'update', 'users': ['Ray'], 'info': 'Norman left'}
)
client3 = _make_client('ws://Emma@localhost:8642/', 0.12, Script()
client3 = _make_client('ws://localhost:8642/', 0.12, Script()
+ {'action': 'login', 'username': 'Emma'}
- {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Welcome to /', 'username': 'Emma'}
- {'kind': 'post', 'source': 'Norman', 'text': 'なに?'}
)
@@ -134,12 +143,14 @@ def test_party():


def test_rooms():
client1 = _make_client('ws://Dandy@localhost:8642/A', 0.10, Script()
client1 = _make_client('ws://localhost:8642/A', 0.10, Script()
+ {'action': 'login', 'username': 'Dandy'}
- {'kind': 'update', 'users': ['Dandy'], 'info': 'Welcome to /A', 'username': 'Dandy'}
+ {'action': 'post', 'text': 'Hi'}
- {'kind': 'post', 'source': 'Dandy', 'text': 'Hi'}
)
client2 = _make_client('ws://Dandy@localhost:8642/B', 0.10, Script()
client2 = _make_client('ws://localhost:8642/B', 0.10, Script()
+ {'action': 'login', 'username': 'Dandy'}
- {'kind': 'update', 'users': ['Dandy'], 'info': 'Welcome to /B', 'username': 'Dandy'}
+ {'action': 'post', 'text': 'Howdy'}
- {'kind': 'post', 'source': 'Dandy', 'text': 'Howdy'}

Yükleniyor…
İptal
Kaydet