github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/security/scram_client.go (about) 1 // Copyright 2021 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package security 15 16 import ( 17 "crypto/sha256" 18 "crypto/sha512" 19 "hash" 20 21 "github.com/xdg/scram" 22 ) 23 24 var ( 25 // SHA256 func 26 SHA256 scram.HashGeneratorFcn = func() hash.Hash { return sha256.New() } 27 // SHA512 func 28 SHA512 scram.HashGeneratorFcn = func() hash.Hash { return sha512.New() } 29 ) 30 31 // XDGSCRAMClient xdg scram client 32 type XDGSCRAMClient struct { 33 *scram.Client 34 *scram.ClientConversation 35 scram.HashGeneratorFcn 36 } 37 38 // Begin xdg scram client Begin 39 func (x *XDGSCRAMClient) Begin(userName, password, authzID string) (err error) { 40 x.Client, err = x.HashGeneratorFcn.NewClient(userName, password, authzID) 41 if err != nil { 42 return err 43 } 44 x.ClientConversation = x.Client.NewConversation() 45 return nil 46 } 47 48 // Step xdg scram client Step 49 func (x *XDGSCRAMClient) Step(challenge string) (response string, err error) { 50 response, err = x.ClientConversation.Step(challenge) 51 return 52 } 53 54 // Done xdg scram client Done 55 func (x *XDGSCRAMClient) Done() bool { 56 return x.ClientConversation.Done() 57 }