Skip to content

Commit

Permalink
Separate credentials to their own config
Browse files Browse the repository at this point in the history
  • Loading branch information
scheibling committed Feb 4, 2024
1 parent d90c6c9 commit 736f9df
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ env
__debug*
tls*.crt
tls*.key
*.py
*.py
credentials.json
10 changes: 10 additions & 0 deletions credentials.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"all": {
"password": "abc123",
"allowedDomains": []
},
"specific": {
"password": "123abc",
"allowedDomains": ["example.com"]
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ require (
github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead // direct
github.com/emersion/go-smtp v0.16.0 // direct
)

require golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead h1:fI1Jck0vUrXT8b
github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-smtp v0.16.0 h1:eB9CY9527WdEZSs5sWisTmilDX7gG+Q/2IdRcmubpa8=
github.com/emersion/go-smtp v0.16.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
58 changes: 57 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import (

"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
"golang.org/x/exp/maps"
)

var TERMINATED string
var LASTMOD int64

type Credential struct {
Password string `json:"password"`
AllowedDomains []string `json:"allowedDomains"`
Expand Down Expand Up @@ -131,6 +135,7 @@ func (s *Session) SendMail() error {
}

func (s *Session) AuthPlain(username, password string) error {
fmt.Println(config.Credentials)
val, ok := config.Credentials[username]

if ok && val.Password == password {
Expand Down Expand Up @@ -223,17 +228,39 @@ func ListenHealthcheck() {
}

func main() {
TERMINATED = ""

// Create config
config = &Config{}
credentials := &map[string]Credential{}

// Get last changed timestamp
credentialsInfo, err := os.Stat("credentials.json")
if err != nil {
log.Fatal(err)
TERMINATED = err.Error()
}

LASTMOD = credentialsInfo.ModTime().Unix()

// Read config.json into temp string
configFile, err := os.ReadFile("config.json")
if err != nil {
log.Fatal(err)
TERMINATED = err.Error()
}

credentialFile, err := os.ReadFile("credentials.json")
if err != nil {
log.Fatal(err)
TERMINATED = err.Error()
}

// Unmarshal config
json.Unmarshal(configFile, &config)
json.Unmarshal(credentialFile, &credentials)

config.Credentials = *credentials

be := &RelayBackend{}

Expand Down Expand Up @@ -332,5 +359,34 @@ func main() {

go Listen(smtps)

ListenHealthcheck()
go ListenHealthcheck()

for {
if TERMINATED != "" {
log.Fatal(TERMINATED)
panic(fmt.Errorf(TERMINATED))
}
time.Sleep(time.Duration(10) * time.Second)

fileInfo, err := os.Stat("credentials.json")
if err != nil {
TERMINATED = err.Error()
}

if LASTMOD < fileInfo.ModTime().Unix() {
updatedCredentials := &map[string]Credential{}
credentialFile, err := os.ReadFile("credentials.json")
if err != nil {
TERMINATED = err.Error()
}

json.Unmarshal(credentialFile, &updatedCredentials)

config.Credentials = *updatedCredentials
LASTMOD = fileInfo.ModTime().Unix()

fmt.Println("Updated credentials")
fmt.Println(maps.Keys(*credentials))
}
}
}

0 comments on commit 736f9df

Please sign in to comment.