refactor: unify all service error codes
This commit is contained in:
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -9,11 +10,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type authorization struct {
|
||||
@@ -25,10 +25,10 @@ type authorization struct {
|
||||
func newAuthorization(key string, expireSeconds int64, anonymous []string) (*authorization, error) {
|
||||
key = strings.TrimSpace(key)
|
||||
if key == "" {
|
||||
return nil, errors.New("authorization key must not be empty")
|
||||
return nil, errcode.ErrTokenSecretKeyNotFound
|
||||
}
|
||||
if expireSeconds <= 0 {
|
||||
return nil, errors.New("authorization expiration must be greater than zero")
|
||||
return nil, errcode.ErrTokenAuthExpire
|
||||
}
|
||||
allowed := make(map[string]struct{}, len(anonymous))
|
||||
for _, item := range anonymous {
|
||||
@@ -43,10 +43,10 @@ func (a *authorization) unaryInterceptor(ctx context.Context, req any, info *grp
|
||||
if !a.isAnonymous(info.FullMethod) {
|
||||
values := metadata.ValueFromIncomingContext(ctx, "authorization")
|
||||
if len(values) == 0 {
|
||||
return nil, status.Error(codes.Unauthenticated, "authorization is required")
|
||||
return nil, errcode.ErrHeaderAuthorization
|
||||
}
|
||||
if err := a.validate(values[0]); err != nil {
|
||||
return nil, status.Error(codes.Unauthenticated, err.Error())
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return handler(ctx, req)
|
||||
@@ -68,7 +68,7 @@ func (a *authorization) httpMiddleware(next http.Handler) http.Handler {
|
||||
func (a *authorization) validate(raw string) error {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return errors.New("authorization is required")
|
||||
return errcode.ErrHeaderAuthorization
|
||||
}
|
||||
claims := &jwt.RegisteredClaims{}
|
||||
tokenValue, err := jwt.ParseWithClaims(raw, claims, func(tokenValue *jwt.Token) (any, error) {
|
||||
@@ -78,14 +78,17 @@ func (a *authorization) validate(raw string) error {
|
||||
return a.key, nil
|
||||
}, jwt.WithExpirationRequired(), jwt.WithIssuedAt(), jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))
|
||||
if err != nil || !tokenValue.Valid {
|
||||
return errors.New("authorization is invalid or expired")
|
||||
if errors.Is(err, jwt.ErrTokenExpired) {
|
||||
return errcode.ErrTokenAuthExpire
|
||||
}
|
||||
return errcode.ErrTokenAuthParseFail
|
||||
}
|
||||
if claims.IssuedAt == nil {
|
||||
return errors.New("authorization issued-at is required")
|
||||
return errcode.ErrTokenDataInvalid
|
||||
}
|
||||
now := time.Now()
|
||||
if claims.IssuedAt.Time.After(now) || now.Sub(claims.IssuedAt.Time) > a.expire {
|
||||
return errors.New("authorization is expired")
|
||||
return errcode.ErrTokenAuthExpire
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -119,5 +122,5 @@ func normalizePath(value string) string {
|
||||
func writeHTTPAuthorizationError(w http.ResponseWriter, err error) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = fmt.Fprintf(w, `{"code":%d,"message":%q}`, codes.Unauthenticated, err.Error())
|
||||
_ = json.NewEncoder(w).Encode(newErrorResponse(err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user