-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathenvelopedsignature.go
81 lines (70 loc) · 2.13 KB
/
envelopedsignature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package signedxml
import (
"errors"
"strings"
"github.com/beevik/etree"
)
// EnvelopedSignature implements the CanonicalizationAlgorithm
// interface and is used for processing the
// http://www.w3.org/2000/09/xmldsig#enveloped-signature transform
// algorithm
type EnvelopedSignature struct{}
// see CanonicalizationAlgorithm.ProcessElement
func (e EnvelopedSignature) ProcessElement(inputXML *etree.Element, transformXML string) (outputXML string, err error) {
transformedXML, err := e.processElement(inputXML.Copy(), transformXML)
if err != nil {
return "", err
}
doc := etree.NewDocument()
doc.SetRoot(transformedXML)
docString, err := doc.WriteToString()
if err != nil {
return "", err
}
//logger.Println(docString)
return docString, nil
}
// see CanonicalizationAlgorithm.ProcessDocument
func (e EnvelopedSignature) ProcessDocument(doc *etree.Document,
transformXML string) (outputXML string, err error) {
transformedRoot, err := e.processElement(doc.Root().Copy(), transformXML)
if err != nil {
return "", err
}
doc.SetRoot(transformedRoot)
docString, err := doc.WriteToString()
if err != nil {
return "", err
}
//logger.Println(docString)
return docString, nil
}
// see CanonicalizationAlgorithm.Process
func (e EnvelopedSignature) Process(inputXML string, transformXML string) (outputXML string, err error) {
doc := etree.NewDocument()
err = doc.ReadFromString(inputXML)
if err != nil {
return "", err
}
return e.ProcessDocument(doc, transformXML)
}
func (e EnvelopedSignature) processElement(inputXML *etree.Element, transformXML string) (outputXML *etree.Element, err error) {
sig := inputXML.FindElement("//Signature")
if sig == nil {
// TODO(adam): Why can't ./Signature (or /Signature, or //Signature) find the root Signature element?
if strings.EqualFold(inputXML.Tag, "Signature") {
sig = inputXML
}
}
if sig == nil {
return nil, errors.New("signedxml: unable to find Signature node")
}
sigParent := sig.Parent()
if sigParent != nil {
elem := sigParent.RemoveChild(sig)
if elem == nil {
return nil, errors.New("signedxml: unable to remove Signature element")
}
}
return inputXML, nil
}