Go File sharing HTTP server with auth and user rights management
Using go get:
# dependencies
$ go get github.com/auth0/go-jwt-middleware github.com/gorilla/handlers github.com/lib/pq github.com/kennygrant/sanitize github.com/ademilly/auth
# micro-share
$ go get github.com/ademilly/micro-shareUsing docker:
$ docker-compose builddocker-compose sets up a psql database, storing user data
$ docker-compose up -d
Creating network "micro-share_default" with the default driver
Creating micro-share_pgsql_1 ... donePOSTGRES_USER and POSTGRES_PASSWORD can be defined using a .env file or through environment variables.
To initialize the psql database, use the psql/schema.sql file with your preferred tool to interact with a psql database. Using the psql CLI tool:
$ PGUSER=validusername PGPASSWORD=validpassword psql -h localhost -p 5432 < psql/schema.sqlThe default docker-compose.yml file expects a values for the following environment variables:
- JWT_KEY => string used to generate JWT tokens
- CERT => path to a certificate file (used for https)
- KEY => path to a key file (used for https)
Certificate and key file are expected to live in a local certs/ directory for the default docker-compose.yml.
$ micro-share -h
Usage: micro-share [options...]
Environment:
JWT_KEY: jwt token key
used for authentication,
expected to be different than the null string
Options:
-certificate string
[optional] path to TLS certificate file
-key string
[optional] path to TLS key file
-port string
port number on which to serve (default "8080")
-root string
path to root directory containing file to be served (default "/tmp")
$ JWT_TOKEN=something micro-share
YYYY/MM/DD HH:mm:ss serving on http://0.0.0.0:8080
$ JWT_TOKEN=something micro-share -port 80 -root path/to/dir
YYYY/MM/DD HH:mm:ss serving on http://0.0.0.0:80
$ JWT_TOKEN=something micro-share -certificate mydomain.crt -key mydomain.key -port 443 -root path/to/dir
YYYY/MM/DD HH:mm:ss serving on https://0.0.0.0:443The JWT_KEY environment variable is used to initialize the token middleware; thus a token generated by a micro-share service at login with a certain JWT_KEY value can not be used to access another micro-share service using another JWT_KEY value.
/used for health checking; serves a welcome message/loginPOST to login; data should be send as json in request body/new-userPOST to create new user; data should be send as json in request body/new-groupPOST to create new group; data should be send as json in request body/new-memberPOST to add a user to a group; data should be send as json in request body/new-readerPOST to allow a user to list and download the file matching the givenbase64md5hash; data should be send as json in request body/uploadPOST file to upload file/get/base64md5hashGET to download file matchingbase64md5hash; is protected by JWT token
Health check:
$ curl https://micro-share.mydomain.com
welcome to this micro-share!Login:
$ curl -X POST -H 'Content-Type: application/json' -d '{ "username": "validusername", "password": "validpassword" }' https://micro-share.mydomain.com/login
SOME.JWT.TOKENNew User:
$ curl -X POST -H 'Content-Type: application/json' -d '{ "username": "validusername", "password": "validpassword" }' https://micro-share.mydomain.com/new-user
SOME_IDNew Group:
$ curl -X POST -H 'Content-Type: application/json' -d '{ "group_name": "valid_group_name" }' https://micro-share.mydomain.com/new-group
SOME_IDNew Member:
$ curl -X POST -H 'Content-Type: application/json' -d '{ "group_name": "valid_group_name", "username": "validusername" }' https://micro-share.mydomain.com/new-member
SOME_IDUpload file:
$ curl -F 'uploadFile=@path/to/file' -H "Authorization: Bearer SOME.JWT.TOKEN" https://micro-share.mydomain.com/upload
base64md5hashAdd Reader:
$ curl -X POST -H 'Content-Type: application/json' -d '{ "group_name": "valid_group_name", "md5": "base64md5hash" }' https://micro-share.mydomain.com/new-reader
SOME_IDList files:
$ curl -H 'Authorization: Bearer SOME.JWT.TOKEN' https://micro-share.mydomain.com/list
somefile - base64md5hash
...Download file:
$ curl -H "Authorization: Bearer SOME.JWT.TOKEN" https://micro-share.mydomain.com/get/base64md5hash
content of file $ wget --header "Authorization: Bearer SOME.JWT.TOKEN" https://micro-share.mydomain.com/get/base64md5hash
=> download content of target file to new file at path base64md5hash