diff --git a/README.md b/README.md index a8fb02b..b6a05c8 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Today it holds the Atlassian clients that `yareco`, `yabaratla` and | `atlassian/jira` | Jira REST API client (Cloud v3 and Server/Data Center v2) | | `atlassian/confluence` | small Confluence REST API client (classic content API) | | `atlassian/internal/transport` | the HTTP machinery both share; not importable from outside | -| `yaiam` | sign a command-line program in at yaiam (device flow, RFC 8628) and keep its tokens (YAIAM-710, 2026-11-09); sign a web application's users in (code flow with PKCE) and read users and groups from yaiam's admin API (2026-11-17); trade a user's token for one at another service, for an agent (token exchange, RFC 8693, 2026-11-21); refresh and revoke a web sign-in's tokens (2026-11-28) | +| `yaiam` | sign a command-line program in at yaiam (device flow, RFC 8628) and keep its tokens (YAIAM-710, 2026-11-09); sign a web application's users in (code flow with PKCE) and read users and groups from yaiam's admin API (2026-11-17); trade a user's token for one at another service, for an agent (token exchange, RFC 8693, 2026-11-21); refresh and revoke a web sign-in's tokens (2026-11-28); fetch an SSH certificate for a public key (ADR-0046, 2026-11-29) | Standard library only, no third-party dependencies. @@ -193,6 +193,23 @@ An example web application is yaiam's `examples/webapp`; an end-to-end test against a real yaiam-authd is yaiam's `internal/oidcprovider/weblogin_test.go`. +### SSH certificates + +A tenant of yaiam can be an SSH certificate authority (yaiam ADR-0046). +Sign the user in with the scope `ssh` (the client needs it and the +audience `yaiam.SSHAudience(issuer)` in yaiam), then trade the access +token and a public key for a short-lived OpenSSH certificate: + +```go +c := &yaiam.Client{Issuer: issuer, ClientID: id, Scopes: []string{"openid", yaiam.SSHScope}} +tok, err := c.Login(ctx, store, show) // or StartDevice/WaitDevice +cert, err := c.SSHCertificate(ctx, tok, publicKeyLine) +keys, err := c.SSHCAKeys(ctx) // what servers trust +``` + +Only the public key is sent. yaseva (`yaseva ssh login`) is the client +that keeps the private key in its vault. + ### Acting for a user: token exchange A confidential client that holds a user's access token -- an diff --git a/yaiam/ssh.go b/yaiam/ssh.go new file mode 100644 index 0000000..d6de38f --- /dev/null +++ b/yaiam/ssh.go @@ -0,0 +1,106 @@ +package yaiam + +// SSH certificates (yaiam ADR-0046): a tenant of yaiam is an SSH +// certificate authority. A program signs its user in with the device +// flow -- Client.Scopes with "ssh"; the client needs that scope and the +// audience SSHAudience in yaiam -- and trades the access token and a +// public key for a short-lived OpenSSH user certificate. Only the public +// key is sent; the private one stays with the program. + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "net/http" + "strings" + "time" +) + +// SSHScope is the scope a token needs for SSH certificates. +const SSHScope = "ssh" + +// SSHAudience is the audience yaiam wants in a token for the SSH +// certificates of the tenant whose issuer is issuer +// ("https://login.example.org/t/default" -> ".../t/default/ssh"). +func SSHAudience(issuer string) string { return strings.TrimSuffix(issuer, "/") + "/ssh" } + +// SSHCertificate is a certificate yaiam signed. +type SSHCertificate struct { + // Certificate is the certificate in authorized_keys format, what an + // ssh-agent or a -cert.pub file takes. + Certificate string `json:"certificate"` + Serial string `json:"serial"` + Principals []string `json:"principals"` + ValidAfter time.Time `json:"valid_after"` + ValidBefore time.Time `json:"valid_before"` +} + +// SSHCertificate asks for a certificate for publicKey (authorized_keys +// format) with the access token t. A refusal is an *Error: +// invalid_token (401: sign in again), access_denied (403: no scope ssh, +// the client's access rule, or a sign-in without the second factor the +// tenant asks for), not_available (404: the tenant issues no +// certificates), invalid_request (400: a key yaiam does not certify). +func (c *Client) SSHCertificate(ctx context.Context, t *Token, publicKey string) (*SSHCertificate, error) { + if t == nil || t.AccessToken == "" { + return nil, ErrLoginRequired + } + if publicKey == "" { + return nil, errors.New("yaiam: ssh certificate: no public key") + } + if err := checkURL("issuer", c.Issuer, c.AllowInsecure); err != nil { + return nil, err + } + body, err := json.Marshal(map[string]string{"public_key": strings.TrimSpace(publicKey)}) + if err != nil { + return nil, err + } + req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimSuffix(c.Issuer, "/")+"/ssh/certificate", bytes.NewReader(body)) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + req.Header.Set("Authorization", "Bearer "+t.AccessToken) + var out SSHCertificate + if err := c.send("ssh certificate", req, &out); err != nil { + return nil, err + } + if out.Certificate == "" || !out.ValidBefore.After(out.ValidAfter) { + return nil, errors.New("yaiam: ssh certificate: incomplete answer") + } + return &out, nil +} + +// SSHCAKeys are the public keys servers of the tenant trust +// (TrustedUserCAKeys), one authorized_keys line each; none while the +// tenant issues no certificates. +func (c *Client) SSHCAKeys(ctx context.Context) ([]string, error) { + if err := checkURL("issuer", c.Issuer, c.AllowInsecure); err != nil { + return nil, err + } + req, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.TrimSuffix(c.Issuer, "/")+"/ssh/ca.pub", nil) + if err != nil { + return nil, err + } + status, body, err := c.do(req) + if err != nil { + return nil, fmt.Errorf("yaiam: ssh ca.pub: %w", err) + } + switch status { + case http.StatusOK: + case http.StatusNotFound: + return nil, nil + default: + return nil, &Error{Endpoint: "ssh ca.pub", Status: status} + } + var out []string + for line := range strings.SplitSeq(string(body), "\n") { + if line = strings.TrimSpace(line); line != "" { + out = append(out, line) + } + } + return out, nil +} diff --git a/yaiam/ssh_test.go b/yaiam/ssh_test.go new file mode 100644 index 0000000..1972817 --- /dev/null +++ b/yaiam/ssh_test.go @@ -0,0 +1,86 @@ +package yaiam + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +// TestSSHCertificate: the public key and the bearer token go to +// /ssh/certificate, the answer comes back; a refusal is an +// *Error with yaiam's code; no token is ErrLoginRequired; ca.pub lists +// the CA keys, none on 404; a plain-http issuer elsewhere than loopback +// is refused before anything is sent. +func TestSSHCertificate(t *testing.T) { + var gotAuth, gotKey string + refuse := "" + caKeys := "ssh-ed25519 AAAA-one\nssh-ed25519 AAAA-two\n" + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPost && r.URL.Path == "/t/default/ssh/certificate": + gotAuth = r.Header.Get("Authorization") + var in map[string]string + json.NewDecoder(r.Body).Decode(&in) + gotKey = in["public_key"] + if refuse != "" { + w.WriteHeader(http.StatusForbidden) + json.NewEncoder(w).Encode(map[string]string{"error": refuse, "error_description": "no SSH certificate for this token"}) + return + } + now := time.Now().UTC() + json.NewEncoder(w).Encode(map[string]any{"certificate": "ssh-ed25519-cert-v01@openssh.com AAAA", "serial": "42", + "principals": []string{"user:alice"}, "valid_after": now.Add(-5 * time.Minute), "valid_before": now.Add(8 * time.Hour)}) + case r.Method == http.MethodGet && r.URL.Path == "/t/default/ssh/ca.pub": + if caKeys == "" { + http.NotFound(w, r) + return + } + w.Write([]byte(caKeys)) + default: + http.NotFound(w, r) + } + })) + defer ts.Close() + c := &Client{Issuer: ts.URL + "/t/default", ClientID: "ssh"} + ctx := context.Background() + tok := &Token{AccessToken: "the-token"} + + cert, err := c.SSHCertificate(ctx, tok, " ssh-ed25519 AAAA-user alice@laptop\n") + if err != nil { + t.Fatal(err) + } + if gotAuth != "Bearer the-token" || gotKey != "ssh-ed25519 AAAA-user alice@laptop" || cert.Serial != "42" || + len(cert.Principals) != 1 || !strings.HasPrefix(cert.Certificate, "ssh-ed25519-cert") { + t.Errorf("sent %q %q, got %+v", gotAuth, gotKey, cert) + } + refuse = "access_denied" + var e *Error + if _, err := c.SSHCertificate(ctx, tok, "ssh-ed25519 AAAA"); !errors.As(err, &e) || e.Code != "access_denied" || e.Status != http.StatusForbidden { + t.Errorf("refused: %v", err) + } + if _, err := c.SSHCertificate(ctx, nil, "ssh-ed25519 AAAA"); !errors.Is(err, ErrLoginRequired) { + t.Errorf("no token: %v", err) + } + if SSHAudience(c.Issuer) != ts.URL+"/t/default/ssh" || SSHAudience(c.Issuer+"/") != ts.URL+"/t/default/ssh" { + t.Errorf("audience = %q", SSHAudience(c.Issuer)) + } + + keys, err := c.SSHCAKeys(ctx) + if err != nil || len(keys) != 2 || keys[1] != "ssh-ed25519 AAAA-two" { + t.Errorf("CA keys = %v, %v", keys, err) + } + caKeys = "" + if keys, err := c.SSHCAKeys(ctx); err != nil || keys != nil { + t.Errorf("no CA = %v, %v", keys, err) + } + + far := &Client{Issuer: "http://login.example.org/t/default", ClientID: "ssh"} + if _, err := far.SSHCertificate(ctx, tok, "ssh-ed25519 AAAA"); err == nil || !strings.Contains(err.Error(), "not https") { + t.Errorf("plain http elsewhere: %v", err) + } +}