/
digest-auth.go
79 lines (76 loc) · 2.44 KB
/
digest-auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"bufio"
"crypto/md5"
"fmt"
auth "github.com/abbot/go-http-auth"
"log"
"os"
"strings"
)
func loadOrCreateDigestFile(name string) *os.File {
if _, err := os.Stat(name); os.IsNotExist(err) {
file, err := os.Create(name)
if err != nil {
log.Fatalf("Error with os.Create(): %v", err)
}
return file
} else {
file, err := os.Open(name)
if err != nil {
log.Fatalf("Error with os.Open(): %v", err)
}
return file
}
}
func loadDigestAuth(realm string) auth.DigestAuth {
file := loadOrCreateDigestFile(settings.DigestPath)
info, _ := file.Stat()
if info.Size() == 0 {
if *debug {
log.Println("Digest File is empty, requesting user and pass")
}
fmt.Println("You specified a digest file but it's empty, please choose a user & pass to login to your BTSyncInator site.")
// Get Username
fmt.Println("Username:")
bufferedIO := bufio.NewReader(os.Stdin)
username, err := bufferedIO.ReadString('\n')
if err != nil {
log.Fatalf("Error with bufferedIO.ReadString(\"\n\"): %v", err)
}
username = strings.Replace(username, "\n", "", -1)
// Get Password
fmt.Println("Password:")
bufferedIO = bufio.NewReader(os.Stdin)
password, err := bufferedIO.ReadString('\n')
if err != nil {
log.Fatalf("Error with bufferedIO.ReadString(\"\n\"): %v", err)
}
password = strings.Replace(password, "\n", "", -1)
// Setup string to hash through md5
byteContents := []byte(username)
byteContents = append(byteContents, []byte(":")...)
byteContents = append(byteContents, []byte(realm)...)
byteContents = append(byteContents, []byte(":")...)
byteContents = append(byteContents, []byte(password)...)
encryptedBytePassArray := md5.Sum(byteContents)
encryptedBytePass := encryptedBytePassArray[:]
// Setup bytes to write to htdigest file
byteContents = []byte(username)
byteContents = append(byteContents, []byte(":")...)
byteContents = append(byteContents, []byte(realm)...)
byteContents = append(byteContents, []byte(":")...)
byteContents = append(byteContents, []byte(fmt.Sprintf("%x", encryptedBytePass))...)
byteContents = append(byteContents, []byte("\n")...)
_, err = file.Write(byteContents)
if err != nil {
log.Fatalf("Error with file.Write(): %v", err)
}
file.Close()
} else if *debug {
log.Printf("Digest File %s loaded", file.Name())
}
secretProvider := auth.HtdigestFileProvider(file.Name())
digestAuth := auth.NewDigestAuthenticator(realm, secretProvider)
return *digestAuth
}