Files
coder/flake.nix
Thomas Kosiewski 6f2834f62a feat: oauth2 - add authorization server metadata endpoint and PKCE support (#18548)
## Summary

  This PR implements critical MCP OAuth2 compliance features for Coder's authorization server, adding PKCE support, resource parameter handling, and OAuth2 server metadata discovery. This brings Coder's OAuth2 implementation significantly closer to production readiness for MCP (Model Context Protocol)
  integrations.

  ## What's Added

  ### OAuth2 Authorization Server Metadata (RFC 8414)
  - Add `/.well-known/oauth-authorization-server` endpoint for automatic client discovery
  - Returns standardized metadata including supported grant types, response types, and PKCE methods
  - Essential for MCP client compatibility and OAuth2 standards compliance

  ### PKCE Support (RFC 7636)
  - Implement Proof Key for Code Exchange with S256 challenge method
  - Add `code_challenge` and `code_challenge_method` parameters to authorization flow
  - Add `code_verifier` validation in token exchange
  - Provides enhanced security for public clients (mobile apps, CLIs)

  ### Resource Parameter Support (RFC 8707)
  - Add `resource` parameter to authorization and token endpoints
  - Store resource URI and bind tokens to specific audiences
  - Critical for MCP's resource-bound token model

  ### Enhanced OAuth2 Error Handling
  - Add OAuth2-compliant error responses with proper error codes
  - Use standard error format: `{"error": "code", "error_description": "details"}`
  - Improve error consistency across OAuth2 endpoints

  ### Authorization UI Improvements
  - Fix authorization flow to use POST-based consent instead of GET redirects
  - Remove dependency on referer headers for security decisions
  - Improve CSRF protection with proper state parameter validation

  ## Why This Matters

  **For MCP Integration:** MCP requires OAuth2 authorization servers to support PKCE, resource parameters, and metadata discovery. Without these features, MCP clients cannot securely authenticate with Coder.

  **For Security:** PKCE prevents authorization code interception attacks, especially critical for public clients. Resource binding ensures tokens are only valid for intended services.

  **For Standards Compliance:** These are widely adopted OAuth2 extensions that improve interoperability with modern OAuth2 clients.

  ## Database Changes

  - **Migration 000343:** Adds `code_challenge`, `code_challenge_method`, `resource_uri` to `oauth2_provider_app_codes`
  - **Migration 000343:** Adds `audience` field to `oauth2_provider_app_tokens` for resource binding
  - **Audit Updates:** New OAuth2 fields properly tracked in audit system
  - **Backward Compatibility:** All changes maintain compatibility with existing OAuth2 flows

  ## Test Coverage

  - Comprehensive PKCE test suite in `coderd/identityprovider/pkce_test.go`
  - OAuth2 metadata endpoint tests in `coderd/oauth2_metadata_test.go`
  - Integration tests covering PKCE + resource parameter combinations
  - Negative tests for invalid PKCE verifiers and malformed requests

  ## Testing Instructions

  ```bash
  # Run the comprehensive OAuth2 test suite
  ./scripts/oauth2/test-mcp-oauth2.sh

  Manual Testing with Interactive Server

  # Start Coder in development mode
  ./scripts/develop.sh

  # In another terminal, set up test app and run interactive flow
  eval $(./scripts/oauth2/setup-test-app.sh)
  ./scripts/oauth2/test-manual-flow.sh
  # Opens browser with OAuth2 flow, handles callback automatically

  # Clean up when done
  ./scripts/oauth2/cleanup-test-app.sh

  Individual Component Testing

  # Test metadata endpoint
  curl -s http://localhost:3000/.well-known/oauth-authorization-server | jq .

  # Test PKCE generation
  ./scripts/oauth2/generate-pkce.sh

  # Run specific test suites
  go test -v ./coderd/identityprovider -run TestVerifyPKCE
  go test -v ./coderd -run TestOAuth2AuthorizationServerMetadata
```

  ### Breaking Changes

  None. All changes maintain backward compatibility with existing OAuth2 flows.

---

Change-Id: Ifbd0d9a543d545f9f56ecaa77ff2238542ff954a
Signed-off-by: Thomas Kosiewski <tk@coder.com>
2025-07-01 15:39:29 +02:00

321 lines
10 KiB
Nix
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
description = "Development environments on your infrastructure";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11";
nixpkgs-unstable.url = "github:nixos/nixpkgs/nixos-unstable";
nixpkgs-pinned.url = "github:nixos/nixpkgs/5deee6281831847857720668867729617629ef1f";
flake-utils.url = "github:numtide/flake-utils";
pnpm2nix = {
url = "github:ThomasK33/pnpm2nix-nzbr";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
drpc = {
url = "github:storj/drpc/v0.0.34";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
};
outputs =
{
self,
nixpkgs,
nixpkgs-pinned,
nixpkgs-unstable,
flake-utils,
drpc,
pnpm2nix,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
# Workaround for: google-chrome has an unfree license (unfree), refusing to evaluate.
config.allowUnfree = true;
};
# pinnedPkgs is used to pin packages that need to stay in sync with CI.
# Everything else uses unstable.
pinnedPkgs = import nixpkgs-pinned {
inherit system;
};
unstablePkgs = import nixpkgs-unstable {
inherit system;
# Workaround for: terraform has an unfree license (bsl11), refusing to evaluate.
config.allowUnfreePredicate =
pkg:
builtins.elem (pkgs.lib.getName pkg) [
"terraform"
];
};
formatter = pkgs.nixfmt-rfc-style;
nodejs = pkgs.nodejs_20;
pnpm = pkgs.pnpm_9.override {
inherit nodejs; # Ensure it points to the above nodejs version
};
# Check in https://search.nixos.org/packages to find new packages.
# Use `nix --extra-experimental-features nix-command --extra-experimental-features flakes flake update`
# to update the lock file if packages are out-of-date.
# From https://nixos.wiki/wiki/Google_Cloud_SDK
gdk = pkgs.google-cloud-sdk.withExtraComponents [
pkgs.google-cloud-sdk.components.gke-gcloud-auth-plugin
];
proto_gen_go_1_30 = pkgs.buildGoModule rec {
name = "protoc-gen-go";
owner = "protocolbuffers";
repo = "protobuf-go";
rev = "v1.30.0";
src = pkgs.fetchFromGitHub {
inherit owner repo rev;
# Updated with ./scripts/update-flake.sh`.
sha256 = "sha256-GTZQ40uoi62Im2F4YvlZWiSNNJ4fEAkRojYa0EYz9HU=";
};
subPackages = [ "cmd/protoc-gen-go" ];
vendorHash = null;
};
# Packages required to build the frontend
frontendPackages =
with pkgs;
[
cairo
pango
pixman
libpng
libjpeg
giflib
librsvg
python312Packages.setuptools # Needed for node-gyp
]
++ (lib.optionals stdenv.targetPlatform.isDarwin [
darwin.apple_sdk.frameworks.Foundation
xcbuild
]);
# The minimal set of packages to build Coder.
devShellPackages =
with pkgs;
[
# google-chrome is not available on aarch64 linux
(lib.optionalDrvAttr (!stdenv.isLinux || !stdenv.isAarch64) google-chrome)
# strace is not available on OSX
(lib.optionalDrvAttr (!pkgs.stdenv.isDarwin) strace)
bat
cairo
curl
cosign
delve
dive
drpc.defaultPackage.${system}
formatter
fzf
gawk
gcc13
gdk
getopt
gh
git
git-lfs
(lib.optionalDrvAttr stdenv.isLinux glibcLocales)
gnumake
gnused
gnugrep
gnutar
unstablePkgs.go_1_24
gofumpt
go-migrate
(pinnedPkgs.golangci-lint)
gopls
gotestsum
hadolint
jq
kubectl
kubectx
kubernetes-helm
lazydocker
lazygit
less
mockgen
moreutils
neovim
nfpm
nix-prefetch-git
nodejs
openssh
openssl
pango
pixman
pkg-config
playwright-driver.browsers
pnpm
postgresql_16
proto_gen_go_1_30
protobuf_23
ripgrep
shellcheck
(pinnedPkgs.shfmt)
sqlc
syft
unstablePkgs.terraform
typos
which
# Needed for many LD system libs!
(lib.optional stdenv.isLinux util-linux)
vim
wget
yq-go
zip
zsh
zstd
]
++ frontendPackages;
docker = pkgs.callPackage ./nix/docker.nix { };
# buildSite packages the site directory.
buildSite = pnpm2nix.packages.${system}.mkPnpmPackage {
inherit nodejs pnpm;
src = ./site/.;
# Required for the `canvas` package!
extraBuildInputs = frontendPackages;
installInPlace = true;
distDir = "out";
};
version = "v0.0.0-nix-${self.shortRev or self.dirtyShortRev}";
# To make faster subsequent builds, you could extract the `.zst`
# slim bundle into it's own derivation.
buildFat =
osArch:
unstablePkgs.buildGo124Module {
name = "coder-${osArch}";
# Updated with ./scripts/update-flake.sh`.
# This should be updated whenever go.mod changes!
vendorHash = "sha256-6sdvX0Wglj0CZiig2VD45JzuTcxwg7yrGoPPQUYvuqU=";
proxyVendor = true;
src = ./.;
nativeBuildInputs = with pkgs; [
getopt
openssl
zstd
];
preBuild = ''
# Replaces /usr/bin/env with an absolute path to the interpreter.
patchShebangs ./scripts
'';
buildPhase = ''
runHook preBuild
# Unpack the site contents.
mkdir -p ./site/out ./site/node_modules/
cp -r ${buildSite.out}/* ./site/out
touch ./site/node_modules/.installed
# Build and copy the binary!
export CODER_FORCE_VERSION=${version}
# Flagging 'site/node_modules/.installed' as an old file,
# as we do not want to trigger codegen during a build.
make -j -o 'site/node_modules/.installed' build/coder_${osArch}
'';
installPhase = ''
mkdir -p $out/bin
cp -r ./build/coder_${osArch} $out/bin/coder
'';
};
in
# "Keep in mind that you need to use the same version of playwright in your node playwright project as in your nixpkgs, or else playwright will try to use browsers versions that aren't installed!"
# - https://nixos.wiki/wiki/Playwright
assert pkgs.lib.assertMsg
(
(pkgs.lib.importJSON ./site/package.json).devDependencies."@playwright/test"
== pkgs.playwright-driver.version
)
"There is a mismatch between the playwright versions in the ./nix.flake and the ./site/package.json file. Please make sure that they use the exact same version.";
rec {
inherit formatter;
devShells = {
default = pkgs.mkShell {
buildInputs = devShellPackages;
PLAYWRIGHT_BROWSERS_PATH = pkgs.playwright-driver.browsers;
PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS = true;
LOCALE_ARCHIVE =
with pkgs;
lib.optionalDrvAttr stdenv.isLinux "${glibcLocales}/lib/locale/locale-archive";
NODE_OPTIONS = "--max-old-space-size=8192";
GOPRIVATE = "coder.com,cdr.dev,go.coder.com,github.com/cdr,github.com/coder";
};
};
packages =
{
default = packages.${system};
proto_gen_go = proto_gen_go_1_30;
site = buildSite;
# Copying `OS_ARCHES` from the Makefile.
x86_64-linux = buildFat "linux_amd64";
aarch64-linux = buildFat "linux_arm64";
x86_64-darwin = buildFat "darwin_amd64";
aarch64-darwin = buildFat "darwin_arm64";
x86_64-windows = buildFat "windows_amd64.exe";
aarch64-windows = buildFat "windows_arm64.exe";
}
// (pkgs.lib.optionalAttrs pkgs.stdenv.isLinux {
dev_image = docker.buildNixShellImage rec {
name = "codercom/oss-dogfood-nix";
tag = "latest-${system}";
# (ThomasK33): Workaround for images with too many layers (>64 layers) causing sysbox
# to have issues on dogfood envs.
maxLayers = 32;
uname = "coder";
homeDirectory = "/home/${uname}";
releaseName = version;
drv = devShells.default.overrideAttrs (oldAttrs: {
buildInputs =
(with pkgs; [
coreutils
nix.out
curl.bin # Ensure the actual curl binary is included in the PATH
glibc.bin # Ensure the glibc binaries are included in the PATH
jq.bin
binutils # ld and strings
filebrowser # Ensure that we're not redownloading filebrowser on each launch
systemd.out
service-wrapper
docker_26
shadow.out
su
ncurses.out # clear
unzip
zip
gzip
procps # free
])
++ oldAttrs.buildInputs;
});
};
});
}
);
}