API backend for Programming Buddies (projects management)
The documentation is dynamically generated by the server using Swagger and Swagger UI
- To view the documentation run the server using one of the set up guides below and then head to
/docsendpoint- e.g. https://localhost:5001/docs for local development
- Set up your environment
- Install docker-compose and do
docker-compose up - for information on how to authenticate for the API, see authentication
Requirements:
- Set up your environment
- MySQL Community Server
- Add line
CONNECT=mysql+pymysql://<user>:<password>@localhost:3306/<database name>to your.envfile to specify connection parameters
- Add line
- Pipenv
- Run
python -m pip install pipenvto install pipenv - Run
pipenv installinside the repository- If you have multiple Python versions installed you might need to specify which one to use
pipenv install --python=python3.7
- If you have multiple Python versions installed you might need to specify which one to use
- Run
- SSL
- On Mac or Linux install the OpenSSL tool and run
openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365inside the repository - On Windows
- Install OpenSSL for Windows here
- Execute the same command as on Mac inside the project directory
- On Mac or Linux install the OpenSSL tool and run
Run the server:
pipenv run python src/runserver.py- Run with flag
--reset-dbto drop and recreate all tables on start
- Run with flag
- For information on how to authenticate for the API, see authentication
- Create a file named
.env- Add line
FLASK_ENV=developmentto have the server automatically restart on file changes
- Add line
- Add line
APP_SECRET=somepasswordas app secret. This is used to sign sessions among other things and is required - Obtain credentials for GitHub OAuth
- Under your GitHub account Settings go to Developer settings and OAuth Apps
- Create a new one and set the homepage url to
https://localhost:5001/and Authorization callback tohttps://localhost:5001/login/github/authorized - Copy the Client Id and Client Secret from that site and save them in
.envasGITHUB_IDandGITHUB_SECRETrespectively
- Furthermore a
JWT_SECRET_KEYis required for signing the JWT-tokens- Pick a strong passphrase so that attackers can't brute-force it and sign tokens distinguishing as your server
Your .env file should now look something like example.env
- To authenticate you have to specify several attributes
account- currentlygithubis the only supported valueusername- your username of account on specified platform in the previous pointredirect- url where should you be redirected after authenticating with OAuth
- Put it all these together
https://<url:port>/<login-or-register>?account=<account>&username=<username>&redirect=<redirect>- e.g.:
https://localhost:5001/<login-or-register>?account=github&username=freddy&redirect=https://localhost:5001willlogin-or-registerusing GitHub account with namefreddywhile developing local with the server hosted onlocalhost:5001
- e.g.:
- You will be redirected to the specified url and get data with it
- If the request failed - when logging in and user was not registered (user not found in the database) or there was any internal error, you will get an error message and code
- If the request succeeded - you will get a JWT
tokenin the URL, that is what you need to authenticate
- Add to your requests
Authorizationheader with a valueBearer <token>where you replace<token>with yourtokenacquired in the step above
/loginroute logs in if the user exists in the database, otherwise returns a error message and code in the URL- Usage:
https://<url:port>/login?account=<account>&username=<username>&redirect=<redirect>- e.g.:
https://localhost:5001/login?account=github&username=freddy&redirect=https://localhost:5001
- e.g.:
/registerroute either logs in if the user exists in the database or registers (creates) a new user, in case of failure returns a error message and code in the URL- Usage:
https://<url:port>/register?account=<account>&username=<username>&redirect=<redirect>- e.g.:
https://localhost:5001/register?account=github&username=freddy&redirect=https://localhost:5001
- e.g.:
- To run multiple tests just specify the directory which contains them for example
pipenv run pytest tests/- This will run all the tests in the
testsdirectory
- This will run all the tests in the
- If you want to run test cases only in a particular file, then just give the full file path
pipenv run pytest tests/example.py
- Python
- Flask
- MySQL
- pyOpenSSL