github.com/cyverse/go-irodsclient@v0.13.2/irods/message/auth_plugin_request.go (about)

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  
     6  	"github.com/cyverse/go-irodsclient/irods/common"
     7  	"golang.org/x/xerrors"
     8  )
     9  
    10  // IRODSMessageAuthPluginRequest stores auth plugin request
    11  type IRODSMessageAuthPluginRequest struct {
    12  	XMLName    xml.Name `xml:"authPlugReqInp_PI"`
    13  	AuthScheme string   `xml:"auth_scheme_"`
    14  	Context    string   `xml:"context_"`
    15  }
    16  
    17  // NewIRODSMessageAuthPluginRequest creates a IRODSMessageAuthPluginRequest
    18  func NewIRODSMessageAuthPluginRequest(authScheme string, context string) *IRODSMessageAuthPluginRequest {
    19  	return &IRODSMessageAuthPluginRequest{
    20  		AuthScheme: authScheme,
    21  		Context:    context,
    22  	}
    23  }
    24  
    25  // GetBytes returns byte array
    26  func (msg *IRODSMessageAuthPluginRequest) GetBytes() ([]byte, error) {
    27  	xmlBytes, err := xml.Marshal(msg)
    28  	if err != nil {
    29  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    30  	}
    31  	return xmlBytes, nil
    32  }
    33  
    34  // FromBytes returns struct from bytes
    35  func (msg *IRODSMessageAuthPluginRequest) FromBytes(bytes []byte) error {
    36  	err := xml.Unmarshal(bytes, msg)
    37  	if err != nil {
    38  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    39  	}
    40  	return nil
    41  }
    42  
    43  // GetMessage builds a message
    44  func (msg *IRODSMessageAuthPluginRequest) GetMessage() (*IRODSMessage, error) {
    45  	bytes, err := msg.GetBytes()
    46  	if err != nil {
    47  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    48  	}
    49  
    50  	msgBody := IRODSMessageBody{
    51  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    52  		Message: bytes,
    53  		Error:   nil,
    54  		Bs:      nil,
    55  		IntInfo: int32(common.AUTH_PLUG_REQ_AN),
    56  	}
    57  
    58  	msgHeader, err := msgBody.BuildHeader()
    59  	if err != nil {
    60  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    61  	}
    62  
    63  	return &IRODSMessage{
    64  		Header: msgHeader,
    65  		Body:   &msgBody,
    66  	}, nil
    67  }