auth.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package auth
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/ed25519"
  6. "crypto/rand"
  7. "encoding/base64"
  8. "encoding/pem"
  9. "fmt"
  10. "io"
  11. "log/slog"
  12. "os"
  13. "path/filepath"
  14. "golang.org/x/crypto/ssh"
  15. )
  16. const defaultPrivateKey = "id_ed25519"
  17. func privateKey() (ssh.Signer, error) {
  18. home, err := os.UserHomeDir()
  19. if err != nil {
  20. return nil, err
  21. }
  22. keyPath := filepath.Join(home, ".ollama", defaultPrivateKey)
  23. privateKeyFile, err := os.ReadFile(keyPath)
  24. if err != nil {
  25. slog.Info(fmt.Sprintf("Failed to load private key: %v", err))
  26. return nil, err
  27. }
  28. return ssh.ParsePrivateKey(privateKeyFile)
  29. }
  30. func GetPublicKey() (ssh.PublicKey, error) {
  31. // try to read pubkey first
  32. pubkey, err := readPubkey()
  33. if err == nil {
  34. return pubkey, nil
  35. }
  36. privateKey, err := privateKey()
  37. if err == nil {
  38. return privateKey.PublicKey(), nil
  39. }
  40. err = initializeKeypair()
  41. if err != nil {
  42. return nil, err
  43. }
  44. return readPubkey()
  45. }
  46. func readPubkey() (ssh.PublicKey, error) {
  47. home, err := os.UserHomeDir()
  48. if err != nil {
  49. return nil, err
  50. }
  51. pubkeyPath := filepath.Join(home, ".ollama", defaultPrivateKey+".pub")
  52. _, err = os.Stat(pubkeyPath)
  53. if os.IsNotExist(err) {
  54. return nil, fmt.Errorf("public key not found")
  55. }
  56. pubKeyFile, err := os.ReadFile(pubkeyPath)
  57. if err != nil {
  58. return nil, fmt.Errorf("failed to read public key: %w", err)
  59. }
  60. return ssh.ParsePublicKey(pubKeyFile)
  61. }
  62. func NewNonce(r io.Reader, length int) (string, error) {
  63. nonce := make([]byte, length)
  64. if _, err := io.ReadFull(r, nonce); err != nil {
  65. return "", err
  66. }
  67. return base64.RawURLEncoding.EncodeToString(nonce), nil
  68. }
  69. func Sign(ctx context.Context, bts []byte) (string, error) {
  70. privateKey, err := privateKey()
  71. if err != nil {
  72. return "", err
  73. }
  74. // get the pubkey, but remove the type
  75. publicKey, err := GetPublicKey()
  76. if err != nil {
  77. return "", err
  78. }
  79. publicKeyBytes := ssh.MarshalAuthorizedKey(publicKey)
  80. parts := bytes.Split(publicKeyBytes, []byte(" "))
  81. if len(parts) < 2 {
  82. return "", fmt.Errorf("malformed public key")
  83. }
  84. signedData, err := privateKey.Sign(rand.Reader, bts)
  85. if err != nil {
  86. return "", err
  87. }
  88. // signature is <pubkey>:<signature>
  89. return fmt.Sprintf("%s:%s", bytes.TrimSpace(parts[1]), base64.StdEncoding.EncodeToString(signedData.Blob)), nil
  90. }
  91. func initializeKeypair() error {
  92. home, err := os.UserHomeDir()
  93. if err != nil {
  94. return err
  95. }
  96. privKeyPath := filepath.Join(home, ".ollama", "id_ed25519")
  97. pubKeyPath := filepath.Join(home, ".ollama", "id_ed25519.pub")
  98. _, err = os.Stat(privKeyPath)
  99. if os.IsNotExist(err) {
  100. fmt.Printf("Couldn't find '%s'. Generating new private key.\n", privKeyPath)
  101. cryptoPublicKey, cryptoPrivateKey, err := ed25519.GenerateKey(rand.Reader)
  102. if err != nil {
  103. return err
  104. }
  105. privateKeyBytes, err := ssh.MarshalPrivateKey(cryptoPrivateKey, "")
  106. if err != nil {
  107. return err
  108. }
  109. if err := os.MkdirAll(filepath.Dir(privKeyPath), 0o755); err != nil {
  110. return fmt.Errorf("could not create directory %w", err)
  111. }
  112. if err := os.WriteFile(privKeyPath, pem.EncodeToMemory(privateKeyBytes), 0o600); err != nil {
  113. return err
  114. }
  115. sshPublicKey, err := ssh.NewPublicKey(cryptoPublicKey)
  116. if err != nil {
  117. return err
  118. }
  119. publicKeyBytes := ssh.MarshalAuthorizedKey(sshPublicKey)
  120. if err := os.WriteFile(pubKeyPath, publicKeyBytes, 0o644); err != nil {
  121. return err
  122. }
  123. fmt.Printf("Your new public key is: \n\n%s\n", publicKeyBytes)
  124. }
  125. return nil
  126. }