Skip to content

Commit

Permalink
feat: implement RefreshToken method for app rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed May 17, 2024
1 parent 7387abb commit 39e3aab
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 45 deletions.
23 changes: 21 additions & 2 deletions internal/rpc/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,31 @@ endStream:
return status.Error(codes.DataLoss, "send message failed")
}

func (s *Server) RefreshToken(ctx context.Context, req *appProto.RefreshTokenRequest) (*appProto.AccessToken, error) {
claims, valid, err := jwt.ParseRefreshToken(req.Token)
if err != nil {
return nil, status.Error(codes.Unauthenticated, err.Error())
} else if !valid || claims.AppCode != GetAuthInfo(ctx).AppCode {
return nil, status.Error(codes.Unauthenticated, "refresh token invalid")
}

accessToken, err := jwt.GenerateAccessToken(claims.ID, claims.UID, claims.AppCode, claims.Payload)
if err != nil {
return nil, status.Error(codes.Internal, "generate access token failed")
}

return &appProto.AccessToken{
AccessToken: accessToken,
Payload: claims.Payload,
}, nil
}

func (s *Server) DestroyToken(ctx context.Context, req *appProto.RefreshTokenRequest) (*emptypb.Empty, error) {
claims, valid, err := jwt.ParseRefreshToken(req.Token)
if err != nil {
return nil, status.Error(codes.Unauthenticated, err.Error())
} else if !valid {
return nil, nil
} else if !valid || claims.AppCode != GetAuthInfo(ctx).AppCode {
return &emptypb.Empty{}, nil
}

if GetAuthInfo(ctx).AppCode != claims.AppCode {
Expand Down
164 changes: 122 additions & 42 deletions internal/rpc/app/appProto/app.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions internal/rpc/app/appProto/app_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/rpc/protos
Submodule protos updated 1 files
+8 −0 app.proto

0 comments on commit 39e3aab

Please sign in to comment.