/
artifact.go
128 lines (119 loc) · 4.18 KB
/
artifact.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
// Copyright © 2017 Aaron Donovan <amdonov@gmail.com>
//
// 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.
package idp
import (
"encoding/xml"
"net/http"
"net/url"
"time"
"github.com/amdonov/lite-idp/model"
"github.com/amdonov/lite-idp/saml"
"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
)
// DefaultArtifactResolveHandler is the default implementation for the artifact resolution handler. It can be used as is, wrapped in other handlers, or replaced completely.
func (i *IDP) DefaultArtifactResolveHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// We require transport authentication rather than message authentication
tlsCert, err := getCertFromRequest(r)
if err != nil {
i.Error(w, err.Error(), http.StatusUnauthorized)
return
}
log.Infof("received artifact resolution request from %s", getSubjectDN(tlsCert.Subject))
i.processArtifactResolutionRequest(w, r)
}
}
func (i *IDP) processArtifactResolutionRequest(w http.ResponseWriter, r *http.Request) {
decoder := xml.NewDecoder(r.Body)
var resolveEnv saml.ArtifactResolveEnvelope
err := decoder.Decode(&resolveEnv)
// TODO confirm appropriate error response for this service
if err != nil {
i.Error(w, err.Error(), http.StatusInternalServerError)
return
}
artifact := resolveEnv.Body.ArtifactResolve.Artifact
data, err := i.TempCache.Get(artifact)
if err != nil {
i.Error(w, err.Error(), http.StatusInternalServerError)
return
}
artifactResponse := &model.ArtifactResponse{}
err = proto.Unmarshal(data, artifactResponse)
// TODO confirm appropriate error response for this service
if err != nil {
i.Error(w, err.Error(), http.StatusInternalServerError)
return
}
now := time.Now().UTC()
response := i.makeAuthnResponse(artifactResponse.Request, artifactResponse.User)
artResponseEnv := saml.ArtifactResponseEnvelope{
Body: saml.ArtifactResponseBody{
ArtifactResponse: saml.ArtifactResponse{
StatusResponseType: saml.StatusResponseType{
ID: saml.NewID(),
IssueInstant: now,
InResponseTo: resolveEnv.Body.ArtifactResolve.ID,
Version: "2.0",
Issuer: saml.NewIssuer(i.entityID),
Status: &saml.Status{
StatusCode: saml.StatusCode{
Value: "urn:oasis:names:tc:SAML:2.0:status:Success",
},
},
},
Response: *response,
},
},
}
signature, err := i.signer.CreateSignature(response.Assertion)
// TODO confirm appropriate error response for this service
if err != nil {
i.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response.Assertion.Signature = signature
// TODO handle these errors. Probably can't do anything besides log, as we've already started to write the
// response.
_, err = w.Write([]byte(xml.Header))
encoder := xml.NewEncoder(w)
err = encoder.Encode(artResponseEnv)
err = encoder.Flush()
}
func (i *IDP) sendArtifactResponse(authRequest *model.AuthnRequest, user *model.User,
w http.ResponseWriter, r *http.Request) error {
target, err := url.Parse(authRequest.AssertionConsumerServiceURL)
if err != nil {
i.Error(w, err.Error(), http.StatusInternalServerError)
}
parameters := url.Values{}
artifact := getArtifact(i.entityID)
// Store required data in the cache
response := &model.ArtifactResponse{
User: user,
Request: authRequest,
}
data, err := proto.Marshal(response)
if err != nil {
i.Error(w, err.Error(), http.StatusInternalServerError)
}
i.TempCache.Set(artifact, data)
parameters.Add("SAMLart", artifact)
parameters.Add("RelayState", authRequest.RelayState)
target.RawQuery = parameters.Encode()
// Don't send temporary redirect. We don't want the post resent
http.Redirect(w, r, target.String(), http.StatusFound)
return nil
}