From b2e65a9df08f7ddac6d26312fd38a8141234a8db Mon Sep 17 00:00:00 2001 From: Urko Date: Wed, 5 Jul 2023 22:07:10 +0200 Subject: [PATCH] init app --- cmd/http/main.go | 60 +++++++++++ cmd/http/views/error.hbs | 24 +++++ cmd/http/views/index.hbs | 37 +++++++ cmd/http/views/styles.hbs | 63 ++++++++++++ cmd/http/views/success.hbs | 22 ++++ go.mod | 33 ++++++ go.sum | 101 +++++++++++++++++++ internal/api/handler/helper.go | 25 +++++ internal/api/handler/prosody_hdl.go | 37 +++++++ internal/api/server.go | 80 +++++++++++++++ internal/services/prosody/account.go | 50 +++++++++ internal/services/prosody/change_password.go | 53 ++++++++++ internal/services/prosody/prosody.go | 19 ++++ kit/config/config.go | 32 ++++++ kit/path.go | 9 ++ 15 files changed, 645 insertions(+) create mode 100644 cmd/http/main.go create mode 100644 cmd/http/views/error.hbs create mode 100644 cmd/http/views/index.hbs create mode 100644 cmd/http/views/styles.hbs create mode 100644 cmd/http/views/success.hbs create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/api/handler/helper.go create mode 100644 internal/api/handler/prosody_hdl.go create mode 100644 internal/api/server.go create mode 100644 internal/services/prosody/account.go create mode 100644 internal/services/prosody/change_password.go create mode 100644 internal/services/prosody/prosody.go create mode 100644 kit/config/config.go create mode 100644 kit/path.go diff --git a/cmd/http/main.go b/cmd/http/main.go new file mode 100644 index 0000000..1e040b9 --- /dev/null +++ b/cmd/http/main.go @@ -0,0 +1,60 @@ +package main + +import ( + "context" + "fmt" + "log" + "os" + "os/signal" + "syscall" + + "gitea.urkob.com/urko/prosody-password/internal/api" + "gitea.urkob.com/urko/prosody-password/internal/services/prosody" + "gitea.urkob.com/urko/prosody-password/kit/config" +) + +func main() { + envFile := "" + if os.Getenv("PROSODY_ENV") != "prod" { + envFile = ".env" + } + cfg := config.NewConfig(envFile) + + ctx, cancel := context.WithCancel(signalContext(context.Background())) + defer cancel() + + restServer := api.NewRestServer(prosody.NewProsody(cfg.Domain)) + + go func() { + if err := restServer.Start(cfg.ApiPort, cfg.Views); err != nil { + panic(fmt.Errorf("restServer.Start: %w", err)) + } + }() + + <-ctx.Done() + + log.Println("on shutdown") + if restServer != nil { + restServer.Shutdown() + } + log.Println("gracefully shutdown") + +} + +func signalContext(ctx context.Context) context.Context { + ctx, cancel := context.WithCancel(ctx) + + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + go func() { + log.Println("listening for shutdown signal") + <-sigs + log.Println("shutdown signal received") + signal.Stop(sigs) + close(sigs) + cancel() + }() + + return ctx +} diff --git a/cmd/http/views/error.hbs b/cmd/http/views/error.hbs new file mode 100644 index 0000000..d5330c5 --- /dev/null +++ b/cmd/http/views/error.hbs @@ -0,0 +1,24 @@ + + + + + + + + Error + {{> styles}} + + + +
+
+

Error

+

Unexpected error: {{message}}

+ Go + Back +
+
+ + + \ No newline at end of file diff --git a/cmd/http/views/index.hbs b/cmd/http/views/index.hbs new file mode 100644 index 0000000..09547d9 --- /dev/null +++ b/cmd/http/views/index.hbs @@ -0,0 +1,37 @@ + + + + + + + + Change XMPP Password + {{> styles}} + + + +
+
+

Change Password

+
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+
+ + + diff --git a/cmd/http/views/styles.hbs b/cmd/http/views/styles.hbs new file mode 100644 index 0000000..f76bb92 --- /dev/null +++ b/cmd/http/views/styles.hbs @@ -0,0 +1,63 @@ + diff --git a/cmd/http/views/success.hbs b/cmd/http/views/success.hbs new file mode 100644 index 0000000..0ed34a5 --- /dev/null +++ b/cmd/http/views/success.hbs @@ -0,0 +1,22 @@ + + + + + + + + Success + {{> styles}} + + + +
+
+

Success

+

Password changed successfully!

