Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## [2.8.0-beta.1](https://github.com/LerianStudio/lib-auth/compare/v2.7.0...v2.8.0-beta.1) (2026-05-18)

Comment thread
qnen marked this conversation as resolved.
## [2.7.0](https://github.com/LerianStudio/lib-auth/compare/v2.6.0...v2.7.0) (2026-04-27)


Expand Down
54 changes: 27 additions & 27 deletions auth/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"strings"
"time"

"github.com/LerianStudio/lib-commons/v5/commons/log"
"github.com/LerianStudio/lib-commons/v5/commons/opentelemetry"
"github.com/LerianStudio/lib-commons/v5/commons/zap"
"github.com/LerianStudio/lib-observability/log"
"github.com/LerianStudio/lib-observability/tracing"
"github.com/LerianStudio/lib-observability/zap"
"go.opentelemetry.io/otel/attribute"

"github.com/LerianStudio/lib-commons/v5/commons"
Expand Down Expand Up @@ -214,7 +214,7 @@ func NewAuthClient(address string, enabled bool, logger *log.Logger) *AuthClient
// If the user is authorized, the request is passed to the next handler; otherwise, a 403 Forbidden status is returned.
func (auth *AuthClient) Authorize(sub, resource, action string) fiber.Handler {
return func(c *fiber.Ctx) error {
ctx := opentelemetry.ExtractHTTPContext(c.UserContext(), c)
ctx := tracing.ExtractHTTPContext(c.UserContext(), c)

_, tracer, reqID, _ := commons.NewTrackingFromContext(ctx)

Expand Down Expand Up @@ -276,7 +276,7 @@ func (auth *AuthClient) checkAuthorization(ctx context.Context, sub, resource, a
if err != nil {
logErrorf(ctx, auth.Logger, "Failed to parse token: %v", err)

opentelemetry.HandleSpanError(span, "Failed to parse token", err)
tracing.HandleSpanError(span, "Failed to parse token", err)

return false, http.StatusUnauthorized, err
}
Expand All @@ -287,7 +287,7 @@ func (auth *AuthClient) checkAuthorization(ctx context.Context, sub, resource, a

err := errors.New("token claims are not in the expected format")

opentelemetry.HandleSpanError(span, "Failed to parse claims", err)
tracing.HandleSpanError(span, "Failed to parse claims", err)

return false, http.StatusUnauthorized, err
}
Expand All @@ -303,7 +303,7 @@ func (auth *AuthClient) checkAuthorization(ctx context.Context, sub, resource, a

err := errors.New("missing owner claim in token")

opentelemetry.HandleSpanError(span, "Missing owner claim in token", err)
tracing.HandleSpanError(span, "Missing owner claim in token", err)

return false, http.StatusUnauthorized, err
}
Expand All @@ -318,9 +318,9 @@ func (auth *AuthClient) checkAuthorization(ctx context.Context, sub, resource, a
"action": action,
}

err = opentelemetry.SetSpanAttributesFromValue(span, "app.request.payload", requestBody, nil)
err = tracing.SetSpanAttributesFromValue(span, "app.request.payload", requestBody, nil)
if err != nil {
opentelemetry.HandleSpanError(span, "Failed to convert request body to JSON string", err)
tracing.HandleSpanError(span, "Failed to convert request body to JSON string", err)

return false, http.StatusInternalServerError, err
}
Expand All @@ -329,7 +329,7 @@ func (auth *AuthClient) checkAuthorization(ctx context.Context, sub, resource, a
if err != nil {
logErrorf(ctx, auth.Logger, "Failed to marshal request body: %v", err)

opentelemetry.HandleSpanError(span, "Failed to marshal request body", err)
tracing.HandleSpanError(span, "Failed to marshal request body", err)

return false, http.StatusInternalServerError, err
}
Expand All @@ -338,12 +338,12 @@ func (auth *AuthClient) checkAuthorization(ctx context.Context, sub, resource, a
if err != nil {
logErrorf(ctx, auth.Logger, "Failed to create request: %v", err)

opentelemetry.HandleSpanError(span, "Failed to create request", err)
tracing.HandleSpanError(span, "Failed to create request", err)

return false, http.StatusInternalServerError, fmt.Errorf("failed to create request: %w", err)
}

opentelemetry.InjectHTTPContext(ctx, req.Header)
tracing.InjectHTTPContext(ctx, req.Header)

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", accessToken)
Expand All @@ -352,7 +352,7 @@ func (auth *AuthClient) checkAuthorization(ctx context.Context, sub, resource, a
if err != nil {
logErrorf(ctx, auth.Logger, "Failed to make request: %v", err)

opentelemetry.HandleSpanError(span, "Failed to make request", err)
tracing.HandleSpanError(span, "Failed to make request", err)

return false, http.StatusInternalServerError, fmt.Errorf("failed to make request: %w", err)
}
Expand All @@ -362,7 +362,7 @@ func (auth *AuthClient) checkAuthorization(ctx context.Context, sub, resource, a
if err != nil {
logErrorf(ctx, auth.Logger, "Failed to read response body: %v", err)

opentelemetry.HandleSpanError(span, "Failed to read response body", err)
tracing.HandleSpanError(span, "Failed to read response body", err)

return false, http.StatusInternalServerError, fmt.Errorf("failed to read response body: %w", err)
}
Expand All @@ -371,15 +371,15 @@ func (auth *AuthClient) checkAuthorization(ctx context.Context, sub, resource, a
if err != nil {
logErrorf(ctx, auth.Logger, "Failed to unmarshal auth error response: %v", err)

opentelemetry.HandleSpanError(span, "Failed to unmarshal auth error response", err)
tracing.HandleSpanError(span, "Failed to unmarshal auth error response", err)

return false, http.StatusInternalServerError, fmt.Errorf("failed to unmarshal auth error response: %w", err)
}

if respError.Code != "" && resp.StatusCode != http.StatusInternalServerError {
logErrorf(ctx, auth.Logger, "Authorization request failed: %s", respError.Message)

opentelemetry.HandleSpanError(span, "Authorization request failed", respError)
tracing.HandleSpanError(span, "Authorization request failed", respError)

return false, resp.StatusCode, respError
}
Expand All @@ -388,7 +388,7 @@ func (auth *AuthClient) checkAuthorization(ctx context.Context, sub, resource, a
if err := json.Unmarshal(body, &response); err != nil {
logErrorf(ctx, auth.Logger, "Failed to unmarshal response: %v", err)

opentelemetry.HandleSpanError(span, "Failed to unmarshal response", err)
tracing.HandleSpanError(span, "Failed to unmarshal response", err)

return false, http.StatusInternalServerError, fmt.Errorf("failed to unmarshal response: %w", err)
}
Expand Down Expand Up @@ -421,9 +421,9 @@ func (auth *AuthClient) GetApplicationToken(ctx context.Context, clientID, clien
"clientSecret": clientSecret,
}

err := opentelemetry.SetSpanAttributesFromValue(span, "app.request.payload", requestBody, nil)
err := tracing.SetSpanAttributesFromValue(span, "app.request.payload", requestBody, nil)
if err != nil {
opentelemetry.HandleSpanError(span, "Failed to convert request body to JSON string", err)
tracing.HandleSpanError(span, "Failed to convert request body to JSON string", err)

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
return "", fmt.Errorf("failed to convert request body to JSON string: %w", err)
}
Expand All @@ -432,7 +432,7 @@ func (auth *AuthClient) GetApplicationToken(ctx context.Context, clientID, clien
if err != nil {
logErrorf(ctx, auth.Logger, "Failed to marshal request body: %v", err)

opentelemetry.HandleSpanError(span, "Failed to marshal request body", err)
tracing.HandleSpanError(span, "Failed to marshal request body", err)

return "", fmt.Errorf("failed to marshal request body: %w", err)
}
Expand All @@ -441,20 +441,20 @@ func (auth *AuthClient) GetApplicationToken(ctx context.Context, clientID, clien
if err != nil {
logErrorf(ctx, auth.Logger, "Failed to create request: %v", err)

opentelemetry.HandleSpanError(span, "Failed to create request", err)
tracing.HandleSpanError(span, "Failed to create request", err)

return "", fmt.Errorf("failed to create request: %w", err)
}

opentelemetry.InjectHTTPContext(ctx, req.Header)
tracing.InjectHTTPContext(ctx, req.Header)

req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
logErrorf(ctx, auth.Logger, "Failed to make request: %v", err)

opentelemetry.HandleSpanError(span, "Failed to make request", err)
tracing.HandleSpanError(span, "Failed to make request", err)

return "", fmt.Errorf("failed to make request: %w", err)
}
Expand All @@ -464,7 +464,7 @@ func (auth *AuthClient) GetApplicationToken(ctx context.Context, clientID, clien
if err != nil {
logErrorf(ctx, auth.Logger, "Failed to read response body: %v", err)

opentelemetry.HandleSpanError(span, "Failed to read response body", err)
tracing.HandleSpanError(span, "Failed to read response body", err)

return "", fmt.Errorf("failed to read response body: %w", err)
}
Expand All @@ -473,15 +473,15 @@ func (auth *AuthClient) GetApplicationToken(ctx context.Context, clientID, clien
if err != nil {
logErrorf(ctx, auth.Logger, "Failed to unmarshal auth error response: %v", err)

opentelemetry.HandleSpanError(span, "Failed to unmarshal auth error response", err)
tracing.HandleSpanError(span, "Failed to unmarshal auth error response", err)

return "", fmt.Errorf("failed to unmarshal auth error response: %w", err)
}

if respError.Code != "" && resp.StatusCode != http.StatusInternalServerError {
logErrorf(ctx, auth.Logger, "Failed to get application token: %s", respError.Message)

opentelemetry.HandleSpanError(span, "Failed to get application token", respError)
tracing.HandleSpanError(span, "Failed to get application token", respError)

return "", respError
}
Expand All @@ -490,7 +490,7 @@ func (auth *AuthClient) GetApplicationToken(ctx context.Context, clientID, clien
if err := json.Unmarshal(body, &response); err != nil {
logErrorf(ctx, auth.Logger, "Failed to unmarshal response: %v", err)

opentelemetry.HandleSpanError(span, "Failed to unmarshal response", err)
tracing.HandleSpanError(span, "Failed to unmarshal response", err)

return "", fmt.Errorf("failed to unmarshal response: %w", err)
}
Expand Down
10 changes: 5 additions & 5 deletions auth/middleware/middlewareGRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"github.com/LerianStudio/lib-commons/v5/commons"
"github.com/LerianStudio/lib-commons/v5/commons/opentelemetry"
"github.com/LerianStudio/lib-observability/tracing"
jwt "github.com/golang-jwt/jwt/v5"
"go.opentelemetry.io/otel/attribute"
"google.golang.org/grpc"
Expand Down Expand Up @@ -63,7 +63,7 @@ func NewGRPCAuthUnaryPolicy(auth *AuthClient, cfg PolicyConfig) grpc.UnaryServer

pol, found := policyForMethod(cfg, info.FullMethod)
if !found {
opentelemetry.HandleSpanError(span, "no policy configured for method", fmt.Errorf("%s", info.FullMethod))
tracing.HandleSpanError(span, "no policy configured for method", fmt.Errorf("%s", info.FullMethod))

return nil, status.Error(codes.Internal, "internal configuration error")
}
Expand All @@ -75,7 +75,7 @@ func NewGRPCAuthUnaryPolicy(auth *AuthClient, cfg PolicyConfig) grpc.UnaryServer

sub, err = cfg.SubResolver(ctx, info.FullMethod, req)
if err != nil {
opentelemetry.HandleSpanError(span, "failed to resolve subject", err)
tracing.HandleSpanError(span, "failed to resolve subject", err)

return nil, status.Error(codes.Internal, "internal configuration error")
}
Expand All @@ -86,8 +86,8 @@ func NewGRPCAuthUnaryPolicy(auth *AuthClient, cfg PolicyConfig) grpc.UnaryServer
"resource": pol.Resource,
"action": pol.Action,
}
if err := opentelemetry.SetSpanAttributesFromValue(span, "app.request.payload", payload, nil); err != nil {
opentelemetry.HandleSpanError(span, "failed to set span payload", err)
if err := tracing.SetSpanAttributesFromValue(span, "app.request.payload", payload, nil); err != nil {
tracing.HandleSpanError(span, "failed to set span payload", err)
}

authorized, httpStatus, err := auth.checkAuthorization(ctx, sub, pol.Resource, pol.Action, token)
Expand Down
2 changes: 1 addition & 1 deletion auth/middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/http/httptest"
"testing"

"github.com/LerianStudio/lib-commons/v5/commons/log"
"github.com/LerianStudio/lib-observability/log"
jwt "github.com/golang-jwt/jwt/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
34 changes: 22 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
module github.com/LerianStudio/lib-auth/v2

go 1.25.9
go 1.26.3

require (
github.com/LerianStudio/lib-commons/v5 v5.0.2
github.com/LerianStudio/lib-commons/v5 v5.2.0-beta.12
github.com/LerianStudio/lib-observability v1.0.0
github.com/gofiber/fiber/v2 v2.52.13
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/stretchr/testify v1.11.1
go.opentelemetry.io/otel v1.43.0
google.golang.org/grpc v1.80.0
google.golang.org/grpc v1.81.0
)

require github.com/google/uuid v1.6.0 // indirect

require (
github.com/andybalholm/brotli v1.2.0 // indirect
github.com/bxcodec/dbresolver/v2 v2.2.1 // indirect
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
Expand All @@ -26,13 +28,15 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.30.1 // indirect
github.com/golang/snappy v1.0.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/klauspost/compress v1.18.5 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.21 // indirect
github.com/montanaflynn/stats v0.8.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/shopspring/decimal v1.4.0 // indirect
Expand All @@ -41,27 +45,33 @@ require (
github.com/tklauser/numcpus v0.11.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.69.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.2.0 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.mongodb.org/mongo-driver v1.17.9 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/bridges/otelzap v0.17.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.18.0 // indirect
go.opentelemetry.io/contrib/bridges/otelzap v0.18.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.43.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.43.0 // indirect
go.opentelemetry.io/otel/log v0.18.0 // indirect
go.opentelemetry.io/otel/log v0.19.0 // indirect
go.opentelemetry.io/otel/metric v1.43.0 // indirect
go.opentelemetry.io/otel/sdk v1.43.0 // indirect
go.opentelemetry.io/otel/sdk/log v0.18.0 // indirect
go.opentelemetry.io/otel/sdk/log v0.19.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.43.0 // indirect
go.opentelemetry.io/otel/trace v1.43.0 // indirect
go.opentelemetry.io/proto/otlp v1.10.0 // indirect
go.uber.org/mock v0.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.1 // indirect
golang.org/x/crypto v0.49.0 // indirect
golang.org/x/net v0.52.0 // indirect
golang.org/x/sys v0.42.0 // indirect
golang.org/x/text v0.35.0 // indirect
go.uber.org/zap v1.28.0 // indirect
golang.org/x/crypto v0.50.0 // indirect
golang.org/x/net v0.53.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.43.0 // indirect
golang.org/x/text v0.36.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9 // indirect
google.golang.org/protobuf v1.36.11 // indirect
Expand Down
Loading
Loading