Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
812 changes: 797 additions & 15 deletions pkgs/idp-provider/Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkgs/idp-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ rsa = "0.9"
pkcs1 = { version = "0.7", features = ["alloc"] }
pkcs8 = { version = "0.10", features = ["encryption", "pem"] }
rusqlite = { version = "0.42", features = ["bundled"] }
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "json"] }

[dev-dependencies]
tower = { version = "0.5", features = ["util"] }
Expand Down
54 changes: 54 additions & 0 deletions pkgs/idp-provider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ SSO never rebinds an id key (#2060).
| `IDP_PROVIDER_OIDC_KEY` | _(unset)_ | Path to the encrypted Slice 2 OIDC private key. Required when `IDP_PROVIDER_CONFIG` is set. |
| `IDP_CA_PASSPHRASE` | _(unset)_ | Passphrase for the signing key, in process memory only (vault name `idp.ca-passphrase`). Required when `IDP_PROVIDER_CONFIG` is set. Never logged, never written to disk by the provider. |
| `IDP_PROVIDER_AUDIT_DB` | _(registry config's `audit_db_path`, else in-memory)_ | Append-only audit SQLite path override. |
| `IDP_PROVIDER_IDENTITY_DB` | _(registry config's `identity_db_path`, else in-memory)_ | Identity store (#13415) SQLite path override. |
| `IDP_GITHUB_CLIENT_SECRET` / `IDP_GOOGLE_CLIENT_SECRET` / `IDP_MICROSOFT_CLIENT_SECRET` | _(unset)_ | Federated-door client secrets, in process memory only (vault names `idp.github.client-secret` / `idp.google.client-secret` / `idp.microsoft.client-secret`). A door whose secret is unset stays disabled. |

## Endpoints

Expand All @@ -73,6 +75,58 @@ SSO never rebinds an id key (#2060).
- `POST /token` — `grant_type=authorization_code` exchange with
PKCE verification; returns short-lived ID + access tokens.
- `GET /userinfo` — Bearer access token in, account claims out.
- `GET /door/{name}/start` — federated door entry: re-validates the authorize request, parks it, redirects upstream.
- `GET /door/{name}/callback` — upstream return: verifies the upstream identity, enforces the allowlist and link state, mints our code, redirects to the relying party.

## Federated doors (#13411 / #13412 / #13414)

Configured doors appear on the login form as "Sign in with …"
links to `GET /door/{name}/start`, which re-validates the relying
party's authorize request, parks it server-side under a random
single-use state handle (300 s), and redirects to the upstream.
`GET /door/{name}/callback` consumes the handle, exchanges the
upstream code, and verifies the upstream identity:

- **OIDC doors** (Google, Microsoft): the upstream ID token's
signature is verified against the upstream JWKS, and `iss` /
`aud` / `exp` / `nonce` are checked. The recorded subject is the
configured claim (`sub`; Microsoft records `oid`, falling back
to `sub`). The provider also sends PKCE (S256) upstream.
- **GitHub door**: the code is exchanged and the user document is
fetched; the subject is the numeric user `id`.

The deny-by-default allowlist is then enforced in the provider,
exactly as for the local door, and a code of *our own* is minted
into the same code store — a door login's tokens are
indistinguishable from a local login's (same account `sub`, same
role claims). Door client secrets are never in the config file:
each door names an environment variable (`client_secret_env`) the
launcher fills from the vault by name (`idp.github.client-secret`
→ `IDP_GITHUB_CLIENT_SECRET`, `idp.google.client-secret` →
`IDP_GOOGLE_CLIENT_SECRET`, `idp.microsoft.client-secret` →
`IDP_MICROSOFT_CLIENT_SECRET`). A door whose secret is unset — or
an OIDC door without an issuer/JWKS configured — is disabled: its
start endpoint denies with an audit record and the rest of the
provider is unaffected.

## Account linking (#13415)

Link state lives in a runtime identity store (SQLite,
`identity_db_path`), not in the config: the config's
`account.identities` list is only the seed. The store keeps one
row per `(provider, subject)` with `active | unlinked` status and
an append-only event history (`seed` / `link` / `relink` /
`unlink`, trigger-protected like the audit table). Rows are never
hard-deleted while the account lives.

- An allowlisted identity arriving at a door for the first time is
**linked by that login** (actor `door-login`); the event lands in
the identity history *and* the audit record.
- An operator unlinks with `idp-provider unlink <provider>
<subject>` (relink: `link`; inspect: `links`). An unlinked
identity's door login is denied (`identity_unlinked`, audited) —
a login never silently undoes an unlink — while the account
stays reachable via the other doors.

## kid convention

Expand Down
2 changes: 1 addition & 1 deletion pkgs/idp-provider/config.example.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"_comment":"Example provider registry config for pkgs/idp-provider (Slice 3, #13409). The REAL config lives on-host with the provider (point IDP_PROVIDER_CONFIG at it) and is never committed: it carries the owner's credential HASH. Generate the hash on-host with `IDP_LOCAL_CREDENTIAL=... idp-provider hash-credential`. An empty hash verifies nothing, so this example cannot open the door.","account":{"account_id":"drew","display_name":"Drew","identities":[{"provider":"devtoday-local","subject":"drew"}],"local_credential_hash":"","roles":["owner","orchestrator","agent","read-only"]},"allowlist":["devtoday-local:drew"],"audit_db_path":"/home/user/idp-provider/audit.sqlite3","clients":[{"client_id":"test-rp","name":"Test relying party (Slice 3 loop proof)","redirect_uris":["http://127.0.0.1:8912/callback"]},{"client_id":"devtoday-local-door","name":"devtoday local door client","redirect_uris":["http://127.0.0.1:8913/callback"]}]}
{"_comment":"Example provider registry config for pkgs/idp-provider (Slice 3, #13409; doors #13411/#13412/#13414; linking #13415). The REAL config lives on-host with the provider (point IDP_PROVIDER_CONFIG at it) and is never committed: it carries the owner's credential HASH. Generate the hash on-host with `IDP_LOCAL_CREDENTIAL=... idp-provider hash-credential`. An empty hash verifies nothing, so this example cannot open the door. Door client ids below are placeholders; door client SECRETS are never in this file — each door names the environment variable the launcher fills from the vault by name (idp.github.client-secret -> IDP_GITHUB_CLIENT_SECRET, idp.google.client-secret -> IDP_GOOGLE_CLIENT_SECRET, idp.microsoft.client-secret -> IDP_MICROSOFT_CLIENT_SECRET). A door whose secret env is unset stays disabled. Door endpoints and the Microsoft issuer (the consumers tenant id segment) must be checked against the actual app registrations when they are created. The account.identities list is the identity store's seed; link state after first boot lives in identity_db_path.","account":{"account_id":"drew","display_name":"Drew","identities":[{"provider":"devtoday-local","subject":"drew"}],"local_credential_hash":"","roles":["owner","orchestrator","agent","read-only"]},"allowlist":["devtoday-local:drew"],"audit_db_path":"/home/user/idp-provider/audit.sqlite3","clients":[{"client_id":"test-rp","name":"Test relying party (Slice 3 loop proof)","redirect_uris":["http://127.0.0.1:8912/callback"]},{"client_id":"devtoday-local-door","name":"devtoday local door client","redirect_uris":["http://127.0.0.1:8913/callback"]}],"doors":[{"authorize_url":"https://github.com/login/oauth/authorize","client_id":"REPLACE_WITH_GITHUB_OAUTH_APP_CLIENT_ID","client_secret_env":"IDP_GITHUB_CLIENT_SECRET","display_name":"GitHub","kind":"github","name":"github","scopes":["read:user"],"token_url":"https://github.com/login/oauth/access_token","userinfo_url":"https://api.github.com/user"},{"authorize_url":"https://accounts.google.com/o/oauth2/v2/auth","client_id":"REPLACE_WITH_GOOGLE_OAUTH_CLIENT_ID","client_secret_env":"IDP_GOOGLE_CLIENT_SECRET","display_name":"Google","issuer":"https://accounts.google.com","jwks_uri":"https://www.googleapis.com/oauth2/v3/certs","kind":"oidc","name":"google","scopes":["openid"],"subject_claim":"sub","token_url":"https://oauth2.googleapis.com/token"},{"authorize_url":"https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize","client_id":"REPLACE_WITH_MICROSOFT_APP_CLIENT_ID","client_secret_env":"IDP_MICROSOFT_CLIENT_SECRET","display_name":"Microsoft","issuer":"https://login.microsoftonline.com/9188040d-6c67-4c5b-b112-36a304b66dad/v2.0","jwks_uri":"https://login.microsoftonline.com/consumers/discovery/v2.0/keys","kind":"oidc","name":"microsoft","scopes":["openid"],"subject_claim":"oid","token_url":"https://login.microsoftonline.com/consumers/oauth2/v2.0/token"}],"identity_db_path":"/home/user/idp-provider/identities.sqlite3"}
44 changes: 32 additions & 12 deletions pkgs/idp-provider/examples/test-rp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ fn http(
Some(i) => (&rest[..i], &rest[i..]),
None => (rest, "/"),
};
let mut stream = TcpStream::connect(authority).map_err(|e| format!("connect {authority}: {e}"))?;
let mut stream =
TcpStream::connect(authority).map_err(|e| format!("connect {authority}: {e}"))?;
let mut req = format!("{method} {path} HTTP/1.1\r\nhost: {authority}\r\nconnection: close\r\n");
if let Some(b) = bearer {
req.push_str(&format!("authorization: Bearer {b}\r\n"));
Expand Down Expand Up @@ -113,7 +114,11 @@ fn http(
chunked = true;
}
}
let body = if chunked { dechunk(body) } else { body.to_string() };
let body = if chunked {
dechunk(body)
} else {
body.to_string()
};
Ok((status, location, body))
}

Expand Down Expand Up @@ -153,12 +158,18 @@ fn main() {
};

// 1. Discovery.
let (status, _l, body) = http("GET", &format!("{issuer}/.well-known/openid-configuration"), None, None)
.unwrap_or_else(|e| run(&e));
let (status, _l, body) = http(
"GET",
&format!("{issuer}/.well-known/openid-configuration"),
None,
None,
)
.unwrap_or_else(|e| run(&e));
if status != 200 {
run(&format!("discovery status {status}"));
}
let discovery: serde_json::Value = serde_json::from_str(&body).unwrap_or_else(|_| run("discovery is not JSON"));
let discovery: serde_json::Value =
serde_json::from_str(&body).unwrap_or_else(|_| run("discovery is not JSON"));
if discovery["issuer"] != issuer {
run("discovery issuer mismatch");
}
Expand All @@ -172,7 +183,8 @@ fn main() {
if status != 200 {
run(&format!("jwks status {status}"));
}
let jwks: serde_json::Value = serde_json::from_str(&body).unwrap_or_else(|_| run("jwks is not JSON"));
let jwks: serde_json::Value =
serde_json::from_str(&body).unwrap_or_else(|_| run("jwks is not JSON"));
let jwk = &jwks["keys"][0];
let kid = jwk["kid"].as_str().unwrap_or("").to_string();
let decode = |s: &str| B64URL.decode(s).unwrap_or_default();
Expand All @@ -197,7 +209,8 @@ fn main() {
("login_id", &login_id),
("credential", &credential),
]);
let (status, location, _body) = http("POST", authz_ep, Some(&form), None).unwrap_or_else(|e| run(&e));
let (status, location, _body) =
http("POST", authz_ep, Some(&form), None).unwrap_or_else(|e| run(&e));
if mode == "deny" {
// The proof's negative case: refusal is the success here.
println!(
Expand Down Expand Up @@ -236,9 +249,14 @@ fn main() {
if status != 200 {
run(&format!("token status {status}: {body}"));
}
let tokens: serde_json::Value = serde_json::from_str(&body).unwrap_or_else(|_| run("token response is not JSON"));
let id_token = tokens["id_token"].as_str().unwrap_or_else(|| run("no id_token"));
let access_token = tokens["access_token"].as_str().unwrap_or_else(|| run("no access_token"));
let tokens: serde_json::Value =
serde_json::from_str(&body).unwrap_or_else(|_| run("token response is not JSON"));
let id_token = tokens["id_token"]
.as_str()
.unwrap_or_else(|| run("no id_token"));
let access_token = tokens["access_token"]
.as_str()
.unwrap_or_else(|| run("no access_token"));

// 5. Verify the ID token against the JWKS key.
let parts: Vec<&str> = id_token.split('.').collect();
Expand All @@ -262,11 +280,13 @@ fn main() {
}

// 6. Userinfo with the access token.
let (status, _l, body) = http("GET", userinfo_ep, None, Some(access_token)).unwrap_or_else(|e| run(&e));
let (status, _l, body) =
http("GET", userinfo_ep, None, Some(access_token)).unwrap_or_else(|e| run(&e));
if status != 200 {
run(&format!("userinfo status {status}"));
}
let userinfo: serde_json::Value = serde_json::from_str(&body).unwrap_or_else(|_| run("userinfo is not JSON"));
let userinfo: serde_json::Value =
serde_json::from_str(&body).unwrap_or_else(|_| run("userinfo is not JSON"));
if userinfo["sub"] != claims["sub"] {
run("userinfo sub does not match id_token sub");
}
Expand Down
Loading