/
metadata.go
115 lines (101 loc) · 3.21 KB
/
metadata.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
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package samlutils
import (
"context"
"github.com/crewjam/httperr"
"github.com/crewjam/saml"
"github.com/crewjam/saml/samlsp"
"io"
"net/http"
"net/url"
"os"
"strings"
)
type MetadataResolverOptions func(opt *MetadataResolverOption)
type MetadataResolverOption struct {
HttpClient *http.Client
}
func WithHttpClient(client *http.Client) MetadataResolverOptions {
return func(opt *MetadataResolverOption) {
opt.HttpClient = client
}
}
// ResolveMetadata try to resolve metadata from given metadata source
// Following modes are supported
// - if the source start with "<", it's treated as inline XML text
// - if the source is a valid HTTP/HTTPS URL, metadata is fetched over network using http.Client
// - if the source is a valid FILE URL (file://), metadata is loaded from file system
// - for any other source value, it's treated as file path
func ResolveMetadata(ctx context.Context, metadataSource string, opts...MetadataResolverOptions) (*saml.EntityDescriptor, []byte, error) {
opt := MetadataResolverOption{
HttpClient: http.DefaultClient,
}
for _, fn := range opts {
fn(&opt)
}
if strings.HasPrefix(metadataSource, "<") {
return ParseMetadataFromXml(metadataSource)
}
metadataUrl, err := url.Parse(metadataSource)
if err != nil {
return nil, nil, err
}
//if it's not url or file url, assume it's relative path
if metadataUrl.Scheme == "file" || metadataUrl.Scheme == "" {
return ParseMetadataFromFile(metadataUrl.Path)
} else {
return FetchMetadata(ctx, opt.HttpClient, metadataUrl)
}
}
func ParseMetadataFromXml(xml string) (*saml.EntityDescriptor, []byte, error) {
data := []byte(xml)
metadata, err := samlsp.ParseMetadata(data)
return metadata, data, err
}
func ParseMetadataFromFile(fileLocation string) (*saml.EntityDescriptor, []byte, error) {
file, err := os.Open(fileLocation)
if err != nil {
return nil, nil, err
}
data, err := io.ReadAll(file)
if err != nil {
return nil, nil, err
}
metadata, err := samlsp.ParseMetadata(data)
return metadata, data, err
}
func FetchMetadata(ctx context.Context, httpClient *http.Client, metadataURL *url.URL) (*saml.EntityDescriptor, []byte, error) {
req, err := http.NewRequest("GET", metadataURL.String(), nil)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)
resp, err := httpClient.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, nil, httperr.Response(*resp)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, data, err
}
metadata, err := samlsp.ParseMetadata(data)
return metadata, data, err
}