authenticator.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "../config"
  32. utils "../utils"
  33. )
  34. type Authenticator struct {
  35. mailMaps map[string]string //TODO: temporary here. Later should be part of mailscanner and never accessed from here
  36. }
  37. func NewAuthenticator() (a *Authenticator) {
  38. a = &Authenticator{
  39. mailMaps: readMailMaps(), //TODO: temporary here. Later should be part of mailscanner and never accessed from here
  40. }
  41. return
  42. }
  43. func (a *Authenticator) Authenticate(user, password string) (string, bool) {
  44. if !utils.RegExpUtilsInstance().EmailChecker.MatchString(user) {
  45. return "", false
  46. }
  47. _, ok := a.mailMaps[user]
  48. return "", ok
  49. }
  50. func (a *Authenticator) Verify(user, token string) bool {
  51. if !utils.RegExpUtilsInstance().EmailChecker.MatchString(user) {
  52. return false
  53. }
  54. _, ok := a.mailMaps[user]
  55. return ok
  56. }
  57. func (a *Authenticator) MailPath(user string) string { //TODO: temporary here. Later should be part of mailscanner and never accessed from here
  58. return a.mailMaps[user]
  59. }
  60. func readMailMaps() map[string]string { //TODO: temporary here. Later should be part of mailscanner and never accessed from here
  61. mailMaps := make(map[string]string)
  62. mapsFile := config.ConfigInstance().VMailboxMaps
  63. if !utils.FileExists(mapsFile) {
  64. return mailMaps
  65. }
  66. file, err := os.Open(mapsFile)
  67. if err != nil {
  68. log.Fatalf("Unable to open virtual mailbox maps %s\n", mapsFile)
  69. }
  70. scanner := bufio.NewScanner(file)
  71. for scanner.Scan() {
  72. mailPathPair := strings.Split(scanner.Text(), " ")
  73. if len(mailPathPair) != 2 {
  74. log.Printf("Invalid record in virtual mailbox maps %s", scanner.Text())
  75. continue
  76. }
  77. mailMaps[mailPathPair[0]] = mailPathPair[1]
  78. }
  79. return mailMaps
  80. }