-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add interceptors: selector, auth - Remove: extraction from payload - Fix docker compose depends name
- Loading branch information
Showing
24 changed files
with
410 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package interceptors | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/go-faster/errors" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"taskem-server/internal/pkg/jwt" | ||
"taskem-server/internal/repositories/token" | ||
) | ||
|
||
const TokenKey = "token" | ||
const TokenPayload = "tokenPayload" | ||
|
||
// Auth get token from grpc request metadata | ||
// | ||
// Already throws formated grpc with status.Errorf | ||
func (i *Interceptor) Auth(ctx context.Context) (context.Context, error) { | ||
tokenMd, err := auth.AuthFromMD(ctx, "bearer") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
claims, err := jwt.GetPayload(tokenMd, i.c.TokenSecret) | ||
|
||
if err != nil { | ||
switch { | ||
case errors.Is(err, jwt.ErrTokenExpired): | ||
return nil, status.Errorf(codes.Unauthenticated, "Token expired") | ||
case errors.Is(err, jwt.ErrTokenParse): | ||
return nil, status.Errorf(codes.InvalidArgument, "Token parse error") | ||
case errors.Is(err, jwt.ErrTokenValidation): | ||
return nil, status.Errorf(codes.Unauthenticated, "Token validation error") | ||
default: | ||
return nil, status.Errorf(codes.Internal, "Internal error") | ||
} | ||
} | ||
|
||
payload := *claims | ||
|
||
_, err = i.tokenRepo.GetToken(ctx, fmt.Sprintf("%s:%s", payload["type"].(string), payload["uid"].(string))) | ||
if err != nil { | ||
switch { | ||
case errors.Is(err, token.ErrNotFound): | ||
return nil, status.Errorf(codes.Unauthenticated, "Wrong token") | ||
default: | ||
return nil, status.Errorf(codes.Internal, "Internal error") | ||
} | ||
} | ||
|
||
ctx = logging.InjectFields(ctx, logging.Fields{"auth.sub", payload}) | ||
ctx = context.WithValue(ctx, TokenKey, tokenMd) | ||
ctx = context.WithValue(ctx, TokenPayload, payload) | ||
|
||
return ctx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package interceptors | ||
|
||
import ( | ||
"go.uber.org/fx" | ||
"taskem-server/internal/config" | ||
"taskem-server/internal/repositories/token" | ||
) | ||
|
||
type Opts struct { | ||
fx.In | ||
Config config.Config | ||
TokenRepo token.Repository | ||
} | ||
|
||
type Interceptor struct { | ||
c config.Config | ||
tokenRepo token.Repository | ||
} | ||
|
||
func New(opts Opts) *Interceptor { | ||
return &Interceptor{ | ||
c: opts.Config, | ||
tokenRepo: opts.TokenRepo, | ||
} | ||
} |
Oops, something went wrong.