-
Notifications
You must be signed in to change notification settings - Fork 0
/
oioidwsrestwsp.go
171 lines (129 loc) · 4.99 KB
/
oioidwsrestwsp.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package oioidwsrest
import (
"fmt"
"crypto/x509"
"net/http"
"crypto/sha1"
"encoding/hex"
"strings"
uuid "github.com/google/uuid"
securityprotocol "github.com/KvalitetsIT/gosecurityprotocol"
)
const HEADER_WWW_AUTHENTICATE = "WWW-Authenticate"
const HEADER_AUTHORIZATION = "Authorization"
type OioIdwsRestHttpProtocolServerConfig struct {
TrustCertFiles []string
AudienceRestriction string
Service securityprotocol.HttpHandler
}
type OioIdwsRestWsp struct {
matchHandler *securityprotocol.MatchHandler
sessionCache securityprotocol.SessionCache
tokenAuthenticator *TokenAuthenticator
Service securityprotocol.HttpHandler
HoK bool
ClientCertHandler func(req *http.Request) *x509.Certificate
}
func NewOioIdwsRestWspFromConfig(config *OioIdwsRestHttpProtocolServerConfig, sessionCache securityprotocol.SessionCache) *OioIdwsRestWsp {
tokenAuthenticator := NewTokenAuthenticator(config.AudienceRestriction, config.TrustCertFiles, true)
return NewOioIdwsRestWsp(sessionCache, tokenAuthenticator, nil, config.Service)
}
func NewOioIdwsRestWsp(sessionCache securityprotocol.SessionCache, tokenAuthenticator *TokenAuthenticator, matchHandler *securityprotocol.MatchHandler, service securityprotocol.HttpHandler) *OioIdwsRestWsp{
n := new(OioIdwsRestWsp)
n.sessionCache = sessionCache
n.tokenAuthenticator = tokenAuthenticator
n.ClientCertHandler = getClientCertificate
n.Service = service
n.HoK = true
return n
}
func (a OioIdwsRestWsp) Handle(w http.ResponseWriter, r *http.Request) (int, error) {
return a.HandleService(w, r, a.Service)
}
func (a OioIdwsRestWsp) HandleService(w http.ResponseWriter, r *http.Request, service securityprotocol.HttpHandler) (int, error) {
// Check that the request is a HTTPS request and that it contains a client certificate
sslClientCertificate := a.ClientCertHandler(r)
if (a.HoK && sslClientCertificate == nil) {
return http.StatusBadRequest, fmt.Errorf("SSL Client Certificate must be supplied (HoK)")
}
// Get the session id
sessionId, err := a.getSessionId(r)
if (err != nil) {
return http.StatusUnauthorized, err
}
// The request identifies a session, check that the session is valid and get it
if (sessionId != "") {
sessionData, err := a.sessionCache.FindSessionDataForSessionId(sessionId)
if (err != nil) {
return http.StatusInternalServerError, err
}
if (sessionData != nil) {
// Validate HoK
hashFromClientCert := hashFromCertificate(sslClientCertificate)
if (a.HoK && hashFromClientCert != sessionData.ClientCertHash) {
return http.StatusUnauthorized, fmt.Errorf("client certificate not HoK")
}
// Check if the user is requesting sessiondata
handlerFunc := securityprotocol.IsRequestForSessionData(sessionData, a.sessionCache, w, r)
if (handlerFunc != nil) {
return handlerFunc()
}
// The session id ok ... pass-through to next handler
return service.Handle(w, r)
}
}
// If the request is not authenticated maybe it is a request for authentication?
assertionAsStr, authenticatedAssertion, authErr := a.tokenAuthenticator.Authenticate(sslClientCertificate, r)
if (authErr == nil && authenticatedAssertion != nil) {
createdSessionId := uuid.New().String()
samlSessionDataCreator, err := securityprotocol.NewSamlSessionDataCreatorWithAssertionAndClientCert(createdSessionId, assertionAsStr, authenticatedAssertion.GetAssertion(), hashFromCertificate(sslClientCertificate))
if (err != nil) {
return http.StatusInternalServerError, err
}
sessionData, err := samlSessionDataCreator.CreateSessionData()
if (err != nil) {
return http.StatusInternalServerError, err
}
err = a.sessionCache.SaveSessionData(sessionData)
if (err != nil) {
return http.StatusInternalServerError, err
}
// Succesful authentication
resCode, err := ResponseWithSuccessfulAuth(w, sessionData)
if (err != nil) {
return http.StatusUnauthorized, err
}
return resCode, nil
}
// The authentication process failed
if (authErr != nil) {
w.Header().Set(HEADER_WWW_AUTHENTICATE, authErr.Error())
}
return http.StatusUnauthorized, authErr
}
func (a OioIdwsRestWsp) getSessionId(r *http.Request) (string, error) {
sessionId := r.Header.Get(HEADER_AUTHORIZATION)
if (sessionId != "") {
sessionIdParts := strings.Split(sessionId, " ")
if (len(sessionIdParts) == 2) {
return sessionIdParts[1], nil
}
return "", fmt.Errorf(fmt.Sprintf("%s header contains illegal value: %s", HEADER_AUTHORIZATION, sessionId))
}
return "", nil
}
func hashFromCertificate(certificate *x509.Certificate) (string) {
if (certificate == nil) {
return ""
}
hasher := sha1.New()
hasher.Write(certificate.Raw)
return hex.EncodeToString(hasher.Sum(nil))
}
func getClientCertificate(req *http.Request) *x509.Certificate {
if (len(req.TLS.PeerCertificates) > 0) {
cert := req.TLS.PeerCertificates[0]
return cert
}
return nil
}