github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/authentication.go (about) 1 package client 2 3 import ( 4 "context" 5 6 "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" 7 "github.com/milvus-io/milvus-sdk-go/v2/internal/utils/crypto" 8 ) 9 10 // CreateCredential create new user and password 11 func (c *GrpcClient) CreateCredential(ctx context.Context, username string, password string) error { 12 if c.Service == nil { 13 return ErrClientNotReady 14 } 15 req := &milvuspb.CreateCredentialRequest{ 16 Username: username, 17 Password: crypto.Base64Encode(password), 18 } 19 resp, err := c.Service.CreateCredential(ctx, req) 20 if err != nil { 21 return err 22 } 23 err = handleRespStatus(resp) 24 if err != nil { 25 return err 26 } 27 return nil 28 } 29 30 // UpdateCredential update password for a user 31 func (c *GrpcClient) UpdateCredential(ctx context.Context, username string, oldPassword string, newPassword string) error { 32 if c.Service == nil { 33 return ErrClientNotReady 34 } 35 req := &milvuspb.UpdateCredentialRequest{ 36 Username: username, 37 OldPassword: crypto.Base64Encode(oldPassword), 38 NewPassword: crypto.Base64Encode(newPassword), 39 } 40 resp, err := c.Service.UpdateCredential(ctx, req) 41 if err != nil { 42 return err 43 } 44 err = handleRespStatus(resp) 45 if err != nil { 46 return err 47 } 48 return nil 49 } 50 51 // DeleteCredential delete a user 52 func (c *GrpcClient) DeleteCredential(ctx context.Context, username string) error { 53 if c.Service == nil { 54 return ErrClientNotReady 55 } 56 req := &milvuspb.DeleteCredentialRequest{ 57 Username: username, 58 } 59 resp, err := c.Service.DeleteCredential(ctx, req) 60 if err != nil { 61 return err 62 } 63 err = handleRespStatus(resp) 64 if err != nil { 65 return err 66 } 67 return nil 68 } 69 70 // ListCredUsers list all usernames 71 func (c *GrpcClient) ListCredUsers(ctx context.Context) ([]string, error) { 72 if c.Service == nil { 73 return nil, ErrClientNotReady 74 } 75 req := &milvuspb.ListCredUsersRequest{} 76 resp, err := c.Service.ListCredUsers(ctx, req) 77 if err != nil { 78 return nil, err 79 } 80 err = handleRespStatus(resp.Status) 81 if err != nil { 82 return nil, err 83 } 84 return resp.Usernames, nil 85 }