Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
Add compression helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Brevoort committed Jul 28, 2015
1 parent 047ffd0 commit 8d0755f
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
30 changes: 30 additions & 0 deletions authnrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ import (
"github.com/RobotsAndPencils/go-saml/util"
)

func ParseCompressedEncodedRequest(b64RequestXML string) (*AuthnRequest, error) {
var authnRequest AuthnRequest
compressedXML, err := base64.StdEncoding.DecodeString(b64RequestXML)
if err != nil {
return nil, err
}
bXML := util.Decompress(compressedXML)

err = xml.Unmarshal(bXML, &authnRequest)
if err != nil {
return nil, err
}

// There is a bug with XML namespaces in Go that's causing XML attributes with colons to not be roundtrip
// marshal and unmarshaled so we'll keep the original string around for validation.
authnRequest.originalString = string(bXML)
return &authnRequest, nil

}

func ParseEncodedRequest(b64RequestXML string) (*AuthnRequest, error) {
authnRequest := AuthnRequest{}
bytesXML, err := base64.StdEncoding.DecodeString(b64RequestXML)
Expand Down Expand Up @@ -227,3 +247,13 @@ func (r *AuthnRequest) EncodedSignedString(privateKeyPath string) (string, error
b64XML := base64.StdEncoding.EncodeToString([]byte(signed))
return b64XML, nil
}

func (r *AuthnRequest) CompressedEncodedSignedString(privateKeyPath string) (string, error) {
signed, err := r.SignedString(privateKeyPath)
if err != nil {
return "", err
}
compressed := util.Compress([]byte(signed))
b64XML := base64.StdEncoding.EncodeToString(compressed)
return b64XML, nil
}
29 changes: 29 additions & 0 deletions authnresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ import (
"github.com/RobotsAndPencils/go-saml/util"
)

func ParseCompressedEncodedResponse(b64ResponseXML string) (*Response, error) {
authnResponse := Response{}
compressedXML, err := base64.StdEncoding.DecodeString(b64ResponseXML)
if err != nil {
return nil, err
}
bXML := util.Decompress(compressedXML)
err = xml.Unmarshal(bXML, &authnResponse)
if err != nil {
return nil, err
}

// There is a bug with XML namespaces in Go that's causing XML attributes with colons to not be roundtrip
// marshal and unmarshaled so we'll keep the original string around for validation.
authnResponse.originalString = string(bXML)
return &authnResponse, nil

}

func ParseEncodedResponse(b64ResponseXML string) (*Response, error) {
response := Response{}
bytesXML, err := base64.StdEncoding.DecodeString(b64ResponseXML)
Expand Down Expand Up @@ -276,6 +295,16 @@ func (r *Response) EncodedSignedString(privateKeyPath string) (string, error) {
return b64XML, nil
}

func (r *Response) CompressedEncodedSignedString(privateKeyPath string) (string, error) {
signed, err := r.SignedString(privateKeyPath)
if err != nil {
return "", err
}
compressed := util.Compress([]byte(signed))
b64XML := base64.StdEncoding.EncodeToString(compressed)
return b64XML, nil
}

// GetAttribute by Name or by FriendlyName. Return blank string if not found
func (r *Response) GetAttribute(name string) string {
for _, attr := range r.Assertion.AttributeStatement.Attributes {
Expand Down
40 changes: 40 additions & 0 deletions util/compress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package util

import (
"bytes"
"compress/flate"
"io"
"strings"
)

func CompressString(in string) string {
buf := new(bytes.Buffer)
compressor, _ := flate.NewWriter(buf, 9)
compressor.Write([]byte(in))
compressor.Close()
return buf.String()
}

func DecompressString(in string) string {
buf := new(bytes.Buffer)
decompressor := flate.NewReader(strings.NewReader(in))
io.Copy(buf, decompressor)
decompressor.Close()
return buf.String()
}

func Compress(in []byte) []byte {
buf := new(bytes.Buffer)
compressor, _ := flate.NewWriter(buf, 9)
compressor.Write(in)
compressor.Close()
return buf.Bytes()
}

func Decompress(in []byte) []byte {
buf := new(bytes.Buffer)
decompressor := flate.NewReader(bytes.NewReader(in))
io.Copy(buf, decompressor)
decompressor.Close()
return buf.Bytes()
}
23 changes: 23 additions & 0 deletions util/compress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCompressString(t *testing.T) {
expected := "This is the test string"
compressed := CompressString(expected)
decompressed := DecompressString(compressed)
assert.Equal(t, expected, decompressed)
assert.True(t, len(compressed) > len(decompressed))
}

func TestCompress(t *testing.T) {
expected := []byte("This is the test string")
compressed := Compress(expected)
decompressed := Decompress(compressed)
assert.Equal(t, expected, decompressed)
assert.True(t, len(compressed) > len(decompressed))
}

0 comments on commit 8d0755f

Please sign in to comment.