Debian Codegraph @

authpinning.go

raw
1 package main
2
3 import (
4 "fmt"
5 "log/slog"
6 "os"
7 "strings"
8 )
9
10 func findAuthPinFilePath(username string) (string, error) {
11 slog.Info("getting datapath for", "user", username)
12 dataPath, err := getDataPath(fsFriendlyJid(username), true)
13 if err != nil {
14 return strEmpty, fmt.Errorf("find auth pin file path: %w", err)
15 }
16 return fmt.Sprintf("%sauthpin", dataPath), nil
17 }
18
19 func parseAuthPinFile(username string) (bool, error) {
20 // Find auth pin file.
21 authPinFile, err := findAuthPinFilePath(username)
22 if err != nil {
23 return false, fmt.Errorf("parse auth pin file: %w", err)
24 }
25 // Read file.
26 slog.Info("reading auth pin", "file", authPinFile)
27 // #nosec G304 -- auth pin file comes from the local system, it's no untrusted input from the network
28 f, err := os.ReadFile(authPinFile)
29 if err != nil {
30 return false, fmt.Errorf("parse auth pin file: %w", err)
31 }
32 // Strip trailing newline.
33 content := strings.TrimSuffix(string(f), "\n")
34 noPLAIN := content == "noPLAIN"
35 slog.Info("parsing content", "noPLAIN", noPLAIN)
36 return noPLAIN, nil
37 }
38
39 func writeAuthPinFile(username string) error {
40 // Find auth pin file.
41 authPinFile, err := findAuthPinFilePath(username)
42 if err != nil {
43 return fmt.Errorf("write auth pin file: %w", err)
44 }
45 slog.Info("writing auth pin", "file", authPinFile)
46 err = os.WriteFile(authPinFile, []byte("noPLAIN\n"), 0o400)
47 return err
48 }