-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity_token_reference.go
140 lines (115 loc) · 3.11 KB
/
security_token_reference.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package xmlsecurity
import (
"crypto/x509"
"errors"
"github.com/beevik/etree"
"github.com/deb-ict/go-xml"
)
type SecurityTokenReference interface {
xml.Node
X509CertificateProvider
GetId() string
SetId(id string)
GetUsage() string
SetUsage(usage string)
GetTokenType() string
SetTokenType(tokenType string)
GetContent() xml.Node
SetContent(content xml.Node)
}
type securityTokenReference struct {
Id string
Usage string
TokenType string
Content xml.Node
}
func NewSecurityTokenReference(context xml.Context) (SecurityTokenReference, error) {
return &securityTokenReference{}, nil
}
func NewSecurityTokenReferenceNode(context xml.Context) (xml.Node, error) {
return NewSecurityTokenReference(context)
}
func (node *securityTokenReference) GetId() string {
return node.Id
}
func (node *securityTokenReference) SetId(id string) {
node.Id = id
}
func (node *securityTokenReference) GetUsage() string {
return node.Usage
}
func (node *securityTokenReference) SetUsage(usage string) {
node.Usage = usage
}
func (node *securityTokenReference) GetTokenType() string {
return node.TokenType
}
func (node *securityTokenReference) SetTokenType(tokenType string) {
node.TokenType = tokenType
}
func (node *securityTokenReference) GetContent() xml.Node {
return node.Content
}
func (node *securityTokenReference) SetContent(content xml.Node) {
node.Content = content
}
func (node *securityTokenReference) GetX509Certificate(context xml.Context) (*x509.Certificate, error) {
if node.Content == nil {
return nil, errors.New("x509 certificate not available")
}
provider, ok := node.Content.(X509CertificateProvider)
if !ok {
return nil, errors.New("x509 certificate not available")
}
return provider.GetX509Certificate(context)
}
func (node *securityTokenReference) LoadXml(context xml.Context, el *etree.Element) error {
err := xml.ValidateElement(el, "SecurityTokenReference", WsseNamespace)
if err != nil {
return err
}
node.SetId(GetWsuId(context, el))
node.SetUsage(el.SelectAttrValue("Usage", ""))
node.SetTokenType(el.SelectAttrValue("TokenType", ""))
for _, child := range el.ChildElements() {
namespaceUri := context.GetNamespaceUri(child.Space)
typeConstructor, err := context.GetTypeConstructor(namespaceUri, child.Tag)
if err == xml.ErrNoTypeConstructor {
continue
}
if err != nil {
return err
}
content, err := typeConstructor(context)
if err != nil {
return err
}
err = content.LoadXml(context, child)
if err != nil {
return err
}
node.SetContent(content)
}
return nil
}
func (node *securityTokenReference) GetXml(context xml.Context) (*etree.Element, error) {
el := etree.NewElement("SecurityTokenReference")
el.Space = context.GetNamespacePrefix(WsseNamespace)
if node.GetId() != "" {
SetWsuId(context, el, node.GetId())
}
if node.GetUsage() != "" {
el.CreateAttr("Usage", node.GetUsage())
}
if node.GetTokenType() != "" {
el.CreateAttr("TokenType", node.GetTokenType())
}
if node.GetContent() != nil {
contentEl, err := node.GetContent().GetXml(context)
if err != nil {
return nil, err
}
el.AddChild(contentEl)
}
return el, nil
}