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 }