CSXL accounts can be linked with GitHub accounts for two interesting purposes:
- To enable GitHub avatars to serve as a user's CSXL profile picture. This allows us to avoid the need to host user profile pictures on our own servers, and also allows users to change their profile picture by changing their GitHub profile picture.
- To enable future extensions which integrate with GitHub, including:
- Login via GitHub rather than UNC SSO (this would give a path toward alumni and industry partners using the CSXL without a UNC SSO account)
- Public CSXL profiles that link to GitHub projects
- Course integrations, notably COMP423 sprint tracking, etc.
Three fields were added to UserEntity:
github
- the GitHub username of the usergithub_avatar_url
- the URL of the user's GitHub avatargithub_id
- the GitHub user ID of the user
Routes were added to api/authentication.py
to handle the GitHub OAuth flow.
GET /auth/github_oauth_login_url
- This produces the URL the client is redirected to in order to initiate the GitHub OAuth flow. The URL is constructed using theGITHUB_CLIENT_ID
environment variable.GET /auth/github
- Upon return from GitHub, the user is redirected to this route with a code. This page produces some HTML to bootstrap the linkage of the CSXL account with the GitHub account. This is necessary due to the CSXL JWT bearer token stored in localStorage.POST /auth/github
- The bootstrapped HTML initiates a POST request to this route, which completes the linkage of the CSXL account with the GitHub account.DELETE /auth/github
- This route unlinks the CSXL account from the GitHub account.
From the user interface, a user can visit their profile to manage the link / unlink with their GitHub account.
To utilize the GitHub integration in development, you will need to create a GitHub OAuth app on your personal account and add the appropriate environment variables to your .env
file, as follows.
-
Go to GitHub > Settings > Developer Settings > Oauth Apps > New OAuth App
-
Fill out the form as follows:
- Application name: CSXL
- Homepage URL:
https://csxl.unc.edu
- Authorization callback URL:
http://localhost:1560/auth/github
- After creating the app, copy the client secret
-
In
backend/.env
, add the following environment variables, with the information from the GitHub OAuth app:
GITHUB_CLIENT_ID
GITHUB_CLIENT_SECRET
- Finally, you will need to either reset the database or run
alembic upgrade head
to add the fields touser
.
In production, the official CSXL GitHub OAuth app's Client ID and secret need to be added to the environment of the deployment:
GITHUB_CLIENT_ID
GITHUB_CLIENT_SECRET
Additionally, the migration 48c0e needs to be run against on the production database: alembic upgrade head
.
For deeper GitHub integration, we will need to store each user's GitHub OAuth token in some kind of store (ephemeral/redis or the database). This will require caution to avoid token leakage through the API.