mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Hex encoding the Data field in the SASL responses made debugging SCRAM more difficult than actually helping. Before: F{"Type":"SASLResponse","Data":"633d655377732c723d4d4d4e4e6d666b536f5862694a68385833466d324f2b4d77787354692f4550753052414157484b7a306b7376336c5747392f4d4a5267504d2c703d616742664b533164383937674b4f4a6d4c7171626c49326b6b4a506f2b58354359516c63473458357657343d"} F{"Type":"SASLInitialResponse","AuthMechanism":"SCRAM-SHA-256","Data":"792c2c6e3d2c723d4d4d4e4e6d666b536f5862694a68385833466d324f2b4d77"} After: F{"Type":"SASLResponse","Data":"c=eSws,r=9dR43UQLL1KbrKKl4/QbxjqgVjZYR9mqnx3rFBiI7R/1pp5oeVYMGhXj,p=b2hmuvTvWn2xN0fclm+O4TwLAarRM8xoHSN7jsKDHAU="} F{"Type":"SASLInitialResponse","AuthMechanism":"SCRAM-SHA-256","Data":"y,,n=,r=9dR43UQLL1KbrKKl4/Qbxjqg"}
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package pgproto3
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
|
|
"github.com/jackc/pgio"
|
|
)
|
|
|
|
type SASLResponse struct {
|
|
Data []byte
|
|
}
|
|
|
|
// Frontend identifies this message as sendable by a PostgreSQL frontend.
|
|
func (*SASLResponse) Frontend() {}
|
|
|
|
// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
|
|
// type identifier and 4 byte message length.
|
|
func (dst *SASLResponse) Decode(src []byte) error {
|
|
*dst = SASLResponse{Data: src}
|
|
return nil
|
|
}
|
|
|
|
// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
|
|
func (src *SASLResponse) Encode(dst []byte) []byte {
|
|
dst = append(dst, 'p')
|
|
dst = pgio.AppendInt32(dst, int32(4+len(src.Data)))
|
|
|
|
dst = append(dst, src.Data...)
|
|
|
|
return dst
|
|
}
|
|
|
|
// MarshalJSON implements encoding/json.Marshaler.
|
|
func (src SASLResponse) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(struct {
|
|
Type string
|
|
Data string
|
|
}{
|
|
Type: "SASLResponse",
|
|
Data: string(src.Data),
|
|
})
|
|
}
|
|
|
|
// UnmarshalJSON implements encoding/json.Unmarshaler.
|
|
func (dst *SASLResponse) UnmarshalJSON(data []byte) error {
|
|
var msg struct {
|
|
Data string
|
|
}
|
|
if err := json.Unmarshal(data, &msg); err != nil {
|
|
return err
|
|
}
|
|
if msg.Data != "" {
|
|
decoded, err := hex.DecodeString(msg.Data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
dst.Data = decoded
|
|
}
|
|
return nil
|
|
}
|