Skip to content

Commit

Permalink
Merge pull request #14 from m-mattia-m/develop
Browse files Browse the repository at this point in the history
parse host, change taraget-host with referer, remove CORS-Origin
  • Loading branch information
m-mattia-m authored Nov 23, 2023
2 parents 947a9df + 33aed59 commit 00419a1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 1 addition & 4 deletions internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ func Router(svc service.Service) *gin.Engine {
r.RedirectTrailingSlash = false

corsPolicy := cors.DefaultConfig()
corsPolicy.AllowOrigins = []string{
viper.GetString("frontend.url"),
fmt.Sprintf("%s://%s:%s", viper.GetString("server.scheme"), viper.GetString("server.domain"), viper.GetString("server.port")),
}
corsPolicy.AllowOrigins = []string{"*"}
corsPolicy.AllowHeaders = append(corsPolicy.AllowHeaders, "Authorization")

r.Use(cors.New(corsPolicy))
Expand Down
4 changes: 2 additions & 2 deletions internal/api/v1/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func (nac *NotificationApiClient) SendNotification(c *gin.Context) {
return
}

host := c.Request.Host
successMessage, err := svc.SendNotification(host, notification)
referer := c.Request.Referer()
successMessage, err := svc.SendNotification(referer, notification)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"message": helper.ValidateErrorResponse(err, "failed to send"),
Expand Down
8 changes: 7 additions & 1 deletion internal/service/domain_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/mailgun/mailgun-go/v4"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"net/url"
"notify/internal/model"
"strings"
"time"
Expand All @@ -22,13 +23,18 @@ func (c *Client) SendNotification(host string, notification model.Notification)
return nil, err
}

validatedHost, err := url.Parse(host)
if err != nil {
return nil, fmt.Errorf("failed to parse host")
}

hosts, err := c.db.ListHosts(model.Host{ProjectId: notification.ProjectId})
if err != nil {
log.Error(err)
return nil, fmt.Errorf("failed to verify host")
}

if index := ifHostInHosts(host, hosts); hosts == nil || index == -1 || !hosts[index].Verified {
if index := ifHostInHosts(validatedHost.Host, hosts); hosts == nil || index == -1 || !hosts[index].Verified {
return nil, fmt.Errorf("failed to verify host")
}

Expand Down

0 comments on commit 00419a1

Please sign in to comment.