Skip to content

Commit

Permalink
feat: 增加 token 续期
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeEirc committed Mar 30, 2022
1 parent cfe4791 commit 69a6b3d
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 343 deletions.
4 changes: 2 additions & 2 deletions cmd/impl/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type FakeServer struct {
}

func (f *FakeServer) GetDBTokenAuthInfo(ctx context.Context,
req *pb.DBTokenRequest) (*pb.DBTokenResponse, error) {
req *pb.TokenRequest) (*pb.DBTokenResponse, error) {
logger.Infof("Get DB Token AuthInfo req: %+v", req)
var status pb.Status
if f.testData.ID != req.Token {
Expand All @@ -36,7 +36,7 @@ func (f *FakeServer) GetDBTokenAuthInfo(ctx context.Context,
}
status.Ok = true
data := f.testData
dbTokenInfo := pb.DBTokenAuthInfo{
dbTokenInfo := pb.TokenAuthInfo{
KeyId: data.ID,
SecreteId: data.Secrete,
Application: ConvertToProtobufApplication(data.Application),
Expand Down
24 changes: 22 additions & 2 deletions cmd/impl/jms.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type JMServer struct {
forwardStore *common.ForwardCache
}

func (j *JMServer) GetDBTokenAuthInfo(ctx context.Context, req *pb.DBTokenRequest) (*pb.DBTokenResponse, error) {
func (j *JMServer) GetDBTokenAuthInfo(ctx context.Context, req *pb.TokenRequest) (*pb.DBTokenResponse, error) {
var status pb.Status
tokenResp, err := j.apiClient.GetConnectTokenAuth(req.Token)
if err != nil {
Expand All @@ -53,7 +53,7 @@ func (j *JMServer) GetDBTokenAuthInfo(ctx context.Context, req *pb.DBTokenReques
return &pb.DBTokenResponse{Status: &status}, nil
}
setting := j.uploader.GetTerminalSetting()
dbTokenInfo := pb.DBTokenAuthInfo{
dbTokenInfo := pb.TokenAuthInfo{
KeyId: tokenAuthInfo.Id,
SecreteId: tokenAuthInfo.Secret,
Application: ConvertToProtobufApplication(tokenAuthInfo.Application),
Expand All @@ -70,6 +70,26 @@ func (j *JMServer) GetDBTokenAuthInfo(ctx context.Context, req *pb.DBTokenReques
return &pb.DBTokenResponse{Status: &status, Data: &dbTokenInfo}, nil
}

func (j *JMServer) RenewToken(ctx context.Context, req *pb.TokenRequest) (*pb.StatusResponse, error) {
var status pb.Status
res, err := j.apiClient.RenewalToken(req.Token)
if err != nil {
status.Err = err.Error()
if res.Msg != "" {
status.Err = res.Msg
}
logger.Errorf("Renew token %s failed: %s", req.Token, err)
return &pb.StatusResponse{Status: &status}, nil
}
logger.Debugf("Renew token %s: %+v", req.Token, res)
status.Ok = res.Ok
if !res.Ok {
status.Err = res.Msg
logger.Infof("Renew token %s failed: %s", req.Token, res.Msg)
}
return &pb.StatusResponse{Status: &status}, nil
}

func (j *JMServer) CreateSession(ctx context.Context, req *pb.SessionCreateRequest) (*pb.SessionCreateResponse, error) {
var (
status pb.Status
Expand Down
2 changes: 1 addition & 1 deletion pkg/jms-sdk-go/httplib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c *Client) Do(method, reqUrl string, data, res interface{}, params ...map[
case strings.Contains(resp.Header.Get("Content-Type"), "application/json"):
err = json.Unmarshal(body, res)
if err != nil {
msg := fmt.Sprintf("%s %s failed, unmarshal '%s' response failed: %s", req.Method, req.URL, body[:12], err)
msg := fmt.Sprintf("%s %s failed, unmarshal '%s' response failed: %s", req.Method, req.URL, body, err)
err = errors.New(msg)
return
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/jms-sdk-go/service/jms_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ func (s *JMService) GetConnectTokenAuth(token string) (resp TokenAuthInfoRespons
return
}

func (s *JMService) RenewalToken(token string) (resp TokenRenewalResponse, err error) {
data := map[string]string{
"token": token,
}
_, err = s.authClient.Patch(TokenRenewalURL, data, &resp)
return
}

type TokenRenewalResponse struct {
Ok bool `json:"ok"`
Msg string `json:"msg"`
}

type TokenAuthInfoResponse struct {
Info TokenAuthInfo
Err []string
Expand Down
1 change: 1 addition & 0 deletions pkg/jms-sdk-go/service/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
AuthMFASelectURL = "/api/v1/authentication/mfa/select/" // 选择 MFA

TokenAuthInfoURL = "/api/v1/authentication/connection-token/secret-info/detail/"
TokenRenewalURL = "/api/v1/authentication/connection-token/renewal/"
)

// Session相关API
Expand Down
146 changes: 73 additions & 73 deletions protobuf-go/protobuf/common.pb.go

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

Loading

0 comments on commit 69a6b3d

Please sign in to comment.