+ Return Home +
+
+ + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4e4c1e7 --- /dev/null +++ b/go.mod @@ -0,0 +1,33 @@ +module gitea.urkob.com/urko/prosody-password + +go 1.20 + +require ( + gitea.urkob.com/urko/go-root-dir v0.0.0-20230311113851-2f6d4355888a + github.com/gofiber/fiber/v2 v2.47.0 + github.com/gofiber/template/handlebars/v2 v2.1.3 + github.com/joho/godotenv v1.5.1 + github.com/kelseyhightower/envconfig v1.4.0 + github.com/xdg-go/pbkdf2 v1.0.0 +) + +require ( + github.com/andybalholm/brotli v1.0.5 // indirect + github.com/aymerick/raymond v2.0.2+incompatible // indirect + github.com/gofiber/template v1.8.2 // indirect + github.com/gofiber/utils v1.1.0 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/klauspost/compress v1.16.5 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/philhofer/fwd v1.1.2 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect + github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect + github.com/tinylib/msgp v1.1.8 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.47.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/sys v0.9.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2726220 --- /dev/null +++ b/go.sum @@ -0,0 +1,101 @@ +gitea.urkob.com/urko/go-root-dir v0.0.0-20230311113851-2f6d4355888a h1:s73cd3bRR6v0LGiBei841iIolbBJN2tbkUwN54X9vVg= +gitea.urkob.com/urko/go-root-dir v0.0.0-20230311113851-2f6d4355888a/go.mod h1:mU9nRHl70tBhJFbgKotpoXMV+s0wx+1uJ988p4oEpSo= +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0= +github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/gofiber/fiber/v2 v2.47.0 h1:EN5lHVCc+Pyqh5OEsk8fzRiifgwpbrP0rulQ4iNf3fs= +github.com/gofiber/fiber/v2 v2.47.0/go.mod h1:mbFMVN1lQuzziTkkakgtKKdjfsXSw9BKR5lmcNksUoU= +github.com/gofiber/template v1.8.2 h1:PIv9s/7Uq6m+Fm2MDNd20pAFFKt5wWs7ZBd8iV9pWwk= +github.com/gofiber/template v1.8.2/go.mod h1:bs/2n0pSNPOkRa5VJ8zTIvedcI/lEYxzV3+YPXdBvq8= +github.com/gofiber/template/handlebars/v2 v2.1.3 h1:Q1unI+B7gOiFYkcpNynkZ59QPJ6qdObpliVgV8r3bE4= +github.com/gofiber/template/handlebars/v2 v2.1.3/go.mod h1:bb6ip6ZEgBqKSdZcbnFLlfL8PcCRslnX6WgpcxVBiTE= +github.com/gofiber/utils v1.1.0 h1:vdEBpn7AzIUJRhe+CiTOJdUcTg4Q9RK+pEa0KPbLdrM= +github.com/gofiber/utils v1.1.0/go.mod h1:poZpsnhBykfnY1Mc0KeEa6mSHrS3dV0+oBWyeQmb2e0= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= +github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI= +github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= +github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= +github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 h1:rmMl4fXJhKMNWl+K+r/fq4FbbKI+Ia2m9hYBLm2h4G4= +github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94/go.mod h1:90zrgN3D/WJsDd1iXHT96alCoN2KJo6/4x1DZC3wZs8= +github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4= +github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk= +github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/tinylib/msgp v1.1.6/go.mod h1:75BAfg2hauQhs3qedfdDZmWAPcFMAvJE5b9rGOMufyw= +github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0= +github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.47.0 h1:y7moDoxYzMooFpT5aHgNgVOQDrS3qlkfiP9mDtGGK9c= +github.com/valyala/fasthttp v1.47.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= +github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= +golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/internal/api/handler/helper.go b/internal/api/handler/helper.go new file mode 100644 index 0000000..b5c54aa --- /dev/null +++ b/internal/api/handler/helper.go @@ -0,0 +1,25 @@ +package handler + +import ( + "log" + + "github.com/gofiber/fiber/v2" +) + +var defaultErrMessage = "could not process request" + +func RenderError(c *fiber.Ctx, err error, message string) error { + if err != nil { + log.Printf("renderError: %s\n", err) + } + return c.Render("error", fiber.Map{ + "message": message, + }, "") +} + +func JSONError(c *fiber.Ctx, status int, err error, message string) error { + if err != nil { + log.Printf("JSONError: %s\n", err) + } + return c.Status(status).SendString("error: " + message) +} diff --git a/internal/api/handler/prosody_hdl.go b/internal/api/handler/prosody_hdl.go new file mode 100644 index 0000000..af0a1e0 --- /dev/null +++ b/internal/api/handler/prosody_hdl.go @@ -0,0 +1,37 @@ +package handler + +import ( + "fmt" + + "gitea.urkob.com/urko/prosody-password/internal/services/prosody" + "github.com/gofiber/fiber/v2" +) + +func NewProsodyHandler(prosodyService *prosody.Prosody) ProsodyHandler { + return ProsodyHandler{ + prosodyService: prosodyService, + } +} + +type ProsodyHandler struct { + prosodyService *prosody.Prosody +} + +type changePasswordReq struct { + CurrentPassword string `json:"current_password"` + NewPassword string `json:"new_password"` + User string `json:"user"` +} + +func (handler ProsodyHandler) Post(c *fiber.Ctx) error { + req := changePasswordReq{} + if err := c.BodyParser(&req); err != nil { + return RenderError(c, fmt.Errorf("id is empty"), defaultErrMessage) + } + + if err := handler.prosodyService.ChangePassword(req.User, req.CurrentPassword, req.NewPassword); err != nil { + return RenderError(c, fmt.Errorf("ChangePassword: %w", err), defaultErrMessage) + } + + return c.Render("success", fiber.Map{}, "") +} diff --git a/internal/api/server.go b/internal/api/server.go new file mode 100644 index 0000000..e5c5daf --- /dev/null +++ b/internal/api/server.go @@ -0,0 +1,80 @@ +package api + +import ( + "log" + + "gitea.urkob.com/urko/prosody-password/internal/api/handler" + "gitea.urkob.com/urko/prosody-password/internal/services/prosody" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cors" + "github.com/gofiber/template/handlebars/v2" +) + +type RestServer struct { + app *fiber.App + prosodyService *prosody.Prosody +} + +func NewRestServer( + prosodyService *prosody.Prosody, +) *RestServer { + return &RestServer{ + prosodyService: prosodyService, + } +} + +func (s *RestServer) Start(apiPort, views string) error { + engine := handlebars.New(views, ".hbs") + s.app = fiber.New(fiber.Config{ + Views: engine, + }) + + // Or extend your config for customization + s.app.Use(cors.New(cors.Config{ + AllowMethods: "POST,OPTIONS", + AllowOrigins: "*", + AllowHeaders: "Origin, Accept, Content-Type, X-CSRF-Token, Authorization", + ExposeHeaders: "Origin", + })) + + s.loadViews() + + prosodyHdl := handler.NewProsodyHandler(s.prosodyService) + s.app.Post("/changePassword", func(c *fiber.Ctx) error { + return prosodyHdl.Post(c) + }) + + if err := s.app.Listen(":" + apiPort); err != nil { + log.Fatalln("app.Listen:", err) + return err + } + return nil +} + +func (s *RestServer) loadViews() { + s.app.Get("/", func(c *fiber.Ctx) error { + return c.Render("index", fiber.Map{}) + }) + + s.app.Get("/error", func(c *fiber.Ctx) error { + message := c.Query("message") + return renderError(c, nil, message) + }) +} + +func renderError(c *fiber.Ctx, err error, message string) error { + if err != nil { + log.Printf("renderError: %s\n", err) + } + return c.Render("error", fiber.Map{ + "message": message, + }) +} + +func (s *RestServer) Shutdown() error { + if err := s.app.Server().Shutdown(); err != nil { + log.Printf("app.Server().Shutdown(): %s\n", err) + return err + } + return nil +} diff --git a/internal/services/prosody/account.go b/internal/services/prosody/account.go new file mode 100644 index 0000000..2dd969c --- /dev/null +++ b/internal/services/prosody/account.go @@ -0,0 +1,50 @@ +package prosody + +import ( + "log" + "os" + "reflect" + "strings" +) + +type account struct { + salt string `prosody:"salt"` + storedKey string `prosody:"stored_key"` + iterationCount int `prosody:"iteration_count"` +} + +func (acc *account) unmarshal(data map[string]interface{}) { + valueOfPerson := reflect.ValueOf(acc).Elem() + typeOfPerson := valueOfPerson.Type() + for i := 0; i < valueOfPerson.NumField(); i++ { + field := valueOfPerson.Field(i) + tag := typeOfPerson.Field(i).Tag.Get("mytag") + + if val, ok := data[tag]; ok { + field.Set(reflect.ValueOf(val)) + } + } +} + +// loadAccount read the user .dat file and retrieves the data store in it +func (p *Prosody) loadAccount(username string) (*account, error) { + var acc *account + data, err := os.ReadFile(p.accountsPath + username + ".dat") + if err != nil { + return nil, err + } + lines := strings.Split(string(data), "\n") + mapValues := make(map[string]interface{}) + for _, line := range lines { + if strings.Contains(line, "=") { + parts := strings.Split(line, "=") + key := strings.Trim(strings.TrimSpace(parts[0]), "[]\"") + log.Println("key", key) + value := strings.TrimSpace(strings.Trim(parts[1], "\"; ")) + mapValues[key] = value + } + } + + acc.unmarshal(mapValues) + return acc, nil +} diff --git a/internal/services/prosody/change_password.go b/internal/services/prosody/change_password.go new file mode 100644 index 0000000..d2a6d2b --- /dev/null +++ b/internal/services/prosody/change_password.go @@ -0,0 +1,53 @@ +package prosody + +import ( + "crypto/hmac" + "crypto/sha1" + "encoding/hex" + "errors" + "fmt" + "log" + "os/exec" + + "github.com/xdg-go/pbkdf2" +) + +func (p *Prosody) ChangePassword(user string, currentPwd string, newPwd string) error { + acc, err := p.loadAccount(user) + if err != nil { + return fmt.Errorf("p.loadAccount %w", err) + } + + storedKey, err := hashPassword(currentPwd, acc.salt, acc.iterationCount) + if err != nil { + return fmt.Errorf("hashPassword: %w", err) + } + + // Compare the hashes + if storedKey != acc.storedKey { + return errors.New("password is incorrect") + } + + result, err := exec.Command("/usr/bin/prosodyctl", "-c", "passwd -s 12 -scny 1").Output() + if err != nil { + return fmt.Errorf("prosodcytl: %w", err) + } + + log.Println("string(result)", string(result)) + return nil +} + +func hashPassword(password, salt string, iterationCount int) (string, error) { + // Hash the password using the SCRAM mechanism + saltedPassword := pbkdf2.Key([]byte(password), []byte(salt), iterationCount, 20, sha1.New) + clientKey := hmacSha1(saltedPassword, []byte("Client Key")) + storedKey := sha1.Sum(clientKey) + + return hex.EncodeToString(storedKey[:]), nil +} + +func hmacSha1(key, data []byte) []byte { + mac := hmac.New(sha1.New, key) + mac.Write(data) + return mac.Sum(nil) +} diff --git a/internal/services/prosody/prosody.go b/internal/services/prosody/prosody.go new file mode 100644 index 0000000..e066621 --- /dev/null +++ b/internal/services/prosody/prosody.go @@ -0,0 +1,19 @@ +package prosody + +type Prosody struct { + binPath string + accountsPath string +} + +// /var/lib/prosody/xmpp%%2eurkob%%2ecom/accounts/ +func NewProsody(domain string) *Prosody { + return &Prosody{ + binPath: "/usr/bin/prosodyctl", + accountsPath: "/var/lib/prosody/" + domain + "/accounts/", + } +} + +func (p *Prosody) WithBinPath(binPath string) *Prosody { + p.binPath = binPath + return p +} diff --git a/kit/config/config.go b/kit/config/config.go new file mode 100644 index 0000000..f3cb70e --- /dev/null +++ b/kit/config/config.go @@ -0,0 +1,32 @@ +package config + +import ( + "log" + + "gitea.urkob.com/urko/prosody-password/kit" + "github.com/joho/godotenv" + "github.com/kelseyhightower/envconfig" +) + +type Config struct { + Domain string `required:"true" split_words:"true"` + ApiPort string `required:"false" split_words:"true"` + Views string `required:"false" split_words:"true"` +} + +func NewConfig(envFile string) *Config { + if envFile != "" { + err := godotenv.Load(kit.RootDir() + "/" + envFile) + if err != nil { + log.Fatalln("godotenv.Load:", err) + } + } + + cfg := &Config{} + err := envconfig.Process("", cfg) + if err != nil { + log.Fatalf("envconfig.Process: %s\n", err) + } + + return cfg +} diff --git a/kit/path.go b/kit/path.go new file mode 100644 index 0000000..de08744 --- /dev/null +++ b/kit/path.go @@ -0,0 +1,9 @@ +package kit + +import ( + root_dir "gitea.urkob.com/urko/go-root-dir" +) + +func RootDir() string { + return root_dir.RootDir("prosody-passwords") +}