github.com/cyverse/go-irodsclient@v0.13.2/irods/message/auth_challenge_response.go (about) 1 package message 2 3 import ( 4 "encoding/base64" 5 "encoding/xml" 6 7 "github.com/cyverse/go-irodsclient/irods/common" 8 "golang.org/x/xerrors" 9 ) 10 11 // IRODSMessageAuthChallengeResponse stores auth challenge 12 type IRODSMessageAuthChallengeResponse struct { 13 XMLName xml.Name `xml:"authRequestOut_PI"` 14 Challenge string `xml:"challenge"` 15 } 16 17 // GetBytes returns byte array 18 func (msg *IRODSMessageAuthChallengeResponse) GetBytes() ([]byte, error) { 19 xmlBytes, err := xml.Marshal(msg) 20 if err != nil { 21 return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err) 22 } 23 return xmlBytes, nil 24 } 25 26 // FromBytes returns struct from bytes 27 func (msg *IRODSMessageAuthChallengeResponse) FromBytes(bytes []byte) error { 28 err := xml.Unmarshal(bytes, msg) 29 if err != nil { 30 return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err) 31 } 32 return nil 33 } 34 35 // GetMessage builds a message 36 func (msg *IRODSMessageAuthChallengeResponse) GetMessage() (*IRODSMessage, error) { 37 bytes, err := msg.GetBytes() 38 if err != nil { 39 return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err) 40 } 41 42 msgBody := IRODSMessageBody{ 43 Type: RODS_MESSAGE_API_REPLY_TYPE, 44 Message: bytes, 45 Error: nil, 46 Bs: nil, 47 IntInfo: int32(common.AUTH_REQUEST_AN), 48 } 49 50 msgHeader, err := msgBody.BuildHeader() 51 if err != nil { 52 return nil, xerrors.Errorf("failed to build header from irods message: %w", err) 53 } 54 55 return &IRODSMessage{ 56 Header: msgHeader, 57 Body: &msgBody, 58 }, nil 59 } 60 61 // FromMessage returns struct from IRODSMessage 62 func (msg *IRODSMessageAuthChallengeResponse) FromMessage(msgIn *IRODSMessage) error { 63 if msgIn.Body == nil { 64 return xerrors.Errorf("empty message body") 65 } 66 67 err := msg.FromBytes(msgIn.Body.Message) 68 if err != nil { 69 return xerrors.Errorf("failed to get irods message from message body") 70 } 71 return nil 72 } 73 74 // GetChallenge returns challenge bytes 75 func (msg *IRODSMessageAuthChallengeResponse) GetChallenge() ([]byte, error) { 76 challengeBytes, err := base64.StdEncoding.DecodeString(msg.Challenge) 77 if err != nil { 78 return nil, xerrors.Errorf("failed to decode an authentication challenge: %w", err) 79 } 80 81 return challengeBytes, nil 82 }