auth.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package auth
  2. import (
  3. "crypto/rand"
  4. "crypto/sha256"
  5. "encoding/base64"
  6. "encoding/hex"
  7. "encoding/json"
  8. "goseg/config"
  9. "fmt"
  10. "time"
  11. "sync"
  12. "github.com/gorilla/websocket"
  13. "golang.org/x/crypto/nacl/secretbox"
  14. )
  15. var (
  16. AuthenticatedClients = struct{
  17. Conns map[*websocket.Conn]bool
  18. sync.Mutex
  19. }{
  20. Conns: make(map[*websocket.Conn]bool),
  21. }
  22. )
  23. // CheckToken checks the validity of the token.
  24. func CheckToken(token string, conn *websocket.Conn, setup bool) (bool, string, error) {
  25. if token == "" {
  26. authStatus := false
  27. if setup {
  28. authStatus = true
  29. }
  30. newToken, err := CreateToken(conn, setup)
  31. if err != nil {
  32. return false, "", err
  33. }
  34. return authStatus, newToken["token"], nil
  35. }
  36. // Token verification logic here...
  37. // ...
  38. return false, "", nil
  39. }
  40. // CreateToken creates a new session token.
  41. func CreateToken(conn *websocket.Conn, setup bool) (map[string]string, error) {
  42. // placeholder logic for IP and UserAgent
  43. ip := "localhost"
  44. userAgent := "lick"
  45. // if more properties are needed from websocket, you can extract them here
  46. conf := config.Conf()
  47. key := conf.KeyFile
  48. // generate random strings for id, secret, and padding
  49. id := NewSecretString(32)
  50. secret := NewSecretString(128)
  51. padding := NewSecretString(32)
  52. contents := map[string]string{
  53. "id": id,
  54. "ip": ip,
  55. "user_agent": userAgent,
  56. "secret": secret,
  57. "padding": padding,
  58. "authorized": fmt.Sprintf("%v", setup),
  59. "created": time.Now().Format("2006-01-02_15:04:05"),
  60. }
  61. // encrypt the contents
  62. encryptedText, err := KeyfileEncrypt(contents, key)
  63. if err != nil {
  64. return nil, fmt.Errorf("failed to encrypt token: %v", err)
  65. }
  66. // and hash
  67. hashed := sha256.Sum256([]byte(encryptedText))
  68. // Update sessions in the system's configuration
  69. now := time.Now().Format("2006-01-02_15:04:05")
  70. if setup {
  71. updates := map[string]interface{}{
  72. "Sessions": map[string]interface{}{
  73. "Authorized": map[string]string{
  74. "Hash": hex.EncodeToString(hashed[:]),
  75. "Created": now,
  76. },
  77. },
  78. }
  79. config.UpdateConf(updates)
  80. } else {
  81. updates := map[string]interface{}{
  82. "Sessions": map[string]interface{}{
  83. "Unauthorized": map[string]string{
  84. "Hash": hex.EncodeToString(hashed[:]),
  85. "Created": now,
  86. },
  87. },
  88. }
  89. config.UpdateConf(updates)
  90. }
  91. return map[string]string{
  92. "id": id,
  93. "token": encryptedText,
  94. }, nil
  95. }
  96. // encrypt the contents using stored keyfile val
  97. func KeyfileEncrypt(contents map[string]string, key string) (string, error) {
  98. contentBytes, err := json.Marshal(contents)
  99. if err != nil {
  100. return "", err
  101. }
  102. // convert key to bytes
  103. keyBytes := []byte(key)
  104. if len(keyBytes) != 32 {
  105. return "", fmt.Errorf("key must be 32 bytes in length")
  106. }
  107. var keyArray [32]byte
  108. copy(keyArray[:], keyBytes)
  109. // generate nonce
  110. var nonce [24]byte
  111. if _, err := rand.Read(nonce[:]); err != nil {
  112. return "", err
  113. }
  114. // encrypt contents
  115. encrypted := secretbox.Seal(nonce[:], contentBytes, &nonce, &keyArray)
  116. return base64.URLEncoding.EncodeToString(encrypted), nil
  117. }
  118. // decrypt routine
  119. func KeyfileDecrypt(encryptedText string, key string) (map[string]string, error) {
  120. // get bytes
  121. keyBytes := []byte(key)
  122. var keyArray [32]byte
  123. copy(keyArray[:], keyBytes)
  124. encryptedBytes, err := base64.URLEncoding.DecodeString(encryptedText)
  125. if err != nil {
  126. return nil, err
  127. }
  128. // get nonce
  129. var nonce [24]byte
  130. copy(nonce[:], encryptedBytes[:24])
  131. // attempt decrypt
  132. decrypted, ok := secretbox.Open(nil, encryptedBytes[24:], &nonce, &keyArray)
  133. if !ok {
  134. return nil, fmt.Errorf("Decryption failed")
  135. }
  136. var contents map[string]string
  137. if err := json.Unmarshal(decrypted, &contents); err != nil {
  138. return nil, err
  139. }
  140. return contents, nil
  141. }
  142. // NewSecretString generates a random secret string of the given length.
  143. func NewSecretString(length int) string {
  144. randBytes := make([]byte, length)
  145. _, err := rand.Read(randBytes)
  146. if err != nil {
  147. return ""
  148. }
  149. return base64.URLEncoding.EncodeToString(randBytes)
  150. }
  151. func Hasher(password string) string {
  152. conf := config.Conf()
  153. salt := conf.Salt
  154. toHash := password + salt
  155. res := sha256.Sum256([]byte(toHash))
  156. return hex.EncodeToString(res[:])
  157. }
  158. func AuthenticateLogin(password string) bool {
  159. conf := config.Conf()
  160. if Hasher(password) == conf.PwHash {
  161. return true
  162. } else {
  163. return false
  164. }
  165. }
  166. func WsIsAuthenticated(conn *websocket.Conn) bool {
  167. AuthenticatedClients.Lock()
  168. _, exists := AuthenticatedClients.Conns[conn]
  169. AuthenticatedClients.Unlock()
  170. return exists
  171. }