auth.go 2.35 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
	fileIsReadable := func(fp string) bool {
23
24
25
26
		info, err := os.Stat(fp)
		if err != nil {
			return false
		}
27
28
29
30
31
32
33
34
35
36
37
38
39

		// Check that it's a regular file, not a directory or other file type
		if !info.Mode().IsRegular() {
			return false
		}

		// Try to open it to check readability
		file, err := os.Open(fp)
		if err != nil {
			return false
		}
		file.Close()
		return true
40
41
42
	}

	systemPath := filepath.Join("/usr/share/ollama/.ollama", defaultPrivateKey)
43
	if fileIsReadable(systemPath) {
44
45
46
		return systemPath, nil
	}

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
	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
77
func NewNonce(r io.Reader, length int) (string, error) {
Patrick Devine's avatar
Patrick Devine committed
78
	nonce := make([]byte, length)
Michael Yang's avatar
Michael Yang committed
79
	if _, err := io.ReadFull(r, nonce); err != nil {
Patrick Devine's avatar
Patrick Devine committed
80
81
		return "", err
	}
Michael Yang's avatar
Michael Yang committed
82

Michael Yang's avatar
Michael Yang committed
83
	return base64.RawURLEncoding.EncodeToString(nonce), nil
Patrick Devine's avatar
Patrick Devine committed
84
85
}

Michael Yang's avatar
Michael Yang committed
86
func Sign(ctx context.Context, bts []byte) (string, error) {
87
	keyPath, err := keyPath()
Patrick Devine's avatar
Patrick Devine committed
88
	if err != nil {
Michael Yang's avatar
Michael Yang committed
89
		return "", err
Patrick Devine's avatar
Patrick Devine committed
90
	}
Michael Yang's avatar
Michael Yang committed
91

Michael Yang's avatar
Michael Yang committed
92
	privateKeyFile, err := os.ReadFile(keyPath)
Patrick Devine's avatar
Patrick Devine committed
93
	if err != nil {
Michael Yang's avatar
Michael Yang committed
94
		slog.Info(fmt.Sprintf("Failed to load private key: %v", err))
Patrick Devine's avatar
Patrick Devine committed
95
96
97
		return "", err
	}

Michael Yang's avatar
Michael Yang committed
98
	privateKey, err := ssh.ParsePrivateKey(privateKeyFile)
Patrick Devine's avatar
Patrick Devine committed
99
100
101
102
103
	if err != nil {
		return "", err
	}

	// get the pubkey, but remove the type
Michael Yang's avatar
Michael Yang committed
104
105
	publicKey := ssh.MarshalAuthorizedKey(privateKey.PublicKey())
	parts := bytes.Split(publicKey, []byte(" "))
Patrick Devine's avatar
Patrick Devine committed
106
	if len(parts) < 2 {
Michael Yang's avatar
lint  
Michael Yang committed
107
		return "", errors.New("malformed public key")
Patrick Devine's avatar
Patrick Devine committed
108
109
	}

Michael Yang's avatar
Michael Yang committed
110
	signedData, err := privateKey.Sign(rand.Reader, bts)
Patrick Devine's avatar
Patrick Devine committed
111
112
113
114
115
	if err != nil {
		return "", err
	}

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