@@ -28,6 +28,8 @@ const (
2828 ErrAuthCertNotFound = "auth.cert_not_found"
2929 ErrAuthCertExpired = "auth.cert_expired"
3030 ErrAuthKeyIsInvalid = "auth.key_is_invalid"
31+ ErrAuthCertRequired = "auth.cert_required"
32+ ErrAuthCertMismatch = "auth.cert_mismatch"
3133
3234 MsgNonExistentKey = "Attempted access with non-existent key."
3335 MsgNonExistentCert = "Attempted access with non-existent cert."
@@ -59,6 +61,16 @@ func initAuthKeyErrors() {
5961 Message : MsgCertificateExpired ,
6062 Code : http .StatusForbidden ,
6163 }
64+
65+ TykErrors [ErrAuthCertRequired ] = config.TykError {
66+ Message : MsgAuthCertRequired ,
67+ Code : http .StatusUnauthorized ,
68+ }
69+
70+ TykErrors [ErrAuthCertMismatch ] = config.TykError {
71+ Message : MsgApiAccessDisallowed ,
72+ Code : http .StatusUnauthorized ,
73+ }
6274}
6375
6476// KeyExists will check if the key being used to access the API is in the request data,
@@ -99,41 +111,81 @@ func (k *AuthKey) ProcessRequest(_ http.ResponseWriter, r *http.Request, _ inter
99111 }
100112
101113 key , authConfig := k .getAuthToken (k .getAuthType (), r )
114+ if key == "" {
115+ key = stripBearer (key )
116+ }
117+ var keyExists , updateSession bool
102118 var certHash string
103-
104- keyExists := false
105119 var session user.SessionState
106- updateSession := false
107- if key != "" {
108- key = stripBearer (key )
109- } else if authConfig .UseCertificate && key == "" && r .TLS != nil && len (r .TLS .PeerCertificates ) > 0 {
110- log .Debug ("Trying to find key by client certificate" )
111- certHash = k .Spec .OrgID + crypto .HexSHA256 (r .TLS .PeerCertificates [0 ].Raw )
112- if time .Now ().After (r .TLS .PeerCertificates [0 ].NotAfter ) {
113- return errorAndStatusCode (ErrAuthCertExpired )
120+ if authConfig .UseCertificate && r .TLS != nil {
121+ if len (r .TLS .PeerCertificates ) > 0 {
122+ if time .Now ().After (r .TLS .PeerCertificates [0 ].NotAfter ) {
123+ return errorAndStatusCode (ErrAuthCertExpired )
124+ }
125+ certHash = k .Spec .OrgID + crypto .HexSHA256 (r .TLS .PeerCertificates [0 ].Raw )
114126 }
115127
116- key = k .Gw .generateToken (k .Spec .OrgID , certHash )
117- } else {
118- k .Logger ().Info ("Attempted access with malformed header, no auth header found." )
119- return errorAndStatusCode (ErrAuthAuthorizationFieldMissing )
120- }
121-
122- session , keyExists = k .CheckSessionAndIdentityForValidKey (key , r )
123- key = session .KeyID
124- if ! keyExists {
125- // fallback to search by cert
126- session , keyExists = k .CheckSessionAndIdentityForValidKey (certHash , r )
127- if ! keyExists {
128- return k .reportInvalidKey (key , r , MsgNonExistentKey , ErrAuthKeyNotFound )
128+ if ! k .Gw .GetConfig ().Security .AllowUnsafeDynamicMTLSToken {
129+ if certHash == "" {
130+ return errorAndStatusCode (ErrAuthCertRequired )
131+ }
132+ key = k .Gw .generateToken (k .Spec .OrgID , certHash )
133+ session , keyExists = k .CheckSessionAndIdentityForValidKey (key , r )
134+ } else {
135+ if key != "" {
136+ session , keyExists = k .CheckSessionAndIdentityForValidKey (key , r )
137+ key = session .KeyID
138+ if ! keyExists {
139+ session , keyExists = k .CheckSessionAndIdentityForValidKey (certHash , r )
140+ if ! keyExists {
141+ return k .reportInvalidKey (key , r , MsgNonExistentKey , ErrAuthKeyNotFound )
142+ }
143+ }
144+ }
129145 }
130146 }
131147
148+ //if key != "" {
149+ // fmt.Println("key: ", key)
150+ // key = stripBearer(key)
151+ //} else if authConfig.UseCertificate && key == "" && r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
152+ // fmt.Println("Trying to find key by client certificate")
153+ // log.Debug("Trying to find key by client certificate")
154+ // certHash = k.Spec.OrgID + crypto.HexSHA256(r.TLS.PeerCertificates[0].Raw)
155+ // fmt.Println("certHash: ", certHash)
156+ // if time.Now().After(r.TLS.PeerCertificates[0].NotAfter) {
157+ // return errorAndStatusCode(ErrAuthCertExpired)
158+ // }
159+ //
160+ // key = k.Gw.generateToken(k.Spec.OrgID, certHash)
161+ // fmt.Println("key: ", key)
162+ //} else {
163+ // k.Logger().Info("Attempted access with malformed header, no auth header found.")
164+ // return errorAndStatusCode(ErrAuthAuthorizationFieldMissing)
165+ //}
166+ //
167+ //session, keyExists = k.CheckSessionAndIdentityForValidKey(key, r)
168+ //key = session.KeyID
169+ //fmt.Printf("CheckSessionAndIdentityForValidKey: %s %v\n", key, keyExists)
170+ //if !keyExists {
171+ // // fallback to search by cert
172+ // session, keyExists = k.CheckSessionAndIdentityForValidKey(certHash, r)
173+ // fmt.Printf("CheckSessionAndIdentityForValidKey (cert hash): %s %v\n", certHash, keyExists)
174+ // if !keyExists {
175+ // return k.reportInvalidKey(key, r, MsgNonExistentKey, ErrAuthKeyNotFound)
176+ // }
177+ //}
178+
132179 if authConfig .UseCertificate {
133180 certLookup := session .Certificate
134181
135182 if r .TLS != nil && len (r .TLS .PeerCertificates ) > 0 {
136183 certLookup = certHash
184+ //if session.Certificate != "" && session.Certificate != certHash {
185+ // // Certificate mismatch - provided certificate doesn't match the one in session
186+ // return errorAndStatusCode(ErrAuthCertMismatch)
187+ //}
188+
137189 if session .Certificate != certHash {
138190 session .Certificate = certHash
139191 updateSession = true
0 commit comments