Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eminetto committed Apr 7, 2023
1 parent 4fda4c8 commit 2d15818
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 38 deletions.
2 changes: 1 addition & 1 deletion feedbacks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/eminetto/api-o11y/feedbacks
go 1.20

require (
github.com/eminetto/api-o11y v0.0.5 // indirect
github.com/eminetto/api-o11y v0.0.6 // indirect
github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/go-chi/httplog v0.3.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions feedbacks/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/eminetto/api-o11y v0.0.4 h1:x+AeLWVSPBrFFlkvp/AV0IMxWBMcKHw0aHZoz9Tfa
github.com/eminetto/api-o11y v0.0.4/go.mod h1:rwcGFE85b639qzTaL13QJR+n5XAmVdKBub6cbzQxsjI=
github.com/eminetto/api-o11y v0.0.5 h1:lX06Px3mZTx0z9o/XH6BnGOjyGy3cs6eqgFajn7v4Is=
github.com/eminetto/api-o11y v0.0.5/go.mod h1:hPKUkRKkl/cuHHsA0mZ6rdJny7o5KiG9dteD8EuFr6g=
github.com/eminetto/api-o11y v0.0.6 h1:PLQa2GsP592Cz7e1Sl6wZnYcoVCP/hdMRTVJTIXQHuo=
github.com/eminetto/api-o11y v0.0.6/go.mod h1:hPKUkRKkl/cuHHsA0mZ6rdJny7o5KiG9dteD8EuFr6g=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
Expand Down
2 changes: 1 addition & 1 deletion feedbacks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {

r := chi.NewRouter()
r.Use(httplog.RequestLogger(logger))
r.Use(middleware.IsAuthenticated)
r.Use(middleware.IsAuthenticated(ctx, otel))
r.Post("/v1/feedback", storeFeedback(ctx, fService, otel))

http.Handle("/", r)
Expand Down
77 changes: 42 additions & 35 deletions internal/middleware/is_authenticated.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,52 @@ import (
"strings"
)

func IsAuthenticated(ctx context.Context, telemetry telemetry.Telemetry, next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
ctx, span := telemetry.Start(ctx, "IsAuthenticated")
defer span.End()
errorMessage := "Erro na autenticação"
tokenString := r.Header.Get("Authorization")
if tokenString == "" {
err := errors.New("Unauthorized")
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
respondWithError(rw, http.StatusUnauthorized, err.Error(), errorMessage)
return
}
payload := `{
func IsAuthenticated(ctx context.Context, telemetry telemetry.Telemetry) func(next http.Handler) http.Handler {
return Handler(ctx, telemetry)
}

func Handler(ctx context.Context, telemetry telemetry.Telemetry) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(rw http.ResponseWriter, r *http.Request) {
ctx, span := telemetry.Start(ctx, "IsAuthenticated")
defer span.End()
errorMessage := "Erro na autenticação"
tokenString := r.Header.Get("Authorization")
if tokenString == "" {
err := errors.New("Unauthorized")
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
respondWithError(rw, http.StatusUnauthorized, err.Error(), errorMessage)
return
}
payload := `{
"token": "` + tokenString + `"
}`

req, err := http.Post(os.Getenv("AUTH_URL")+"/v1/validate-token", "text/plain", strings.NewReader(payload))
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
respondWithError(rw, http.StatusUnauthorized, err.Error(), errorMessage)
return
}
defer req.Body.Close()
type result struct {
Email string `json:"email"`
}
var res result
err = json.NewDecoder(req.Body).Decode(&res)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
respondWithError(rw, http.StatusUnauthorized, err.Error(), errorMessage)
return
req, err := http.Post(os.Getenv("AUTH_URL")+"/v1/validate-token", "text/plain", strings.NewReader(payload))
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
respondWithError(rw, http.StatusUnauthorized, err.Error(), errorMessage)
return
}
defer req.Body.Close()
type result struct {
Email string `json:"email"`
}
var res result
err = json.NewDecoder(req.Body).Decode(&res)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
respondWithError(rw, http.StatusUnauthorized, err.Error(), errorMessage)
return
}
newCTX := context.WithValue(ctx, "email", res.Email)
next.ServeHTTP(rw, r.WithContext(newCTX))
}
newCTX := context.WithValue(ctx, "email", res.Email)
next.ServeHTTP(rw, r.WithContext(newCTX))
})
return http.HandlerFunc(fn)
}
}

// RespondWithError return a http error
Expand Down
2 changes: 1 addition & 1 deletion votes/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/eminetto/api-o11y/votes
go 1.20

require (
github.com/eminetto/api-o11y v0.0.5 // indirect
github.com/eminetto/api-o11y v0.0.6 // indirect
github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/go-chi/httplog v0.3.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions votes/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/eminetto/api-o11y v0.0.4 h1:x+AeLWVSPBrFFlkvp/AV0IMxWBMcKHw0aHZoz9Tfa
github.com/eminetto/api-o11y v0.0.4/go.mod h1:rwcGFE85b639qzTaL13QJR+n5XAmVdKBub6cbzQxsjI=
github.com/eminetto/api-o11y v0.0.5 h1:lX06Px3mZTx0z9o/XH6BnGOjyGy3cs6eqgFajn7v4Is=
github.com/eminetto/api-o11y v0.0.5/go.mod h1:hPKUkRKkl/cuHHsA0mZ6rdJny7o5KiG9dteD8EuFr6g=
github.com/eminetto/api-o11y v0.0.6 h1:PLQa2GsP592Cz7e1Sl6wZnYcoVCP/hdMRTVJTIXQHuo=
github.com/eminetto/api-o11y v0.0.6/go.mod h1:hPKUkRKkl/cuHHsA0mZ6rdJny7o5KiG9dteD8EuFr6g=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
Expand Down

0 comments on commit 2d15818

Please sign in to comment.