|
1 | 1 | package saml2 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "compress/flate" |
| 6 | + "crypto" |
| 7 | + "crypto/tls" |
4 | 8 | "crypto/x509" |
5 | 9 | "encoding/base64" |
6 | 10 | "encoding/hex" |
7 | 11 | "encoding/pem" |
| 12 | + "encoding/xml" |
| 13 | + "fmt" |
| 14 | + "io/ioutil" |
| 15 | + "log" |
8 | 16 | "testing" |
9 | 17 |
|
10 | | - "bytes" |
11 | | - "compress/flate" |
12 | | - |
13 | 18 | "github.com/beevik/etree" |
14 | 19 | "github.com/russellhaering/gosaml2/types" |
15 | | - "github.com/russellhaering/goxmldsig" |
16 | | - require "github.com/stretchr/testify/require" |
| 20 | + dsig "github.com/russellhaering/goxmldsig" |
| 21 | + "github.com/stretchr/testify/require" |
17 | 22 | ) |
18 | 23 |
|
| 24 | +var cert tls.Certificate |
| 25 | +var pk crypto.PrivateKey |
| 26 | + |
| 27 | +func init() { |
| 28 | + var err error |
| 29 | + pfx := "./testdata/test" |
| 30 | + cert, err = tls.LoadX509KeyPair(fmt.Sprintf("%s.crt", pfx), fmt.Sprintf("%s.key", pfx)) |
| 31 | + if err != nil { |
| 32 | + log.Fatal(err) |
| 33 | + } |
| 34 | + pk = cert.PrivateKey |
| 35 | +} |
| 36 | + |
| 37 | +func TestDecode(t *testing.T) { |
| 38 | + f, err := ioutil.ReadFile("./testdata/saml.post") |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("could not open test file: %v\n", err) |
| 41 | + } |
| 42 | + decoded := make([]byte, len(f)) |
| 43 | + |
| 44 | + base64.StdEncoding.Decode(decoded, f) |
| 45 | + response := &types.Response{} |
| 46 | + |
| 47 | + err = xml.Unmarshal(decoded, response) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("error decoding test saml: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + ea := response.EncryptedAssertions[0] |
| 53 | + |
| 54 | + k, err := ea.EncryptedKey.DecryptSymmetricKey(&cert) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("could not get symmetric key: %v\n", err) |
| 57 | + } |
| 58 | + |
| 59 | + if k == nil { |
| 60 | + t.Fatalf("no symmetric key") |
| 61 | + } |
| 62 | + |
| 63 | + assertion, err := ea.Decrypt(&cert) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("error decrypting saml data: %v\n", err) |
| 66 | + } |
| 67 | + |
| 68 | + f2, err := ioutil.ReadFile("./testdata/saml.xml") |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("could not read expected output") |
| 71 | + } |
| 72 | + |
| 73 | + expected := &types.Assertion{} |
| 74 | + err = xml.Unmarshal(f2, expected) |
| 75 | + |
| 76 | + require.EqualValues(t, expected, assertion, "decrypted assertion did not match expectation") |
| 77 | +} |
| 78 | + |
19 | 79 | func signResponse(t *testing.T, resp string, sp *SAMLServiceProvider) string { |
20 | 80 | doc := etree.NewDocument() |
21 | 81 | err := doc.ReadFromBytes([]byte(resp)) |
|
0 commit comments