auth.go 7.7 KB

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