Simple Golang SMTP relay/proxy server
go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package main
  2. import (
  3. "bufio"
  4. "errors"
  5. "os"
  6. "strings"
  7. "golang.org/x/crypto/bcrypt"
  8. )
  9. var (
  10. filename string
  11. )
  12. func AuthLoadFile(file string) error {
  13. f, err := os.Open(file)
  14. if err != nil {
  15. return err
  16. }
  17. f.Close()
  18. filename = file
  19. return nil
  20. }
  21. func AuthReady() bool {
  22. return (filename != "")
  23. }
  24. func AuthFetch(username string) (string, string, error) {
  25. if ! AuthReady() {
  26. return "", "", errors.New("Authentication file not specified. Call LoadFile() first")
  27. }
  28. file, err := os.Open(filename)
  29. if err != nil {
  30. return "", "", err
  31. }
  32. defer file.Close()
  33. scanner := bufio.NewScanner(file)
  34. for scanner.Scan() {
  35. parts := strings.Fields(scanner.Text())
  36. if len(parts) != 3 {
  37. continue
  38. }
  39. if strings.ToLower(username) == strings.ToLower(parts[0]) {
  40. return parts[1], parts[2], nil
  41. }
  42. }
  43. return "", "", errors.New("User not found")
  44. }
  45. func AuthCheckPassword(username string, secret string) error {
  46. hash, _, err := AuthFetch(username)
  47. if err != nil {
  48. return err
  49. }
  50. if bcrypt.CompareHashAndPassword([]byte(hash), []byte(secret)) == nil {
  51. return nil
  52. }
  53. return errors.New("Password invalid")
  54. }