auth.go 2.11 KB
Newer Older
1
package auth
Patrick Devine's avatar
Patrick Devine committed
2
3
4

import (
	"bytes"
5
	"context"
Patrick Devine's avatar
Patrick Devine committed
6
7
	"crypto/rand"
	"encoding/base64"
Michael Yang's avatar
lint  
Michael Yang committed
8
	"errors"
Patrick Devine's avatar
Patrick Devine committed
9
10
	"fmt"
	"io"
11
	"log/slog"
Patrick Devine's avatar
Patrick Devine committed
12
	"os"
Michael Yang's avatar
Michael Yang committed
13
	"path/filepath"
14
	"strings"
Patrick Devine's avatar
Patrick Devine committed
15
16

	"golang.org/x/crypto/ssh"
17
18
)

Michael Yang's avatar
Michael Yang committed
19
const defaultPrivateKey = "id_ed25519"
Patrick Devine's avatar
Patrick Devine committed
20

21
func keyPath() (string, error) {
22
23
24
25
26
27
28
29
30
31
32
33
34
	fileExists := func(fp string) bool {
		info, err := os.Stat(fp)
		if err != nil {
			return false
		}
		return !info.IsDir()
	}

	systemPath := filepath.Join("/usr/share/ollama/.ollama", defaultPrivateKey)
	if fileExists(systemPath) {
		return systemPath, nil
	}

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	home, err := os.UserHomeDir()
	if err != nil {
		return "", err
	}

	return filepath.Join(home, ".ollama", defaultPrivateKey), nil
}

func GetPublicKey() (string, error) {
	keyPath, err := keyPath()
	if err != nil {
		return "", err
	}

	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
}

Michael Yang's avatar
Michael Yang committed
65
func NewNonce(r io.Reader, length int) (string, error) {
Patrick Devine's avatar
Patrick Devine committed
66
	nonce := make([]byte, length)
Michael Yang's avatar
Michael Yang committed
67
	if _, err := io.ReadFull(r, nonce); err != nil {
Patrick Devine's avatar
Patrick Devine committed
68
69
		return "", err
	}
Michael Yang's avatar
Michael Yang committed
70

Michael Yang's avatar
Michael Yang committed
71
	return base64.RawURLEncoding.EncodeToString(nonce), nil
Patrick Devine's avatar
Patrick Devine committed
72
73
}

Michael Yang's avatar
Michael Yang committed
74
func Sign(ctx context.Context, bts []byte) (string, error) {
75
	keyPath, err := keyPath()
Patrick Devine's avatar
Patrick Devine committed
76
	if err != nil {
Michael Yang's avatar
Michael Yang committed
77
		return "", err
Patrick Devine's avatar
Patrick Devine committed
78
	}
Michael Yang's avatar
Michael Yang committed
79

Michael Yang's avatar
Michael Yang committed
80
	privateKeyFile, err := os.ReadFile(keyPath)
Patrick Devine's avatar
Patrick Devine committed
81
	if err != nil {
Michael Yang's avatar
Michael Yang committed
82
		slog.Info(fmt.Sprintf("Failed to load private key: %v", err))
Patrick Devine's avatar
Patrick Devine committed
83
84
85
		return "", err
	}

Michael Yang's avatar
Michael Yang committed
86
	privateKey, err := ssh.ParsePrivateKey(privateKeyFile)
Patrick Devine's avatar
Patrick Devine committed
87
88
89
90
91
	if err != nil {
		return "", err
	}

	// get the pubkey, but remove the type
Michael Yang's avatar
Michael Yang committed
92
93
	publicKey := ssh.MarshalAuthorizedKey(privateKey.PublicKey())
	parts := bytes.Split(publicKey, []byte(" "))
Patrick Devine's avatar
Patrick Devine committed
94
	if len(parts) < 2 {
Michael Yang's avatar
lint  
Michael Yang committed
95
		return "", errors.New("malformed public key")
Patrick Devine's avatar
Patrick Devine committed
96
97
	}

Michael Yang's avatar
Michael Yang committed
98
	signedData, err := privateKey.Sign(rand.Reader, bts)
Patrick Devine's avatar
Patrick Devine committed
99
100
101
102
103
	if err != nil {
		return "", err
	}

	// signature is <pubkey>:<signature>
Michael Yang's avatar
Michael Yang committed
104
	return fmt.Sprintf("%s:%s", bytes.TrimSpace(parts[1]), base64.StdEncoding.EncodeToString(signedData.Blob)), nil
Patrick Devine's avatar
Patrick Devine committed
105
}