auth.go 7.5 KB

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