85 lines
2.5 KiB
Go
85 lines
2.5 KiB
Go
package frame
|
|
|
|
import (
|
|
coder "git.hpds.cc/Component/mq_coder"
|
|
)
|
|
|
|
// AuthenticationFrame is used to authenticate the client,
|
|
// Once the connection is established, the client immediately, sends information
|
|
// to the server, server gets the way to authenticate according to authName and
|
|
// use authPayload to do a authentication.
|
|
//
|
|
// AuthenticationFrame is a coder encoded.
|
|
type AuthenticationFrame struct {
|
|
authName string
|
|
authPayload string
|
|
}
|
|
|
|
// NewAuthenticationFrame creates a new AuthenticationFrame.
|
|
func NewAuthenticationFrame(authName string, authPayload string) *AuthenticationFrame {
|
|
return &AuthenticationFrame{
|
|
authName: authName,
|
|
authPayload: authPayload,
|
|
}
|
|
}
|
|
|
|
// Type returns the type of AuthenticationFrame.
|
|
func (h *AuthenticationFrame) Type() Type {
|
|
return TagOfAuthenticationFrame
|
|
}
|
|
|
|
// Encode encodes AuthenticationFrame to bytes in coder codec.
|
|
func (h *AuthenticationFrame) Encode() []byte {
|
|
// auth
|
|
authNameBlock := coder.NewPrimitivePacketEncoder(byte(TagOfAuthenticationName))
|
|
authNameBlock.SetStringValue(h.authName)
|
|
authPayloadBlock := coder.NewPrimitivePacketEncoder(byte(TagOfAuthenticationPayload))
|
|
authPayloadBlock.SetStringValue(h.authPayload)
|
|
// authentication frame
|
|
authentication := coder.NewNodePacketEncoder(byte(h.Type()))
|
|
authentication.AddPrimitivePacket(authNameBlock)
|
|
authentication.AddPrimitivePacket(authPayloadBlock)
|
|
|
|
return authentication.Encode()
|
|
}
|
|
|
|
// DecodeToAuthenticationFrame decodes coder encoded bytes to AuthenticationFrame.
|
|
func DecodeToAuthenticationFrame(buf []byte) (*AuthenticationFrame, error) {
|
|
node := coder.NodePacket{}
|
|
_, err := coder.DecodeToNodePacket(buf, &node)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
authentication := &AuthenticationFrame{}
|
|
|
|
// auth
|
|
if authNameBlock, ok := node.PrimitivePackets[byte(TagOfAuthenticationName)]; ok {
|
|
authName, err := authNameBlock.ToUTF8String()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
authentication.authName = authName
|
|
}
|
|
if authPayloadBlock, ok := node.PrimitivePackets[byte(TagOfAuthenticationPayload)]; ok {
|
|
authPayload, err := authPayloadBlock.ToUTF8String()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
authentication.authPayload = authPayload
|
|
}
|
|
|
|
return authentication, nil
|
|
}
|
|
|
|
// AuthPayload returns authentication payload.
|
|
func (h *AuthenticationFrame) AuthPayload() string {
|
|
return h.authPayload
|
|
}
|
|
|
|
// AuthName returns authentication name,
|
|
// server finds the mode of authentication in AuthName.
|
|
func (h *AuthenticationFrame) AuthName() string {
|
|
return h.authName
|
|
}
|