Browse Source

Undo auth stuff

master
Roderic Day 5 years ago
parent
commit
31342b768d
3 changed files with 31 additions and 38 deletions
  1. +7
    -6
      pico.js
  2. +2
    -21
      pico.py
  3. +22
    -11
      test.py

+ 7
- 6
pico.js View File

connect(username) connect(username)
}, },
sendLogout: (e) => { sendLogout: (e) => {
localStorage.removeItem('username')
State.websocket.send(JSON.stringify({action: 'logout'})) State.websocket.send(JSON.stringify({action: 'logout'}))
State.posts = [] State.posts = []
}, },
view() { view() {
return m('.login', return m('.login',
m('form', {onsubmit: Login.sendLogin}, 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'), m('button', 'Login'),
), ),
State.kind === 'error' && m('.error', State.info), State.kind === 'error' && m('.error', State.info),
m.mount(document.body, Main) m.mount(document.body, Main)


const connect = (username) => { 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 = new WebSocket(wsUrl)


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

State.websocket.onmessage = (e) => { State.websocket.onmessage = (e) => {
const message = JSON.parse(e.data) const message = JSON.parse(e.data)
if(message.kind === 'post') { if(message.kind === 'post') {


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

+ 2
- 21
pico.py View File

from pathlib import Path from pathlib import Path


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




rooms = collections.defaultdict(set) rooms = collections.defaultdict(set)


class PicoProtocol(websockets.WebSocketServerProtocol): 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): def serve_file(self, path):
document = Path(__file__, '..', Path(path).name).resolve() document = Path(__file__, '..', Path(path).name).resolve()
if not document.is_file(): if not document.is_file():
async def process_request(self, path, request_headers): async def process_request(self, path, request_headers):
if request_headers.get('Upgrade') != 'websocket': if request_headers.get('Upgrade') != 'websocket':
return self.serve_file(path) 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) return await super().process_request(path, request_headers)




sockets = rooms[path] sockets = rooms[path]


while True: 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() ts = datetime.datetime.now().isoformat()
reply = functools.partial(send_json, websocket=ws, ts=ts) reply = functools.partial(send_json, websocket=ws, ts=ts)
error = functools.partial(reply, kind='error') error = functools.partial(reply, kind='error')
await error(info='Message without action is invalid') await error(info='Message without action is invalid')


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

+ 22
- 11
test.py View File





def test_happy_path(): 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'} - {'kind': 'update', 'users': ['TestUser'], 'info': 'Welcome to /A', 'username': 'TestUser'}
+ {'action': 'post', 'text': 'Hello World!'} + {'action': 'post', 'text': 'Hello World!'}
- {'kind': 'post', 'source': 'TestUser', 'text': 'Hello World!'} - {'kind': 'post', 'source': 'TestUser', 'text': 'Hello World!'}




def test_name_taken(): 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'], 'info': 'Welcome to /', 'username': 'A'}
- {'kind': 'update', 'users': ['A', 'B'], 'info': 'B joined'} - {'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'} - {'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': ['A', 'B'], 'info': 'Welcome to /', 'username': 'B'}
- {'kind': 'update', 'users': ['B'], 'info': 'A left'} - {'kind': 'update', 'users': ['B'], 'info': 'A left'}
) )




def test_interact(): 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'], 'info': 'Welcome to /', 'username': 'Alice'}
- {'kind': 'update', 'users': ['Alice', 'Bob'], 'info': 'Bob joined'} - {'kind': 'update', 'users': ['Alice', 'Bob'], 'info': 'Bob joined'}
+ {'action': 'post', 'text': 'Hey Bob!'} + {'action': 'post', 'text': 'Hey Bob!'}
- {'kind': 'post', 'source': 'Alice', 'text': 'Hey Bob!'} - {'kind': 'post', 'source': 'Alice', 'text': 'Hey Bob!'}
- {'kind': 'post', 'source': 'Bob', 'text': 'Howdy!'} - {'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': 'update', 'users': ['Alice', 'Bob'], 'info': 'Welcome to /', 'username': 'Bob'}
- {'kind': 'post', 'source': 'Alice', 'text': 'Hey Bob!'} - {'kind': 'post', 'source': 'Alice', 'text': 'Hey Bob!'}
+ {'action': 'post', 'text': 'Howdy!'} + {'action': 'post', 'text': 'Howdy!'}




def test_party(): 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'], 'info': 'Welcome to /', 'username': 'Norman'}
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Ray joined'} - {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Ray joined'}
- {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Emma joined'} - {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Emma joined'}
- {'kind': 'post', 'source': 'Norman', 'text': 'なに?'} - {'kind': 'post', 'source': 'Norman', 'text': 'なに?'}
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Emma left'} - {'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': ['Norman', 'Ray'], 'info': 'Welcome to /', 'username': 'Ray'}
- {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Emma joined'} - {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Emma joined'}
- {'kind': 'post', 'source': 'Norman', 'text': 'なに?'} - {'kind': 'post', 'source': 'Norman', 'text': 'なに?'}
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Emma left'} - {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Emma left'}
- {'kind': 'update', 'users': ['Ray'], 'info': 'Norman 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': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Welcome to /', 'username': 'Emma'}
- {'kind': 'post', 'source': 'Norman', 'text': 'なに?'} - {'kind': 'post', 'source': 'Norman', 'text': 'なに?'}
) )




def test_rooms(): 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'} - {'kind': 'update', 'users': ['Dandy'], 'info': 'Welcome to /A', 'username': 'Dandy'}
+ {'action': 'post', 'text': 'Hi'} + {'action': 'post', 'text': 'Hi'}
- {'kind': 'post', 'source': 'Dandy', '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'} - {'kind': 'update', 'users': ['Dandy'], 'info': 'Welcome to /B', 'username': 'Dandy'}
+ {'action': 'post', 'text': 'Howdy'} + {'action': 'post', 'text': 'Howdy'}
- {'kind': 'post', 'source': 'Dandy', 'text': 'Howdy'} - {'kind': 'post', 'source': 'Dandy', 'text': 'Howdy'}

Loading…
Cancel
Save