auth.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package auth
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/rand"
  6. "encoding/base64"
  7. "fmt"
  8. "io"
  9. "log/slog"
  10. "os"
  11. "path/filepath"
  12. "golang.org/x/crypto/ssh"
  13. )
  14. const defaultPrivateKey = "id_ed25519"
  15. func NewNonce(r io.Reader, length int) (string, error) {
  16. nonce := make([]byte, length)
  17. if _, err := io.ReadFull(r, nonce); err != nil {
  18. return "", err
  19. }
  20. return base64.RawURLEncoding.EncodeToString(nonce), nil
  21. }
  22. func Sign(ctx context.Context, bts []byte) (string, error) {
  23. home, err := os.UserHomeDir()
  24. if err != nil {
  25. return "", err
  26. }
  27. keyPath := filepath.Join(home, ".ollama", defaultPrivateKey)
  28. privateKeyFile, err := os.ReadFile(keyPath)
  29. if err != nil {
  30. slog.Info(fmt.Sprintf("Failed to load private key: %v", err))
  31. return "", err
  32. }
  33. privateKey, err := ssh.ParsePrivateKey(privateKeyFile)
  34. if err != nil {
  35. return "", err
  36. }
  37. // get the pubkey, but remove the type
  38. publicKey := ssh.MarshalAuthorizedKey(privateKey.PublicKey())
  39. parts := bytes.Split(publicKey, []byte(" "))
  40. if len(parts) < 2 {
  41. return "", fmt.Errorf("malformed public key")
  42. }
  43. signedData, err := privateKey.Sign(rand.Reader, bts)
  44. if err != nil {
  45. return "", err
  46. }
  47. // signature is <pubkey>:<signature>
  48. return fmt.Sprintf("%s:%s", bytes.TrimSpace(parts[1]), base64.StdEncoding.EncodeToString(signedData.Blob)), nil
  49. }