-
Notifications
You must be signed in to change notification settings - Fork 15
/
saml.go
123 lines (107 loc) · 3.11 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
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
package lib
import (
"encoding/xml"
"fmt"
"net/url"
saml2 "github.com/russellhaering/gosaml2"
"github.com/russellhaering/gosaml2/types"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"github.com/avenga/couper/config"
)
const (
FnSamlSsoURL = "saml_sso_url"
NameIDFormatUnspecified = "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"
)
var NoOpSamlSsoURLFunction = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "saml_label",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, _ cty.Type) (ret cty.Value, err error) {
if len(args) > 0 {
return cty.StringVal(""), fmt.Errorf("missing saml block with referenced label %q", args[0].AsString())
}
return cty.StringVal(""), fmt.Errorf("missing saml definitions")
},
})
func NewSamlSsoURLFunction(configs []*config.SAML, origin *url.URL) function.Function {
type entity struct {
config *config.SAML
descriptor *types.EntityDescriptor
err error
}
samlEntities := make(map[string]*entity)
for _, conf := range configs {
metadata := &types.EntityDescriptor{}
err := xml.Unmarshal(conf.MetadataBytes, metadata)
samlEntities[conf.Name] = &entity{
config: conf,
descriptor: metadata,
err: err,
}
}
return function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "saml_label",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, _ cty.Type) (ret cty.Value, err error) {
label := args[0].AsString()
ent, exist := samlEntities[label]
if !exist {
return NoOpSamlSsoURLFunction.Call(args)
}
metadata := ent.descriptor
var ssoURL string
for _, ssoService := range metadata.IDPSSODescriptor.SingleSignOnServices {
if ssoService.Binding == "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" {
ssoURL = ssoService.Location
continue
}
}
nameIDFormat := getNameIDFormat(metadata.IDPSSODescriptor.NameIDFormats)
absAcsURL, err := AbsoluteURL(ent.config.SpAcsURL, origin)
if err != nil {
return cty.StringVal(""), err
}
sp := &saml2.SAMLServiceProvider{
AssertionConsumerServiceURL: absAcsURL,
IdentityProviderSSOURL: ssoURL,
ServiceProviderIssuer: ent.config.SpEntityID,
SignAuthnRequests: false,
}
if nameIDFormat != "" {
sp.NameIdFormat = nameIDFormat
}
samlSsoURL, err := sp.BuildAuthURL("")
if err != nil {
return cty.StringVal(""), err
}
return cty.StringVal(samlSsoURL), nil
},
})
}
func getNameIDFormat(supportedNameIDFormats []types.NameIDFormat) string {
nameIDFormat := ""
if isSupportedNameIDFormat(supportedNameIDFormats, NameIDFormatUnspecified) {
nameIDFormat = NameIDFormatUnspecified
} else if len(supportedNameIDFormats) > 0 {
nameIDFormat = supportedNameIDFormats[0].Value
}
return nameIDFormat
}
func isSupportedNameIDFormat(supportedNameIDFormats []types.NameIDFormat, nameIDFormat string) bool {
for _, n := range supportedNameIDFormats {
if n.Value == nameIDFormat {
return true
}
}
return false
}