2024-02-06 04:59:52 +08:00
|
|
|
package auth
|
2023-08-11 02:34:25 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-08-12 06:41:55 +08:00
|
|
|
"context"
|
2023-08-11 02:34:25 +08:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2024-08-02 05:52:15 +08:00
|
|
|
"errors"
|
2023-08-11 02:34:25 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-01-19 02:52:01 +08:00
|
|
|
"log/slog"
|
2023-08-11 02:34:25 +08:00
|
|
|
"os"
|
2023-09-20 00:36:30 +08:00
|
|
|
"path/filepath"
|
2024-05-01 02:02:08 +08:00
|
|
|
"strings"
|
2023-08-11 02:34:25 +08:00
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
2024-02-06 04:59:52 +08:00
|
|
|
)
|
|
|
|
|
2024-02-15 03:29:49 +08:00
|
|
|
const defaultPrivateKey = "id_ed25519"
|
2023-08-11 02:34:25 +08:00
|
|
|
|
2024-05-01 02:02:08 +08:00
|
|
|
func GetPublicKey() (string, error) {
|
2025-09-23 14:20:20 +08:00
|
|
|
home, err := os.UserHomeDir()
|
2024-05-01 02:02:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2025-09-23 14:20:20 +08:00
|
|
|
keyPath := filepath.Join(home, ".ollama", defaultPrivateKey)
|
2024-05-01 02:02:08 +08:00
|
|
|
privateKeyFile, err := os.ReadFile(keyPath)
|
|
|
|
if err != nil {
|
|
|
|
slog.Info(fmt.Sprintf("Failed to load private key: %v", err))
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKey, err := ssh.ParsePrivateKey(privateKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey := ssh.MarshalAuthorizedKey(privateKey.PublicKey())
|
|
|
|
|
|
|
|
return strings.TrimSpace(string(publicKey)), nil
|
|
|
|
}
|
|
|
|
|
2024-02-15 03:29:49 +08:00
|
|
|
func NewNonce(r io.Reader, length int) (string, error) {
|
2023-08-11 02:34:25 +08:00
|
|
|
nonce := make([]byte, length)
|
2024-02-15 03:29:49 +08:00
|
|
|
if _, err := io.ReadFull(r, nonce); err != nil {
|
2023-08-11 02:34:25 +08:00
|
|
|
return "", err
|
|
|
|
}
|
2023-08-22 09:38:31 +08:00
|
|
|
|
2024-02-15 03:29:49 +08:00
|
|
|
return base64.RawURLEncoding.EncodeToString(nonce), nil
|
2023-08-11 02:34:25 +08:00
|
|
|
}
|
|
|
|
|
2024-02-15 03:29:49 +08:00
|
|
|
func Sign(ctx context.Context, bts []byte) (string, error) {
|
2025-09-23 14:20:20 +08:00
|
|
|
home, err := os.UserHomeDir()
|
2023-08-11 02:34:25 +08:00
|
|
|
if err != nil {
|
2023-10-21 07:52:48 +08:00
|
|
|
return "", err
|
2023-08-11 02:34:25 +08:00
|
|
|
}
|
2024-02-08 03:00:06 +08:00
|
|
|
|
2025-09-23 14:20:20 +08:00
|
|
|
keyPath := filepath.Join(home, ".ollama", defaultPrivateKey)
|
2024-02-15 03:29:49 +08:00
|
|
|
privateKeyFile, err := os.ReadFile(keyPath)
|
2023-08-11 02:34:25 +08:00
|
|
|
if err != nil {
|
2024-02-15 03:29:49 +08:00
|
|
|
slog.Info(fmt.Sprintf("Failed to load private key: %v", err))
|
2023-08-11 02:34:25 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-02-15 03:29:49 +08:00
|
|
|
privateKey, err := ssh.ParsePrivateKey(privateKeyFile)
|
2023-08-11 02:34:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the pubkey, but remove the type
|
2024-02-15 03:29:49 +08:00
|
|
|
publicKey := ssh.MarshalAuthorizedKey(privateKey.PublicKey())
|
|
|
|
parts := bytes.Split(publicKey, []byte(" "))
|
2023-08-11 02:34:25 +08:00
|
|
|
if len(parts) < 2 {
|
2024-08-02 05:52:15 +08:00
|
|
|
return "", errors.New("malformed public key")
|
2023-08-11 02:34:25 +08:00
|
|
|
}
|
|
|
|
|
2024-02-15 03:29:49 +08:00
|
|
|
signedData, err := privateKey.Sign(rand.Reader, bts)
|
2023-08-11 02:34:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// signature is <pubkey>:<signature>
|
2024-02-15 03:29:49 +08:00
|
|
|
return fmt.Sprintf("%s:%s", bytes.TrimSpace(parts[1]), base64.StdEncoding.EncodeToString(signedData.Blob)), nil
|
2023-08-11 02:34:25 +08:00
|
|
|
}
|