authenticator.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package auth
  26. import (
  27. "bufio"
  28. "log"
  29. "os"
  30. "strings"
  31. config "git.semlanik.org/semlanik/gostfix/config"
  32. db "git.semlanik.org/semlanik/gostfix/db"
  33. utils "git.semlanik.org/semlanik/gostfix/utils"
  34. uuid "github.com/google/uuid"
  35. )
  36. type Authenticator struct {
  37. storage *db.Storage
  38. mailMaps map[string]string //TODO: temporary here. Later should be part of mailscanner and never accessed from here
  39. }
  40. func NewAuthenticator() (a *Authenticator) {
  41. storage, err := db.NewStorage()
  42. if err != nil {
  43. log.Fatalf("Unable to intialize user storage %s", err)
  44. return nil
  45. }
  46. a = &Authenticator{
  47. mailMaps: readMailMaps(), //TODO: temporary here. Later should be part of mailscanner and never accessed from here
  48. storage: storage,
  49. }
  50. return
  51. }
  52. func (a *Authenticator) Authenticate(user, password string) (string, bool) {
  53. if !utils.RegExpUtilsInstance().EmailChecker.MatchString(user) {
  54. return "", false
  55. }
  56. if a.storage.CheckUser(user, password) != nil {
  57. return "", false
  58. }
  59. token := uuid.New().String()
  60. a.storage.AddToken(user, token)
  61. return token, true
  62. }
  63. func (a *Authenticator) Verify(user, token string) bool {
  64. if !utils.RegExpUtilsInstance().EmailChecker.MatchString(user) {
  65. return false
  66. }
  67. return a.storage.CheckToken(user, token) == nil
  68. }
  69. func (a *Authenticator) MailPath(user string) string { //TODO: temporary here. Later should be part of mailscanner and never accessed from here
  70. return a.mailMaps[user]
  71. }
  72. func readMailMaps() map[string]string { //TODO: temporary here. Later should be part of mailscanner and never accessed from here
  73. mailMaps := make(map[string]string)
  74. mapsFile := config.ConfigInstance().VMailboxMaps
  75. if !utils.FileExists(mapsFile) {
  76. return mailMaps
  77. }
  78. file, err := os.Open(mapsFile)
  79. if err != nil {
  80. log.Fatalf("Unable to open virtual mailbox maps %s\n", mapsFile)
  81. }
  82. scanner := bufio.NewScanner(file)
  83. for scanner.Scan() {
  84. mailPathPair := strings.Split(scanner.Text(), " ")
  85. if len(mailPathPair) != 2 {
  86. log.Printf("Invalid record in virtual mailbox maps %s", scanner.Text())
  87. continue
  88. }
  89. mailMaps[mailPathPair[0]] = mailPathPair[1]
  90. }
  91. return mailMaps
  92. }