-
Notifications
You must be signed in to change notification settings - Fork 2
/
saml.go
72 lines (62 loc) · 1.49 KB
/
saml.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
package upvs
import (
goContext "context"
"encoding/xml"
"strconv"
)
type SamlToken struct {
ActorUPVSIdentityID string
SubjectUPVSIdentityID string
DelegationType int
}
func (c *Connector) GetSamlToken(ctx goContext.Context, oboToken string) (*SamlToken, error) {
signedOboToken, err := c.signOboToken(ctx, oboToken)
if err != nil {
return nil, err
}
response, err := c.sendRequest(
ctx,
&request{
method: "GET",
url: "/api/upvs/assertion",
headers: map[string]string{
"Authorization": "Bearer " + signedOboToken,
"Accept": "application/samlassertion+xml",
},
},
)
if err != nil {
return nil, err
}
return parseSamlToken(response)
}
type RawSamlToken struct {
XMLName xml.Name `xml:"Assertion"`
Attributes []struct {
Name string `xml:",attr"`
AttributeValue string
} `xml:"AttributeStatement>Attribute"`
}
func parseSamlToken(data []byte) (*SamlToken, error) {
raw := &RawSamlToken{}
err := xml.Unmarshal(data, raw)
if err != nil {
return nil, err
}
token := &SamlToken{}
for _, attribute := range raw.Attributes {
if attribute.Name == "Actor.UPVSIdentityID" {
token.ActorUPVSIdentityID = attribute.AttributeValue
}
if attribute.Name == "Subject.UPVSIdentityID" {
token.SubjectUPVSIdentityID = attribute.AttributeValue
}
if attribute.Name == "DelegationType" {
token.DelegationType, err = strconv.Atoi(attribute.AttributeValue)
if err != nil {
return nil, err
}
}
}
return token, nil
}