auth.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. "io/ioutil"
  14. "net"
  15. "net/http"
  16. "strings"
  17. "sync"
  18. "time"
  19. "github.com/gorilla/websocket"
  20. fernet "github.com/fernet/fernet-go"
  21. )
  22. var (
  23. // maps a websocket conn to a tokenid
  24. // tokenid's can be referenced from the global conf
  25. AuthenticatedClients = struct {
  26. Conns map[string]*websocket.Conn
  27. sync.RWMutex
  28. }{
  29. Conns: make(map[string]*websocket.Conn),
  30. }
  31. UnauthClients = struct {
  32. Conns map[string]*websocket.Conn
  33. sync.RWMutex
  34. }{
  35. Conns: make(map[string]*websocket.Conn),
  36. }
  37. )
  38. // check if websocket-token pair is auth'd
  39. func WsIsAuthenticated(conn *websocket.Conn, token string) bool {
  40. AuthenticatedClients.RLock()
  41. defer AuthenticatedClients.RUnlock()
  42. if AuthenticatedClients.Conns[token] == conn {
  43. return true
  44. } else {
  45. return false
  46. }
  47. }
  48. // quick check if websocket is authed at all for unauth broadcast (not for auth on its own)
  49. func WsAuthCheck(conn *websocket.Conn) bool {
  50. AuthenticatedClients.RLock()
  51. defer AuthenticatedClients.RUnlock()
  52. for tokenID, con := range AuthenticatedClients.Conns {
  53. if con == conn {
  54. config.Logger.Info(fmt.Sprintf("%s is in auth map",tokenID))
  55. return true
  56. }
  57. }
  58. config.Logger.Info("Client not in auth map")
  59. return false
  60. }
  61. func TokenIdAuthed(token string) bool {
  62. AuthenticatedClients.RLock()
  63. defer AuthenticatedClients.RUnlock()
  64. for tokenID, _ := range AuthenticatedClients.Conns {
  65. if token == tokenID {
  66. config.Logger.Info(fmt.Sprintf("%s is in auth map",token))
  67. return true
  68. }
  69. }
  70. config.Logger.Info("Token not in auth map")
  71. return false
  72. }
  73. // this takes a bool for auth/unauth -- also persists to config
  74. func AddToAuthMap(conn *websocket.Conn, token map[string]string, authed bool) error {
  75. tokenStr := token["token"]
  76. tokenId := token["id"]
  77. hashed := sha512.Sum512([]byte(tokenStr))
  78. hash := hex.EncodeToString(hashed[:])
  79. if authed {
  80. AuthenticatedClients.Lock()
  81. AuthenticatedClients.Conns[tokenId] = conn
  82. AuthenticatedClients.Unlock()
  83. UnauthClients.Lock()
  84. if _, ok := UnauthClients.Conns[tokenId]; ok {
  85. delete(UnauthClients.Conns, tokenId)
  86. }
  87. UnauthClients.Unlock()
  88. } else {
  89. UnauthClients.Lock()
  90. UnauthClients.Conns[tokenId] = conn
  91. UnauthClients.Unlock()
  92. AuthenticatedClients.Lock()
  93. if _, ok := AuthenticatedClients.Conns[tokenId]; ok {
  94. delete(AuthenticatedClients.Conns, tokenId)
  95. }
  96. AuthenticatedClients.Unlock()
  97. }
  98. now := time.Now().Format("2006-01-02_15:04:05")
  99. err := AddSession(tokenId, hash, now, authed)
  100. if err != nil {
  101. return err
  102. }
  103. return nil
  104. }
  105. // the same but the other way
  106. func RemoveFromAuthMap(tokenId string, fromAuthorized bool) error {
  107. if fromAuthorized {
  108. AuthenticatedClients.Lock()
  109. if _, ok := AuthenticatedClients.Conns[tokenId]; ok {
  110. delete(AuthenticatedClients.Conns, tokenId)
  111. }
  112. AuthenticatedClients.Unlock()
  113. } else {
  114. UnauthClients.Lock()
  115. if _, ok := UnauthClients.Conns[tokenId]; ok {
  116. delete(UnauthClients.Conns, tokenId)
  117. }
  118. UnauthClients.Unlock()
  119. }
  120. return nil
  121. }
  122. // check the validity of the token
  123. func CheckToken(token map[string]string, conn *websocket.Conn, r *http.Request, setup bool) bool {
  124. // great you have token. we see if valid.
  125. if token["token"] == "" {
  126. return false
  127. }
  128. config.Logger.Info(fmt.Sprintf("Checking token %s",token["id"]))
  129. conf := config.Conf()
  130. key := conf.KeyFile
  131. res, err := KeyfileDecrypt(token["token"], key)
  132. if err != nil {
  133. config.Logger.Warn("Invalid token provided")
  134. return false
  135. } else {
  136. // so you decrypt. now we see the useragent and ip.
  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. // hashed := sha512.Sum512([]byte(token["token"]))
  145. // hash := hex.EncodeToString(hashed[:])
  146. // you in auth map?
  147. if TokenIdAuthed(token["id"]) {
  148. if ip == res["ip"] && userAgent == res["user_agent"] {
  149. config.Logger.Info("Token authenticated")
  150. return true
  151. } else {
  152. config.Logger.Warn("Token doesn't match session!")
  153. return false
  154. }
  155. } else {
  156. config.Logger.Warn("Token isn't an authenticated session")
  157. return false
  158. }
  159. }
  160. return false
  161. }
  162. // create a new session token
  163. func CreateToken(conn *websocket.Conn, r *http.Request, setup bool) (map[string]string, error) {
  164. // extract conn info
  165. var ip string
  166. if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
  167. ip = strings.Split(forwarded, ",")[0]
  168. } else {
  169. ip, _, _ = net.SplitHostPort(r.RemoteAddr)
  170. }
  171. userAgent := r.Header.Get("User-Agent")
  172. conf := config.Conf()
  173. now := time.Now().Format("2006-01-02_15:04:05")
  174. // generate random strings for id, secret, and padding
  175. id := config.RandString(32)
  176. secret := config.RandString(128)
  177. padding := config.RandString(32)
  178. contents := map[string]string{
  179. "id": id,
  180. "ip": ip,
  181. "user_agent": userAgent,
  182. "secret": secret,
  183. "padding": padding,
  184. "authorized": fmt.Sprintf("%v", setup),
  185. "created": now,
  186. }
  187. // encrypt the contents
  188. key := conf.KeyFile
  189. encryptedText, err := KeyfileEncrypt(contents, key)
  190. if err != nil {
  191. config.Logger.Error(fmt.Sprintf("failed to encrypt token: %v", err))
  192. return nil, fmt.Errorf("failed to encrypt token: %v", err)
  193. }
  194. token := map[string]string{
  195. "id": id,
  196. "token": encryptedText,
  197. }
  198. // Update sessions in the system's configuration
  199. AddToAuthMap(conn, token, setup)
  200. return token, nil
  201. }
  202. // take session details and add to SysConfig
  203. func AddSession(tokenID string, hash string, created string, authorized bool) error {
  204. session := structs.SessionInfo{
  205. Hash: hash,
  206. Created: created,
  207. }
  208. if authorized {
  209. update := map[string]interface{}{
  210. "Sessions": map[string]interface{}{
  211. "Authorized": map[string]structs.SessionInfo{
  212. tokenID: session,
  213. },
  214. },
  215. }
  216. if err := config.UpdateConf(update); err != nil {
  217. return fmt.Errorf("Error adding session: %v", err)
  218. }
  219. if err := RemoveFromAuthMap(tokenID, false); err != nil {
  220. return fmt.Errorf("Error removing session: %v", err)
  221. }
  222. } else {
  223. update := map[string]interface{}{
  224. "Sessions": map[string]interface{}{
  225. "Unauthorized": map[string]structs.SessionInfo{
  226. tokenID: session,
  227. },
  228. },
  229. }
  230. if err := config.UpdateConf(update); err != nil {
  231. return fmt.Errorf("Error adding session: %v", err)
  232. }
  233. if err := RemoveFromAuthMap(tokenID, true); err != nil {
  234. return fmt.Errorf("Error removing session: %v", err)
  235. }
  236. }
  237. return nil
  238. }
  239. // encrypt the contents using stored keyfile val
  240. func KeyfileEncrypt(contents map[string]string, keyStr string) (string, error) {
  241. fileBytes, err := ioutil.ReadFile(keyStr)
  242. if err != nil {
  243. return "", err
  244. }
  245. contentBytes, err := json.Marshal(contents)
  246. if err != nil {
  247. return "", err
  248. }
  249. key, err := fernet.DecodeKey(string(fileBytes))
  250. if err != nil {
  251. return "", err
  252. }
  253. tok, err := fernet.EncryptAndSign(contentBytes, key)
  254. if err != nil {
  255. return "", err
  256. }
  257. return string(tok), nil
  258. }
  259. func KeyfileDecrypt(tokenStr string, keyStr string) (map[string]string, error) {
  260. fileBytes, err := ioutil.ReadFile(keyStr)
  261. if err != nil {
  262. return nil, err
  263. }
  264. key, err := fernet.DecodeKey(string(fileBytes))
  265. if err != nil {
  266. return nil, err
  267. }
  268. decrypted := fernet.VerifyAndDecrypt([]byte(tokenStr), 60*time.Second, []*fernet.Key{key})
  269. if decrypted == nil {
  270. return nil, fmt.Errorf("verification or decryption failed")
  271. }
  272. var contents map[string]string
  273. err = json.Unmarshal(decrypted, &contents)
  274. if err != nil {
  275. return nil, err
  276. }
  277. return contents, nil
  278. }
  279. // salted sha512
  280. func Hasher(password string) string {
  281. conf := config.Conf()
  282. salt := conf.Salt
  283. toHash := salt + password
  284. res := sha512.Sum512([]byte(toHash))
  285. return hex.EncodeToString(res[:])
  286. }
  287. // check if pw matches sysconfig
  288. func AuthenticateLogin(password string) bool {
  289. conf := config.Conf()
  290. hash := Hasher(password)
  291. if hash == conf.PwHash {
  292. return true
  293. } else {
  294. config.Logger.Warn(fmt.Sprintf("debug: failed pw hash: %v", hash))
  295. return false
  296. }
  297. }