From 0e0bf51428863320b775db302c2c1d8a14d91fce Mon Sep 17 00:00:00 2001 From: Mathieu Mailhos Date: Sat, 1 Feb 2020 02:58:15 +1100 Subject: [PATCH] feat(slo): add Bytes() and Deflate() functions for LogoutRequest (#251) Bytes() is needed in for returning a byte array for POST Binding. Deflate() is needed for compressing using gzip algorithm a LogoutRequest byte array. --- schema.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/schema.go b/schema.go index bb4651a9..f66bff48 100644 --- a/schema.go +++ b/schema.go @@ -1,6 +1,8 @@ package saml import ( + "bytes" + "compress/flate" "encoding/xml" "strconv" "time" @@ -112,6 +114,43 @@ func (r *LogoutRequest) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err return nil } +// Bytes returns a byte array representation of the LogoutRequest +func (r *LogoutRequest) Bytes() ([]byte, error) { + doc := etree.NewDocument() + doc.SetRoot(r.Element()) + + buf, err := doc.WriteToBytes() + if err != nil { + return nil, err + } + + return buf, nil +} + +// Deflate returns a compressed byte array of the LogoutRequest +func (r *LogoutRequest) Deflate() ([]byte, error) { + buf, err := r.Bytes() + if err != nil { + return nil, err + } + + var b bytes.Buffer + writer, err := flate.NewWriter(&b, flate.DefaultCompression) + if err != nil { + return nil, err + } + + if _, err := writer.Write(buf); err != nil { + return nil, err + } + + if err := writer.Close(); err != nil { + return nil, err + } + + return b.Bytes(), nil +} + // Element returns an etree.Element representing the object // Element returns an etree.Element representing the object in XML form. func (r *AuthnRequest) Element() *etree.Element {