auth.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package auth
  2. // package for authenticating websockets
  3. // we use a homespun jwt knock-off because no tls on lan
  4. // authentication adds you to the AuthenticatedClients map
  5. // broadcasts get sent to members of this map
  6. import (
  7. "crypto/rand"
  8. "crypto/sha256"
  9. "encoding/base64"
  10. "encoding/hex"
  11. "encoding/json"
  12. "fmt"
  13. "goseg/config"
  14. "goseg/structs"
  15. "net"
  16. "net/http"
  17. "strings"
  18. "sync"
  19. "time"
  20. "github.com/gorilla/websocket"
  21. "golang.org/x/crypto/nacl/secretbox"
  22. )
  23. var (
  24. // maps a websocket conn to a tokenid
  25. // tokenid's can be referenced from the global conf
  26. AuthenticatedClients = struct {
  27. Conns map[*websocket.Conn]string
  28. sync.RWMutex
  29. }{
  30. Conns: make(map[*websocket.Conn]string),
  31. }
  32. UnauthClients = struct {
  33. Conns map[*websocket.Conn]string
  34. sync.RWMutex
  35. }{
  36. Conns: make(map[*websocket.Conn]string),
  37. }
  38. )
  39. // check if websocket session is in the auth map
  40. func WsIsAuthenticated(conn *websocket.Conn) bool {
  41. AuthenticatedClients.Lock()
  42. defer AuthenticatedClients.Unlock()
  43. _, exists := AuthenticatedClients.Conns[conn]
  44. return exists
  45. }
  46. // check if a hash is in the auth map
  47. func hashAuthenticated(hash string) bool {
  48. AuthenticatedClients.RLock() // Acquire read lock
  49. defer AuthenticatedClients.RUnlock() // Release read lock
  50. for _, v := range AuthenticatedClients.Conns {
  51. if v == hash {
  52. return true
  53. }
  54. }
  55. return false
  56. }
  57. // check the validity of the token
  58. func CheckToken(token string, conn *websocket.Conn, r *http.Request, setup bool) (bool, string, error) {
  59. // no token? we have problem.
  60. if token == "" {
  61. authStatus := false
  62. if setup {
  63. authStatus = true
  64. }
  65. // you take token.
  66. newToken, err := CreateToken(conn, r, setup)
  67. if err != nil {
  68. return false, "", err
  69. }
  70. return authStatus, newToken["token"], nil
  71. } else {
  72. // great you have token. we see if valid.
  73. conf := config.Conf()
  74. key := conf.KeyFile
  75. res, err := KeyfileDecrypt(token, key)
  76. if err != nil {
  77. config.Logger.Warn("Invalid token provided")
  78. return false, "", err
  79. } else {
  80. // so you decrypt. now we see the useragent and ip.
  81. var ip string
  82. if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
  83. ip = strings.Split(forwarded, ",")[0]
  84. } else {
  85. ip, _, _ = net.SplitHostPort(r.RemoteAddr)
  86. }
  87. userAgent := r.Header.Get("User-Agent")
  88. hashed := sha256.Sum256([]byte(token))
  89. hash := hex.EncodeToString(hashed[:])
  90. // you in auth map?
  91. if hashAuthenticated(hash) {
  92. if ip == res["ip"] && userAgent == res["user_agent"] {
  93. return true, res["id"], nil
  94. } else {
  95. config.Logger.Warn("Token doesn't match session!")
  96. return false, res["id"], err
  97. }
  98. } else {
  99. config.Logger.Warn("Token isn't an authenticated session")
  100. return false, res["id"], err
  101. }
  102. }
  103. }
  104. return false, "", nil
  105. }
  106. // create a new session token
  107. func CreateToken(conn *websocket.Conn, r *http.Request, setup bool) (map[string]string, error) {
  108. // extract conn info
  109. var ip string
  110. if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
  111. ip = strings.Split(forwarded, ",")[0]
  112. } else {
  113. ip, _, _ = net.SplitHostPort(r.RemoteAddr)
  114. }
  115. userAgent := r.Header.Get("User-Agent")
  116. conf := config.Conf()
  117. now := time.Now().Format("2006-01-02_15:04:05")
  118. // generate random strings for id, secret, and padding
  119. id := config.RandString(32)
  120. secret := config.RandString(128)
  121. padding := config.RandString(32)
  122. contents := map[string]string{
  123. "id": id,
  124. "ip": ip,
  125. "user_agent": userAgent,
  126. "secret": secret,
  127. "padding": padding,
  128. "authorized": fmt.Sprintf("%v", setup),
  129. "created": now,
  130. }
  131. // encrypt the contents
  132. key := conf.KeyFile
  133. encryptedText, err := KeyfileEncrypt(contents, key)
  134. if err != nil {
  135. return nil, fmt.Errorf("failed to encrypt token: %v", err)
  136. }
  137. hashed := sha256.Sum256([]byte(encryptedText))
  138. hash := hex.EncodeToString(hashed[:])
  139. // Update sessions in the system's configuration
  140. AddSession(id, hash, now, setup)
  141. return map[string]string{
  142. "id": id,
  143. "token": encryptedText,
  144. }, nil
  145. }
  146. // take session details and add to SysConfig
  147. func AddSession(tokenID string, hash string, created string, authorized bool) error {
  148. session := structs.SessionInfo{
  149. Hash: hash,
  150. Created: created,
  151. }
  152. if authorized {
  153. update := map[string]interface{}{
  154. "Sessions": map[string]interface{}{
  155. "Authorized": map[string]string{
  156. "Hash": session.Hash,
  157. "Created": session.Created,
  158. },
  159. },
  160. }
  161. if err := config.UpdateConf(update); err != nil {
  162. return fmt.Errorf("Error adding session: %v", err)
  163. }
  164. if err := config.RemoveSession(tokenID, false); err != nil {
  165. return fmt.Errorf("Error removing session: %v", err)
  166. }
  167. } else {
  168. update := map[string]interface{}{
  169. "Sessions": map[string]interface{}{
  170. "Unauthorized": map[string]string{
  171. "Hash": session.Hash,
  172. "Created": session.Created,
  173. },
  174. },
  175. }
  176. if err := config.UpdateConf(update); err != nil {
  177. return fmt.Errorf("Error adding session: %v", err)
  178. }
  179. if err := config.RemoveSession(tokenID, true); err != nil {
  180. return fmt.Errorf("Error removing session: %v", err)
  181. }
  182. }
  183. return nil
  184. }
  185. // encrypt the contents using stored keyfile val
  186. func KeyfileEncrypt(contents map[string]string, key string) (string, error) {
  187. contentBytes, err := json.Marshal(contents)
  188. if err != nil {
  189. return "", err
  190. }
  191. // convert key to bytes
  192. keyBytes := []byte(key)
  193. if len(keyBytes) != 32 {
  194. return "", fmt.Errorf("key must be 32 bytes in length")
  195. }
  196. var keyArray [32]byte
  197. copy(keyArray[:], keyBytes)
  198. // generate nonce
  199. var nonce [24]byte
  200. if _, err := rand.Read(nonce[:]); err != nil {
  201. return "", err
  202. }
  203. // encrypt contents
  204. encrypted := secretbox.Seal(nonce[:], contentBytes, &nonce, &keyArray)
  205. return base64.URLEncoding.EncodeToString(encrypted), nil
  206. }
  207. // decrypt routine
  208. func KeyfileDecrypt(encryptedText string, key string) (map[string]string, error) {
  209. // get bytes
  210. keyBytes := []byte(key)
  211. var keyArray [32]byte
  212. copy(keyArray[:], keyBytes)
  213. encryptedBytes, err := base64.URLEncoding.DecodeString(encryptedText)
  214. if err != nil {
  215. return nil, err
  216. }
  217. // get nonce
  218. var nonce [24]byte
  219. copy(nonce[:], encryptedBytes[:24])
  220. // attempt decrypt
  221. decrypted, ok := secretbox.Open(nil, encryptedBytes[24:], &nonce, &keyArray)
  222. if !ok {
  223. return nil, fmt.Errorf("Decryption failed")
  224. }
  225. var contents map[string]string
  226. if err := json.Unmarshal(decrypted, &contents); err != nil {
  227. return nil, err
  228. }
  229. return contents, nil
  230. }
  231. // salted sha256
  232. func Hasher(password string) string {
  233. conf := config.Conf()
  234. salt := conf.Salt
  235. toHash := salt + password
  236. res := sha256.Sum256([]byte(toHash))
  237. return hex.EncodeToString(res[:])
  238. }
  239. // check if pw matches sysconfig
  240. func AuthenticateLogin(password string) bool {
  241. conf := config.Conf()
  242. hash := Hasher(password)
  243. if hash == conf.PwHash {
  244. return true
  245. } else {
  246. config.Logger.Warn(fmt.Sprintf("debug: failed pw hash: %v",hash))
  247. return false
  248. }
  249. }