You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/rpc/auth/auth.go

115 lines
3.8 KiB

3 years ago
package auth
import (
"context"
2 years ago
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
pbAuth "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/auth"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msggateway"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
3 years ago
"google.golang.org/grpc"
)
2 years ago
type authServer struct {
2 years ago
authDatabase controller.AuthDatabase
2 years ago
userRpcClient *rpcclient.UserClient
2 years ago
RegisterCenter discoveryregistry.SvcDiscoveryRegistry
2 years ago
}
2 years ago
func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error {
2 years ago
rdb, err := cache.NewRedis()
if err != nil {
return err
}
pbAuth.RegisterAuthServer(server, &authServer{
2 years ago
userRpcClient: rpcclient.NewUserClient(client),
RegisterCenter: client,
2 years ago
authDatabase: controller.NewAuthDatabase(cache.NewMsgCacheModel(rdb), config.Config.TokenPolicy.AccessSecret, config.Config.TokenPolicy.AccessExpire),
})
return nil
}
func (s *authServer) UserToken(ctx context.Context, req *pbAuth.UserTokenReq) (*pbAuth.UserTokenResp, error) {
resp := pbAuth.UserTokenResp{}
2 years ago
if _, err := s.userRpcClient.GetUserInfo(ctx, req.UserID); err != nil {
return nil, err
3 years ago
}
2 years ago
token, err := s.authDatabase.CreateToken(ctx, req.UserID, constant.PlatformIDToName(int(req.PlatformID)))
3 years ago
if err != nil {
return nil, err
3 years ago
}
resp.Token = token
resp.ExpireTimeSeconds = config.Config.TokenPolicy.AccessExpire
return &resp, nil
3 years ago
}
func (s *authServer) parseToken(ctx context.Context, tokensString string) (claims *tokenverify.Claims, err error) {
2 years ago
claims, err = tokenverify.GetClaimFromToken(tokensString)
if err != nil {
return nil, utils.Wrap(err, "")
}
2 years ago
m, err := s.authDatabase.GetTokensWithoutError(ctx, claims.UID, claims.Platform)
3 years ago
if err != nil {
return nil, err
3 years ago
}
if len(m) == 0 {
2 years ago
return nil, errs.ErrTokenNotExist.Wrap()
}
if v, ok := m[tokensString]; ok {
switch v {
case constant.NormalToken:
return claims, nil
case constant.KickedToken:
2 years ago
return nil, errs.ErrTokenKicked.Wrap()
default:
2 years ago
return nil, utils.Wrap(errs.ErrTokenUnknown, "")
}
}
2 years ago
return nil, errs.ErrTokenNotExist.Wrap()
3 years ago
}
func (s *authServer) ParseToken(ctx context.Context, req *pbAuth.ParseTokenReq) (resp *pbAuth.ParseTokenResp, err error) {
resp = &pbAuth.ParseTokenResp{}
claims, err := s.parseToken(ctx, req.Token)
if err != nil {
return nil, err
}
resp.UserID = claims.UID
resp.Platform = claims.Platform
resp.ExpireTimeSeconds = claims.ExpiresAt.Unix()
return resp, nil
}
func (s *authServer) ForceLogout(ctx context.Context, req *pbAuth.ForceLogoutReq) (*pbAuth.ForceLogoutResp, error) {
resp := pbAuth.ForceLogoutResp{}
2 years ago
if err := tokenverify.CheckAdmin(ctx); err != nil {
return nil, err
}
2 years ago
if err := s.forceKickOff(ctx, req.UserID, req.PlatformID, mcontext.GetOperationID(ctx)); err != nil {
return nil, err
}
return &resp, nil
}
func (s *authServer) forceKickOff(ctx context.Context, userID string, platformID int32, operationID string) error {
2 years ago
conns, err := s.RegisterCenter.GetConns(ctx, config.Config.RpcRegisterName.OpenImMessageGatewayName)
if err != nil {
return err
}
2 years ago
for _, v := range conns {
2 years ago
client := msggateway.NewMsgGatewayClient(v)
2 years ago
kickReq := &msggateway.KickUserOfflineReq{KickUserIDList: []string{userID}, PlatformID: platformID}
_, err := client.KickUserOffline(ctx, kickReq)
2 years ago
return utils.Wrap(err, "")
}
2 years ago
return errs.ErrInternalServer.Wrap()
2 years ago
}