2021-06-22 19:46:44 +00:00
|
|
|
---
|
|
|
|
title: Github
|
|
|
|
---
|
|
|
|
|
2022-06-15 19:31:34 +00:00
|
|
|
<span class="badge badge--primary">Support level: authentik</span>
|
|
|
|
|
2021-06-22 19:46:44 +00:00
|
|
|
Allows users to authenticate using their Github credentials
|
|
|
|
|
|
|
|
## Preparation
|
|
|
|
|
|
|
|
The following placeholders will be used:
|
|
|
|
|
2022-05-09 19:22:41 +00:00
|
|
|
- `authentik.company` is the FQDN of the authentik install.
|
|
|
|
- `www.my.company` Homepage URL for your site
|
2021-06-22 19:46:44 +00:00
|
|
|
|
|
|
|
## Github
|
|
|
|
|
2021-08-02 08:27:07 +00:00
|
|
|
1. Create an OAuth app under Developer Settings https://github.com/settings/developers by clicking on the **Register a new application**
|
2021-06-22 19:46:44 +00:00
|
|
|
|
|
|
|
![Register OAuth App](githubdeveloper1.png)
|
|
|
|
|
2021-10-18 14:35:12 +00:00
|
|
|
2. **Application Name:** Choose a name users will recognize ie: authentik
|
2021-06-22 19:46:44 +00:00
|
|
|
3. **Homepage URL**:: www.my.company
|
|
|
|
4. **Authorization callback URL**: https://authentik.company/source/oauth/callback/github
|
|
|
|
5. Click **Register Application**
|
|
|
|
|
|
|
|
Example screenshot
|
|
|
|
|
2022-05-17 22:03:02 +00:00
|
|
|
![](githubdeveloperexample.png)
|
2021-06-22 19:46:44 +00:00
|
|
|
|
|
|
|
6. Copy the **Client ID** and _save it for later_
|
2022-05-09 19:22:41 +00:00
|
|
|
7. Click **Generate a new client secret** and _save it for later_ You will not be able to see the secret again, so be sure to copy it now.
|
2021-06-22 19:46:44 +00:00
|
|
|
|
2021-10-18 14:35:12 +00:00
|
|
|
## authentik
|
2021-06-22 19:46:44 +00:00
|
|
|
|
2022-05-17 22:03:02 +00:00
|
|
|
8. Under _Directory -> Federation & Social login_ Click **Create Github OAuth Source**
|
2021-06-22 19:46:44 +00:00
|
|
|
|
|
|
|
9. **Name**: Choose a name (For the example I use Github)
|
|
|
|
10. **Slug**: github (If you choose a different slug the URLs will need to be updated to reflect the change)
|
2022-05-09 19:22:41 +00:00
|
|
|
11. **Consumer Key:** Client ID from step 6
|
2021-06-22 19:46:44 +00:00
|
|
|
12. **Consumer Secret:** Client Secret from step 7
|
|
|
|
|
2021-10-18 14:35:12 +00:00
|
|
|
Here is an example of a complete authentik Github OAuth Source
|
2021-06-22 19:46:44 +00:00
|
|
|
|
2022-05-17 22:03:02 +00:00
|
|
|
![](githubexample2.png)
|
2021-06-22 19:46:44 +00:00
|
|
|
|
|
|
|
Save, and you now have Github as a source.
|
|
|
|
|
|
|
|
:::note
|
2022-12-12 15:52:00 +00:00
|
|
|
For more details on how-to have the new source display on the Login Page see [here](../general#add-sources-to-default-login-page).
|
2021-08-02 08:27:07 +00:00
|
|
|
:::
|
2022-01-03 21:00:00 +00:00
|
|
|
|
|
|
|
### Checking for membership of a GitHub Organisation
|
|
|
|
|
|
|
|
:::info
|
|
|
|
Requires authentik 2021.12.5.
|
|
|
|
:::
|
|
|
|
|
|
|
|
To check if the user is member of an organisation, you can use the following policy on your flows:
|
|
|
|
|
2022-10-25 19:17:08 +00:00
|
|
|
:::info
|
|
|
|
Make sure to include `read:org` in the sources' _Scopes_ setting.
|
|
|
|
:::
|
|
|
|
|
2022-01-03 21:00:00 +00:00
|
|
|
```python
|
2022-05-27 14:07:33 +00:00
|
|
|
# Ensure flow is only run during oauth logins via Github
|
2022-10-25 19:17:08 +00:00
|
|
|
if context["source"].provider_type != "github":
|
2022-05-27 14:07:33 +00:00
|
|
|
return True
|
|
|
|
|
2022-01-03 21:00:00 +00:00
|
|
|
accepted_org = "foo"
|
|
|
|
|
|
|
|
# Get the user-source connection object from the context, and get the access token
|
2022-10-25 19:17:08 +00:00
|
|
|
connection = context["goauthentik.io/sources/connection"]
|
2022-01-03 21:00:00 +00:00
|
|
|
access_token = connection.access_token
|
|
|
|
|
|
|
|
# We also access the user info authentik already retrieved, to get the correct username
|
|
|
|
github_username = context["oauth_userinfo"]
|
|
|
|
|
|
|
|
# Github does not include Organisations in the userinfo endpoint, so we have to call another URL
|
|
|
|
|
2022-10-25 19:17:08 +00:00
|
|
|
orgs_response = requests.get(
|
2022-01-03 21:00:00 +00:00
|
|
|
"https://api.github.com/user/orgs",
|
|
|
|
auth=(github_username["login"], access_token),
|
|
|
|
headers={
|
|
|
|
"accept": "application/vnd.github.v3+json"
|
|
|
|
}
|
2022-10-25 19:17:08 +00:00
|
|
|
)
|
|
|
|
orgs_response.raise_for_status()
|
|
|
|
orgs = orgs_response.json()
|
2022-01-03 21:00:00 +00:00
|
|
|
|
|
|
|
# `orgs` will be formatted like this
|
|
|
|
# [
|
|
|
|
# {
|
|
|
|
# "login": "beryjuorg",
|
|
|
|
# [...]
|
|
|
|
# }
|
|
|
|
# ]
|
|
|
|
user_matched = any(org['login'] == accepted_org for org in orgs)
|
|
|
|
if not user_matched:
|
|
|
|
ak_message(f"User is not member of {accepted_org}.")
|
|
|
|
return user_matched
|
|
|
|
```
|
|
|
|
|
|
|
|
If a user is not member of the chosen organisation, they will see this message
|
|
|
|
|
|
|
|
![](./github_org_membership.png)
|