Xe/flitter — pull request #12 by Xe (2014-10-30 06:10:02+00:00)
Title: lagann: support authentication

Ref #9

This is a giant pull request that completely changes how the routing
in Lagann works. Lagann now uses three routers:

- `routing` for `/` and all child paths
- `appmux` for internal API calls
- `usermux` for API calls that need user auth

Thanks to the magic of Negroni, they can each have a separate middleware
stack and I can get the benefits of allowing some routes to require
authentication by the new `X-Lagann-Auth` header as well as getting the
advantage of Drone's router where it makes sense, all while keeping things
devilishly simple.

```
λ curl -X POST --data "username=xena&password=foo&sshkey=$(cat .ssh/id_rsa.pub)" http://127.0.0.1:3000/register
{
"code":200,
"data":[
{"authkey":"fb0bde3f-7820-488d-b0cc-f35b8bb3f427","user":"xena"}
],
"message":"User xena created."
}

λ curl -X POST --data "username=xena&password=foo&sshkey=$(cat .ssh/id_rsa.pub)" http://127.0.0.1:3000/register
{
"code":409,
"data":null,
"message":"User xena already exists"
}

λ curl -X POST --data "username=xena&password=foo" http://127.0.0.1:3000/login
{
"code":200,
"data":[
{"authkey":"bc5c493e-7bc5-4d41-a1e4-9b7ec3b37a2a","user":"xena"}
],
"message":"Logged in as xena."
}

λ etcdctl get /flitter/lagann/users/xena/password
bSNmDdYlIt8KRUdR8XLOgxXMsAEMJX+o4M0u+BKXKag=
```

```
λ curl -H "X-Lagann-Auth: bc5c493e-7bc5-4d41-a1e4-9b7ec3b37a2a" -X POST http://127.0.0.1:3000/user/create --data '{"name": "foobang", "user": ["xena"]}'
{
"code":200,
"data":null,
"message":"App foobang created"
}

λ curl -X POST http://127.0.0.1:3000/user/create --data '{"name": "foobang", "user": ["xena"]}'
{
"code": 401,
"message": "Not authorized",
"data": ["Try setting X-Lagann-Auth."]
}
```

Changes:

8fb9f47 (Sam Dodrill)
lagann: login, you are implemented

4ada678 (Sam Dodrill)
lagann/main: update for servemux oddities

271e98c (Sam Dodrill)
lagann/main: remove unused startUnit

"A real man never dies, even when he's killed!"
- Kamina

0c407b5 (Sam Dodrill)
lagann: move internal views to their own file

Also get rid of that dirty reply() function completely.

59a589f (Sam Dodrill)
lagann: use auth, break into several servemuxes

Giant commit for good reason

All routed HandleFuncs are in a file based on if they are prelogin or
postlogin.

4162402 (Sam Dodrill)
lagann/datatypes: User now has a Password

b86f12a (Sam Dodrill)
lib/utils: add simple password hashing function

8ab0d36 (Sam Dodrill)
lib/utils: move lagann/main.reply into a util function

3db05dd (Sam Dodrill)
middlewares/auth: document all functions better

c3488be (Sam Dodrill)
middlewares: introduce correct auth header checker

RIP SSH key-based header. You were a cool idea while you lasted. :(

e0abae0 (Sam Dodrill)
deprecate X-Lagann-Auth

30cb4c6 (Sam Dodrill)
middlewares/auth: bail out on an auth error

22ffed6 (Sam Dodrill)
move middlewares into lib

05d3d91 (Sam Dodrill)
lagann/middlewares: add etcd ssh key auth middleware

61b2d95 (Sam Dodrill)
lib: add utils package

73217fe (Sam Dodrill)
lagann: add parent middlewares package