auth.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/sha512"
  8. "encoding/hex"
  9. "encoding/json"
  10. "fmt"
  11. "goseg/config"
  12. "goseg/structs"
  13. "net"
  14. "net/http"
  15. "strings"
  16. "sync"
  17. "time"
  18. "github.com/gorilla/websocket"
  19. fernet "github.com/fernet/fernet-go"
  20. )
  21. var (
  22. // maps a websocket conn to a tokenid
  23. // tokenid's can be referenced from the global conf
  24. AuthenticatedClients = struct {
  25. Conns map[string]*websocket.Conn
  26. sync.RWMutex
  27. }{
  28. Conns: make(map[string]*websocket.Conn),
  29. }
  30. UnauthClients = struct {
  31. Conns map[string]*websocket.Conn
  32. sync.RWMutex
  33. }{
  34. Conns: make(map[string]*websocket.Conn),
  35. }
  36. )
  37. // check if websocket-token pair is auth'd
  38. func WsIsAuthenticated(conn *websocket.Conn, token string) bool {
  39. AuthenticatedClients.RLock()
  40. defer AuthenticatedClients.RUnlock()
  41. if AuthenticatedClients.Conns[token] == conn {
  42. return true
  43. } else {
  44. return false
  45. }
  46. }
  47. // quick check if websocket is authed at all for unauth broadcast (not for actual auth)
  48. func WsAuthCheck(conn *websocket.Conn) bool {
  49. AuthenticatedClients.RLock()
  50. defer AuthenticatedClients.RUnlock()
  51. for _, con := range AuthenticatedClients.Conns {
  52. if con == conn {
  53. config.Logger.Info("Client in auth map")
  54. return true
  55. }
  56. }
  57. config.Logger.Info("Client not in auth map")
  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 := sha512.Sum512([]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 {
  94. // great you have token. we see if valid.
  95. if token["token"] == "" {
  96. return false
  97. }
  98. config.Logger.Info(fmt.Sprintf("Checking token %s",token["id"]))
  99. conf := config.Conf()
  100. key := conf.KeyFile
  101. res, err := KeyfileDecrypt(token["token"], key)
  102. if err != nil {
  103. config.Logger.Warn("Invalid token provided")
  104. return false
  105. } else {
  106. // so you decrypt. now we see the useragent and ip.
  107. var ip string
  108. if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
  109. ip = strings.Split(forwarded, ",")[0]
  110. } else {
  111. ip, _, _ = net.SplitHostPort(r.RemoteAddr)
  112. }
  113. userAgent := r.Header.Get("User-Agent")
  114. hashed := sha512.Sum512([]byte(token["token"]))
  115. hash := hex.EncodeToString(hashed[:])
  116. // you in auth map?
  117. if WsIsAuthenticated(conn, hash) {
  118. if ip == res["ip"] && userAgent == res["user_agent"] {
  119. config.Logger.Info("Token authenticated")
  120. return true
  121. } else {
  122. config.Logger.Warn("Token doesn't match session!")
  123. return false
  124. }
  125. } else {
  126. config.Logger.Warn("Token isn't an authenticated session")
  127. return false
  128. }
  129. }
  130. return false
  131. }
  132. // create a new session token
  133. func CreateToken(conn *websocket.Conn, r *http.Request, setup bool) (map[string]string, error) {
  134. // extract conn info
  135. var ip string
  136. if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
  137. ip = strings.Split(forwarded, ",")[0]
  138. } else {
  139. ip, _, _ = net.SplitHostPort(r.RemoteAddr)
  140. }
  141. userAgent := r.Header.Get("User-Agent")
  142. conf := config.Conf()
  143. now := time.Now().Format("2006-01-02_15:04:05")
  144. // generate random strings for id, secret, and padding
  145. id := config.RandString(32)
  146. secret := config.RandString(128)
  147. padding := config.RandString(32)
  148. contents := map[string]string{
  149. "id": id,
  150. "ip": ip,
  151. "user_agent": userAgent,
  152. "secret": secret,
  153. "padding": padding,
  154. "authorized": fmt.Sprintf("%v", setup),
  155. "created": now,
  156. }
  157. // encrypt the contents
  158. key := conf.KeyFile
  159. encryptedText, err := KeyfileEncrypt(contents, key)
  160. if err != nil {
  161. config.Logger.Error(fmt.Sprintf("failed to encrypt token: %v", err))
  162. return nil, fmt.Errorf("failed to encrypt token: %v", err)
  163. }
  164. token := map[string]string{
  165. "id": id,
  166. "token": encryptedText,
  167. }
  168. // Update sessions in the system's configuration
  169. AddToAuthMap(conn, token, setup)
  170. return token, nil
  171. }
  172. // take session details and add to SysConfig
  173. func AddSession(tokenID string, hash string, created string, authorized bool) error {
  174. session := structs.SessionInfo{
  175. Hash: hash,
  176. Created: created,
  177. }
  178. if authorized {
  179. update := map[string]interface{}{
  180. "Sessions": map[string]interface{}{
  181. "Authorized": map[string]structs.SessionInfo{
  182. tokenID: session,
  183. },
  184. },
  185. }
  186. if err := config.UpdateConf(update); err != nil {
  187. return fmt.Errorf("Error adding session: %v", err)
  188. }
  189. if err := config.RemoveSession(tokenID, false); err != nil {
  190. return fmt.Errorf("Error removing session: %v", err)
  191. }
  192. } else {
  193. update := map[string]interface{}{
  194. "Sessions": map[string]interface{}{
  195. "Unauthorized": map[string]structs.SessionInfo{
  196. tokenID: session,
  197. },
  198. },
  199. }
  200. if err := config.UpdateConf(update); err != nil {
  201. return fmt.Errorf("Error adding session: %v", err)
  202. }
  203. if err := config.RemoveSession(tokenID, true); err != nil {
  204. return fmt.Errorf("Error removing session: %v", err)
  205. }
  206. }
  207. return nil
  208. }
  209. // encrypt the contents using stored keyfile val
  210. func KeyfileEncrypt(contents map[string]string, keyStr string) (string, error) {
  211. contentBytes, err := json.Marshal(contents)
  212. if err != nil {
  213. return "", err
  214. }
  215. key, err := fernet.DecodeKey(keyStr)
  216. if err != nil {
  217. return "", err
  218. }
  219. tok, err := fernet.EncryptAndSign(contentBytes, key)
  220. if err != nil {
  221. return "", err
  222. }
  223. return string(tok), nil
  224. }
  225. func KeyfileDecrypt(tokenStr string, keyStr string) (map[string]string, error) {
  226. key, err := fernet.DecodeKey(keyStr)
  227. if err != nil {
  228. return nil, err
  229. }
  230. decrypted := fernet.VerifyAndDecrypt([]byte(tokenStr), 60*time.Second, []*fernet.Key{key})
  231. if decrypted == nil {
  232. return nil, fmt.Errorf("verification or decryption failed")
  233. }
  234. var contents map[string]string
  235. err = json.Unmarshal(decrypted, &contents)
  236. if err != nil {
  237. return nil, err
  238. }
  239. return contents, nil
  240. }
  241. // salted sha512
  242. func Hasher(password string) string {
  243. conf := config.Conf()
  244. salt := conf.Salt
  245. toHash := salt + password
  246. res := sha512.Sum512([]byte(toHash))
  247. return hex.EncodeToString(res[:])
  248. }
  249. // check if pw matches sysconfig
  250. func AuthenticateLogin(password string) bool {
  251. conf := config.Conf()
  252. hash := Hasher(password)
  253. if hash == conf.PwHash {
  254. return true
  255. } else {
  256. config.Logger.Warn(fmt.Sprintf("debug: failed pw hash: %v", hash))
  257. return false
  258. }
  259. }