auth.go 6.3 KB

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