]> git.lizzy.rs Git - go-anidb.git/blob - auth.go
anidb: Initial attempt at high level AniDB library
[go-anidb.git] / auth.go
1 package anidb
2
3 import (
4         "crypto/aes"
5         "crypto/cipher"
6         "crypto/rand"
7         "errors"
8         "io"
9         "runtime"
10 )
11
12 // We still have the key and IV somewhere in memory...
13 // but it's better than plaintext.
14 type credentials struct {
15         username []byte
16         password []byte
17         udpKey   []byte
18 }
19
20 func (c *credentials) shred() {
21         if c != nil {
22                 io.ReadFull(rand.Reader, c.username)
23                 io.ReadFull(rand.Reader, c.password)
24                 io.ReadFull(rand.Reader, c.udpKey)
25                 c.username = nil
26                 c.password = nil
27                 c.udpKey = nil
28         }
29 }
30
31 // Randomly generated on every execution
32 var aesKey []byte
33
34 func init() {
35         aesKey = make([]byte, aes.BlockSize)
36         if _, err := io.ReadFull(rand.Reader, aesKey); err != nil {
37                 panic(err)
38         }
39 }
40
41 func crypt(plaintext string) []byte {
42         p := []byte(plaintext)
43
44         block, err := aes.NewCipher(aesKey)
45         if err != nil {
46                 panic(err)
47         }
48
49         ciphertext := make([]byte, len(p)+aes.BlockSize)
50         iv := ciphertext[:aes.BlockSize]
51         if _, err := io.ReadFull(rand.Reader, iv); err != nil {
52                 panic(err)
53         }
54
55         stream := cipher.NewCTR(block, iv)
56         stream.XORKeyStream(ciphertext[aes.BlockSize:], p)
57
58         return ciphertext
59 }
60
61 func decrypt(ciphertext []byte) string {
62         if len(ciphertext) <= aes.BlockSize {
63                 return ""
64         }
65         p := make([]byte, len(ciphertext)-aes.BlockSize)
66
67         block, err := aes.NewCipher(aesKey)
68         if err != nil {
69                 panic(err)
70         }
71
72         stream := cipher.NewCTR(block, ciphertext[:aes.BlockSize])
73         stream.XORKeyStream(p, ciphertext[aes.BlockSize:])
74
75         return string(p)
76 }
77
78 func newCredentials(username, password, udpKey string) *credentials {
79         return &credentials{
80                 username: crypt(username),
81                 password: crypt(password),
82                 udpKey:   crypt(udpKey),
83         }
84 }
85
86 // Re-authenticates the current user. Do not use unless really necessary.
87 func (a *AniDB) ReAuth() error {
88         return a.udp.ReAuth()
89 }
90
91 func (udp *udpWrap) ReAuth() error {
92         if c := udp.credentials; c != nil {
93                 defer runtime.GC() // any better way to clean the plaintexts?
94
95                 udp.connected = true
96                 return udp.Auth(
97                         decrypt(c.username),
98                         decrypt(c.password),
99                         decrypt(c.udpKey))
100         }
101         return errors.New("No credentials stored")
102 }
103
104 // Saves the used credentials in the AniDB struct, to allow automatic
105 // re-authentication when needed; they are (properly) encrypted with a key that's
106 // uniquely generated every time the module is initialized.
107 func (a *AniDB) SetCredentials(username, password, udpKey string) {
108         a.udp.credentials.shred()
109         a.udp.credentials = newCredentials(username, password, udpKey)
110 }
111
112 // Authenticates to anidb's UDP API and, on success, stores the credentials using
113 // SetCredentials. If udpKey is not "", the communication with the server
114 // will be encrypted, but in the VERY weak ECB mode.
115 func (a *AniDB) Auth(username, password, udpKey string) (err error) {
116         defer runtime.GC() // any better way to clean the plaintexts?
117
118         if err = a.udp.Auth(username, password, udpKey); err == nil {
119                 a.udp.connected = true
120                 a.SetCredentials(username, password, udpKey)
121         }
122         return
123 }
124
125 // Logs the user out and removes the credentials from the AniDB struct.
126 func (a *AniDB) Logout() error {
127         if a.udp.connected {
128                 a.udp.credentials.shred()
129                 a.udp.credentials = nil
130
131                 a.udp.connected = false
132                 return a.udp.Logout()
133         }
134         return nil
135 }