2023-10-04 11:38:41 -05:00
# External Authentication
2022-11-07 13:04:00 -06:00
2025-01-07 15:10:16 -05:00
Coder supports external authentication via OAuth2.0. This allows enabling any OAuth provider as well as integrations with Git providers,
such as GitHub, GitLab, and Bitbucket.
External authentication can also be used to integrate with external services
like JFrog Artifactory and others.
2023-10-04 11:38:41 -05:00
To add an external authentication provider, you'll need to create an OAuth
2025-01-07 15:10:16 -05:00
application. The following providers have been tested and work with Coder:
2022-11-07 13:04:00 -06:00
- [Azure DevOps ](https://learn.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops )
2024-03-04 18:12:46 +00:00
- [Azure DevOps (via Entra ID) ](https://learn.microsoft.com/en-us/entra/architecture/auth-oauth2 )
2025-01-07 15:10:16 -05:00
- [BitBucket ](https://support.atlassian.com/bitbucket-cloud/docs/use-oauth-on-bitbucket-cloud/ )
2025-04-01 16:03:25 -05:00
- [GitHub ](#configure-a-github-oauth-app )
2025-01-07 15:10:16 -05:00
- [GitLab ](https://docs.gitlab.com/ee/integration/oauth_provider.html )
2022-11-07 13:04:00 -06:00
2025-01-07 15:10:16 -05:00
If you have experience with a provider that is not listed here, please
[file an issue ](https://github.com/coder/internal/issues/new?title=request%28docs%29%3A+external-auth+-+request+title+here%0D%0A&labels=["customer-feedback","docs"]&body=doc%3A+%5Bexternal-auth%5D%28https%3A%2F%2Fcoder.com%2Fdocs%2Fadmin%2Fexternal-auth%29%0D%0A%0D%0Aplease+enter+your+request+here%0D%0A )
## Configuration
2025-04-01 16:03:25 -05:00
### Set environment variables
2025-01-07 15:10:16 -05:00
After you create an OAuth application, set environment variables to configure the Coder server to use it:
2022-11-07 13:04:00 -06:00
2023-08-23 12:27:57 +03:00
```env
2024-04-20 12:26:53 -04:00
CODER_EXTERNAL_AUTH_0_ID = "<USER_DEFINED_ID>"
CODER_EXTERNAL_AUTH_0_TYPE = <github| gitlab| azure-devops| bitbucket-cloud| bitbucket-server| etc>
2025-01-07 15:10:16 -05:00
CODER_EXTERNAL_AUTH_0_CLIENT_ID = <OAuth app client ID>
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET = <OAuth app client secret>
2023-10-04 11:38:41 -05:00
2025-01-07 15:10:16 -05:00
# Optionally, configure a custom display name and icon:
2023-10-04 11:38:41 -05:00
CODER_EXTERNAL_AUTH_0_DISPLAY_NAME = "Google Calendar"
CODER_EXTERNAL_AUTH_0_DISPLAY_ICON = "https://mycustomicon.com/google.svg"
2022-11-07 13:04:00 -06:00
```
2025-04-01 16:03:25 -05:00
The `CODER_EXTERNAL_AUTH_0_ID` environment variable is used as an identifier for the authentication provider.
This variable is used as part of the callback URL path that you must configure in your OAuth provider settings.
If the value in your callback URL doesn't match the `CODER_EXTERNAL_AUTH_0_ID` value, authentication will fail with `redirect URI is not valid` .
Set it with a value that helps you identify the provider.
For example, if you use `CODER_EXTERNAL_AUTH_0_ID="primary-github"` for your GitHub provider,
configure your callback URL as `https://example.com/external-auth/primary-github/callback` .
### Add an authentication button to the workspace template
2024-04-20 12:26:53 -04:00
2025-01-07 15:10:16 -05:00
Add the following code to any template to add a button to the workspace setup page which will allow you to authenticate with your provider:
2023-06-18 14:01:37 -05:00
2025-01-07 15:10:16 -05:00
```tf
data "coder_external_auth" "<github|gitlab|azure-devops|bitbucket-cloud|bitbucket-server|other>" {
id = "<USER_DEFINED_ID>"
}
2024-04-20 12:26:53 -04:00
2025-01-07 15:10:16 -05:00
# GitHub Example (CODER_EXTERNAL_AUTH_0_ID="primary-github")
# makes a GitHub authentication token available at data.coder_external_auth.github.access_token
data "coder_external_auth" "github" {
id = "primary-github"
}
2024-04-20 12:26:53 -04:00
```
2025-04-01 16:03:25 -05:00
Inside your Terraform code, you now have access to authentication variables.
Reference the documentation for your chosen provider for more information on how to supply it with a token.
2023-03-23 10:53:42 -04:00
2025-01-07 15:10:16 -05:00
### Workspace CLI
2023-03-23 10:53:42 -04:00
2025-06-16 13:18:55 -04:00
Use [`external-auth` ](../../reference/cli/external-auth.md ) in the Coder CLI to access a token within the workspace:
2025-01-07 15:10:16 -05:00
```shell
2025-03-14 16:17:34 -05:00
coder external-auth access-token <USER_DEFINED_ID>
2023-03-23 10:53:42 -04:00
```
2025-04-09 11:16:00 -04:00
## Git Authentication in Workspaces
Coder provides automatic Git authentication for workspaces through SSH authentication and Git-provider specific env variables.
When performing Git operations, Coder first attempts to use external auth provider tokens if available.
If no tokens are available, it defaults to SSH authentication.
### OAuth (external auth)
2025-04-23 11:14:15 -04:00
For Git providers configured with [external authentication ](#configuration ), Coder can use OAuth tokens for Git operations over HTTPS.
When using SSH URLs (like `git@github.com:organization/repo.git` ), Coder uses SSH keys as described in the [SSH Authentication ](#ssh-authentication ) section instead.
2025-04-09 11:16:00 -04:00
2025-04-23 11:14:15 -04:00
For Git operations over HTTPS, Coder automatically uses the appropriate external auth provider
token based on the repository URL.
This works through Git's `GIT_ASKPASS` mechanism, which Coder configures in each workspace.
2025-04-09 11:16:00 -04:00
2025-04-23 11:14:15 -04:00
To use OAuth tokens for Git authentication over HTTPS:
1. Complete the OAuth authentication flow (**Login with GitHub**, **Login with GitLab** ).
1. Use HTTPS URLs when interacting with repositories (`https://github.com/organization/repo.git` ).
1. Coder automatically handles authentication. You can perform your Git operations as you normally would.
Behind the scenes, Coder:
- Stores your OAuth token securely in its database
- Sets up `GIT_ASKPASS` at `/tmp/coder.<random-string>/coder` in your workspaces
- Retrieves and injects the appropriate token when Git operations require authentication
2025-04-09 11:16:00 -04:00
To manually access these tokens within a workspace:
```shell
coder external-auth access-token <USER_DEFINED_ID>
```
### SSH Authentication
Coder automatically generates an SSH key pair for each user that can be used for Git operations.
When you use SSH URLs for Git repositories, for example, `git@github.com:organization/repo.git` , Coder checks for and uses an existing SSH key.
If one is not available, it uses the Coder-generated one.
The `coder gitssh` command wraps the standard `ssh` command and injects the SSH key during Git operations.
This works automatically when you:
1. Clone a repository using SSH URLs
1. Pull/push changes to remote repositories
1. Use any Git command that requires SSH authentication
You must add the SSH key to your Git provider.
#### Add your Coder SSH key to your Git provider
1. View your Coder Git SSH key:
```shell
coder publickey
` ``
1. Add the key to your Git provider accounts:
- [GitHub](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account#adding-a-new-ssh-key-to-your-account)
- [GitLab](https://docs.gitlab.com/user/ssh/#add-an-ssh-key-to-your-gitlab-account)
2025-01-07 15:10:16 -05:00
## Git-provider specific env variables
2023-11-09 16:14:22 -05:00
2025-01-07 15:10:16 -05:00
### Azure DevOps
2023-04-28 12:41:47 +01:00
Azure DevOps requires the following environment variables:
2023-08-23 12:27:57 +03:00
` ``env
2023-10-04 11:38:41 -05:00
CODER_EXTERNAL_AUTH_0_ID="primary-azure-devops"
CODER_EXTERNAL_AUTH_0_TYPE=azure-devops
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxxxxx
2023-04-28 12:41:47 +01:00
# Ensure this value is your "Client Secret", not "App Secret"
2023-10-04 11:38:41 -05:00
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxxxxxx
CODER_EXTERNAL_AUTH_0_AUTH_URL="https://app.vssps.visualstudio.com/oauth2/authorize"
CODER_EXTERNAL_AUTH_0_TOKEN_URL="https://app.vssps.visualstudio.com/oauth2/token"
2023-04-28 12:41:47 +01:00
` ``
2025-01-07 15:10:16 -05:00
### Azure DevOps (via Entra ID)
2024-03-04 18:12:46 +00:00
Azure DevOps (via Entra ID) requires the following environment variables:
` ``env
CODER_EXTERNAL_AUTH_0_ID="primary-azure-devops"
CODER_EXTERNAL_AUTH_0_TYPE=azure-devops-entra
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxxxxx
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxxxxxx
CODER_EXTERNAL_AUTH_0_AUTH_URL="https://login.microsoftonline.com/<TENANT ID>/oauth2/authorize"
` ``
2025-03-10 15:58:20 -05:00
> [!NOTE]
> Your app registration in Entra ID requires the ` vso.code_write` scope
2024-03-04 18:12:46 +00:00
2025-01-07 15:10:16 -05:00
### Bitbucket Server
Bitbucket Server requires the following environment variables:
` ``env
CODER_EXTERNAL_AUTH_0_ID="primary-bitbucket-server"
CODER_EXTERNAL_AUTH_0_TYPE=bitbucket-server
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxx
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxx
2025-04-01 16:03:25 -05:00
CODER_EXTERNAL_AUTH_0_AUTH_URL=https://bitbucket.example.com/rest/oauth2/latest/authorize
2025-01-07 15:10:16 -05:00
` ``
2025-04-01 16:03:25 -05:00
When configuring your Bitbucket OAuth application, set the redirect URI to
` https://example.com/external-auth/primary-bitbucket-server/callback`.
This callback path includes the value of ` CODER_EXTERNAL_AUTH_0_ID`.
2025-01-07 15:10:16 -05:00
### Gitea
` ``env
CODER_EXTERNAL_AUTH_0_ID="gitea"
CODER_EXTERNAL_AUTH_0_TYPE=gitea
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxxxxxx
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxxxxxx
# If self managed, set the Auth URL to your Gitea instance
CODER_EXTERNAL_AUTH_0_AUTH_URL="https://gitea.com/login/oauth/authorize"
` ``
2025-04-01 16:03:25 -05:00
The redirect URI for Gitea should be
` https://coder.example.com/external-auth/gitea/callback`.
2025-01-07 15:10:16 -05:00
### GitHub
2025-04-01 16:03:25 -05:00
Use this section as a reference for environment variables to customize your setup
or to integrate with an existing GitHub authentication.
For a more complete, step-by-step guide, follow the
[configure a GitHub OAuth app](#configure-a-github-oauth-app) section instead.
2025-01-07 15:10:16 -05:00
` ``env
2025-04-01 16:03:25 -05:00
CODER_EXTERNAL_AUTH_0_ID="primary-github"
2025-01-07 15:10:16 -05:00
CODER_EXTERNAL_AUTH_0_TYPE=github
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxxxxx
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxxxxxx
` ``
2025-04-01 16:03:25 -05:00
When configuring your GitHub OAuth application, set the
[authorization callback URL](https://docs.github.com/en/apps/creating-github-apps/registering-a-github-app/about-the-user-authorization-callback-url)
as ` https://example.com/external-auth/primary-github/callback`, where
` primary-github` matches your ` CODER_EXTERNAL_AUTH_0_ID` value.
2025-01-07 15:10:16 -05:00
### GitHub Enterprise
GitHub Enterprise requires the following environment variables:
` ``env
CODER_EXTERNAL_AUTH_0_ID="primary-github"
CODER_EXTERNAL_AUTH_0_TYPE=github
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxxxxx
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxxxxxx
CODER_EXTERNAL_AUTH_0_VALIDATE_URL="https://github.example.com/api/v3/user"
CODER_EXTERNAL_AUTH_0_AUTH_URL="https://github.example.com/login/oauth/authorize"
CODER_EXTERNAL_AUTH_0_TOKEN_URL="https://github.example.com/login/oauth/access_token"
` ``
2025-04-01 16:03:25 -05:00
When configuring your GitHub Enterprise OAuth application, set the
[authorization callback URL](https://docs.github.com/en/apps/creating-github-apps/registering-a-github-app/about-the-user-authorization-callback-url)
as ` https://example.com/external-auth/primary-github/callback`, where
` primary-github` matches your ` CODER_EXTERNAL_AUTH_0_ID` value.
2025-01-07 15:10:16 -05:00
### GitLab self-managed
2024-02-25 07:11:13 -08:00
GitLab self-managed requires the following environment variables:
` ``env
CODER_EXTERNAL_AUTH_0_ID="primary-gitlab"
CODER_EXTERNAL_AUTH_0_TYPE=gitlab
# This value is the "Application ID"
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxxxxx
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxxxxxx
2025-04-01 16:03:25 -05:00
CODER_EXTERNAL_AUTH_0_VALIDATE_URL="https://gitlab.example.com/oauth/token/info"
CODER_EXTERNAL_AUTH_0_AUTH_URL="https://gitlab.example.com/oauth/authorize"
CODER_EXTERNAL_AUTH_0_TOKEN_URL="https://gitlab.example.com/oauth/token"
CODER_EXTERNAL_AUTH_0_REGEX=gitlab\.example\.com
2024-02-25 07:11:13 -08:00
` ``
2025-04-01 16:03:25 -05:00
When [configuring your GitLab OAuth application](https://docs.gitlab.com/17.5/integration/oauth_provider/),
set the redirect URI to ` https://example.com/external-auth/primary-gitlab/callback`.
Note that the redirect URI must include the value of ` CODER_EXTERNAL_AUTH_0_ID` (in this example, ` primary-gitlab`).
2025-01-07 15:10:16 -05:00
### JFrog Artifactory
2024-02-26 12:35:18 -06:00
2025-06-16 13:18:55 -04:00
Visit the [JFrog Artifactory](../../admin/integrations/jfrog-artifactory.md) guide for instructions on how to set up for JFrog Artifactory.
2024-02-26 12:35:18 -06:00
2025-01-07 15:10:16 -05:00
## Self-managed Git providers
2022-11-07 13:04:00 -06:00
2023-08-23 12:27:57 +03:00
Custom authentication and token URLs should be used for self-managed Git
provider deployments.
2022-11-07 13:04:00 -06:00
2023-08-23 12:27:57 +03:00
` ``env
2023-10-04 11:38:41 -05:00
CODER_EXTERNAL_AUTH_0_AUTH_URL="https://github.example.com/oauth/authorize"
CODER_EXTERNAL_AUTH_0_TOKEN_URL="https://github.example.com/oauth/token"
2025-04-01 16:03:25 -05:00
CODER_EXTERNAL_AUTH_0_VALIDATE_URL="https://example.com/oauth/token/info"
CODER_EXTERNAL_AUTH_0_REGEX=github\.company\.com
2022-11-07 13:04:00 -06:00
` ``
2025-03-10 15:58:20 -05:00
> [!NOTE]
2025-04-01 16:03:25 -05:00
> The ` REGEX` variable must be set if using a custom Git domain.
2023-10-12 21:27:12 -07:00
2024-10-05 08:52:04 -07:00
## Custom scopes
2022-11-07 13:04:00 -06:00
Optionally, you can request custom scopes:
2023-08-23 12:27:57 +03:00
` ``env
2023-10-04 11:38:41 -05:00
CODER_EXTERNAL_AUTH_0_SCOPES="repo:read repo:write write:gpg_key"
2022-11-07 13:04:00 -06:00
` ``
2025-01-07 15:10:16 -05:00
## OAuth provider
### Configure a GitHub OAuth app
1. [Create a GitHub App](https://docs.github.com/en/apps/creating-github-apps/registering-a-github-app/registering-a-github-app)
2025-04-01 16:03:25 -05:00
- Set the authorization callback URL to
` https://coder.example.com/external-auth/primary-github/callback`, where ` primary-github`
is the value you set for ` CODER_EXTERNAL_AUTH_0_ID`.
2025-01-07 15:10:16 -05:00
- Deactivate Webhooks.
- Enable fine-grained access to specific repositories or a subset of
permissions for security.
2025-06-16 13:18:55 -04:00

2025-01-07 15:10:16 -05:00
1. Adjust the GitHub app permissions. You can use more or fewer permissions than
are listed here, this example allows users to clone
repositories:
2025-06-16 13:18:55 -04:00

2025-01-07 15:10:16 -05:00
| Name | Permission | Description |
|---------------|--------------|--------------------------------------------------------|
| Contents | Read & Write | Grants access to code and commit statuses. |
| Pull requests | Read & Write | Grants access to create and update pull requests. |
| Workflows | Read & Write | Grants access to update files in ` .github/workflows/`. |
| Metadata | Read-only | Grants access to metadata written by GitHub Apps. |
| Members | Read-only | Grants access to organization members and teams. |
1. Install the App for your organization. You may select a subset of
repositories to grant access to.
2025-06-16 13:18:55 -04:00

2025-01-07 15:10:16 -05:00
2025-04-02 16:51:57 -04:00
## Multiple External Providers (Premium)
2024-12-17 13:20:01 -05:00
Below is an example configuration with multiple providers:
2022-11-07 13:04:00 -06:00
2025-03-10 15:58:20 -05:00
> [!IMPORTANT]
> To support regex matching for paths like ` github\.com/org`, add the following ` git config` line to the [Coder agent startup script](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent#startup_script):
>
> ` ``shell
> git config --global credential.useHttpPath true
> ` ``
2025-01-03 14:13:46 -05:00
2023-08-23 12:27:57 +03:00
` ``env
2022-11-07 13:04:00 -06:00
# Provider 1) github.com
2023-10-04 11:38:41 -05:00
CODER_EXTERNAL_AUTH_0_ID=primary-github
CODER_EXTERNAL_AUTH_0_TYPE=github
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxxxxx
CODER_EXTERNAL_AUTH_0_CLIENT_SECRET=xxxxxxx
2024-11-03 10:01:06 -06:00
CODER_EXTERNAL_AUTH_0_REGEX=github\.com/org
2022-11-07 13:04:00 -06:00
# Provider 2) github.example.com
2023-10-04 11:38:41 -05:00
CODER_EXTERNAL_AUTH_1_ID=secondary-github
CODER_EXTERNAL_AUTH_1_TYPE=github
CODER_EXTERNAL_AUTH_1_CLIENT_ID=xxxxxx
CODER_EXTERNAL_AUTH_1_CLIENT_SECRET=xxxxxxx
2024-11-03 10:01:06 -06:00
CODER_EXTERNAL_AUTH_1_REGEX=github\.example\.com
2023-10-04 11:38:41 -05:00
CODER_EXTERNAL_AUTH_1_AUTH_URL="https://github.example.com/login/oauth/authorize"
CODER_EXTERNAL_AUTH_1_TOKEN_URL="https://github.example.com/login/oauth/access_token"
2023-10-04 13:49:40 -05:00
CODER_EXTERNAL_AUTH_1_VALIDATE_URL="https://github.example.com/api/v3/user"
2022-11-07 13:04:00 -06:00
` ``