@@ -2,6 +2,7 @@ package server_v1
22
33import (
44 "context"
5+ "crypto/tls"
56 "errors"
67 "net/http"
78
@@ -19,6 +20,8 @@ type AuthHandler struct {
1920 CbClient * gocbcorex.BucketsTrackingAgentManager
2021}
2122
23+ type CtxKeyTlsConnState struct {}
24+
2225func (a AuthHandler ) getUserPassFromRequest (authHdr string ) (string , string , error ) {
2326 // we reuse the Basic auth parsing built into the go net library
2427 r := http.Request {
@@ -35,23 +38,60 @@ func (a AuthHandler) getUserPassFromRequest(authHdr string) (string, string, err
3538 return username , password , nil
3639}
3740
38- func (a AuthHandler ) MaybeGetUserPassFromRequest (authHdr string ) (string , string , * Status ) {
39- username , password , err := a .getUserPassFromRequest (authHdr )
41+ func (a AuthHandler ) MaybeGetUserPassFromRequest (authHdr * string ) (string , string , * Status ) {
42+ if authHdr == nil {
43+ return "" , "" , nil
44+ }
45+
46+ username , password , err := a .getUserPassFromRequest (* authHdr )
4047 if err != nil {
4148 return "" , "" , a .ErrorHandler .NewInvalidAuthHeaderStatus (err )
4249 }
4350
4451 return username , password , nil
4552}
4653
47- func (a AuthHandler ) MaybeGetOboUserFromContext (ctx context.Context , authHdr string ) (string , string , * Status ) {
54+ func (a AuthHandler ) MaybeGetConnStateFromContext (ctx context.Context ) (* tls.ConnectionState , * Status ) {
55+ connState , ok := ctx .Value (CtxKeyTlsConnState {}).(* tls.ConnectionState )
56+ if connState == nil || ! ok {
57+ return nil , nil
58+ }
59+
60+ return connState , nil
61+ }
62+
63+ func (a AuthHandler ) MaybeGetOboUserFromContext (ctx context.Context , authHdr * string ) (string , string , * Status ) {
4864 username , password , errHe := a .MaybeGetUserPassFromRequest (authHdr )
4965 if errHe != nil {
5066 return "" , "" , errHe
5167 }
5268
53- if username == "" && password == "" {
54- return "" , "" , nil
69+ connState , errSt := a .MaybeGetConnStateFromContext (ctx )
70+ if errSt != nil {
71+ return "" , "" , errSt
72+ }
73+
74+ credsFound := username != "" && password != ""
75+ certFound := connState != nil && len (connState .PeerCertificates ) != 0
76+
77+ switch {
78+ case ! credsFound && ! certFound :
79+ return "" , "" , a .ErrorHandler .NewNoAuthStatus ()
80+ case credsFound && certFound :
81+ a .Logger .Debug ("username/password taking priority over client cert auth as both were given." )
82+ case credsFound :
83+ case certFound :
84+ oboUser , oboDomain , err := a .Authenticator .ValidateConnStateForObo (ctx , connState )
85+ if err != nil {
86+ if errors .Is (err , auth .ErrInvalidCertificate ) {
87+ return "" , "" , a .ErrorHandler .NewInvalidCertificateStatus ()
88+ }
89+
90+ a .Logger .Error ("received an unexpected cert authentication error" , zap .Error (err ))
91+ return "" , "" , a .ErrorHandler .NewInternalStatus ()
92+ }
93+
94+ return oboUser , oboDomain , nil
5595 }
5696
5797 oboUser , oboDomain , err := a .Authenticator .ValidateUserForObo (ctx , username , password )
@@ -67,7 +107,7 @@ func (a AuthHandler) MaybeGetOboUserFromContext(ctx context.Context, authHdr str
67107 return oboUser , oboDomain , nil
68108}
69109
70- func (a AuthHandler ) GetOboUserFromRequest (ctx context.Context , authHdr string ) (string , string , * Status ) {
110+ func (a AuthHandler ) GetOboUserFromRequest (ctx context.Context , authHdr * string ) (string , string , * Status ) {
71111 user , domain , st := a .MaybeGetOboUserFromContext (ctx , authHdr )
72112 if st != nil {
73113 return "" , "" , st
@@ -81,7 +121,7 @@ func (a AuthHandler) GetOboUserFromRequest(ctx context.Context, authHdr string)
81121}
82122
83123func (a AuthHandler ) GetHttpOboInfoFromContext (ctx context.Context , authHdr string ) (* cbhttpx.OnBehalfOfInfo , * Status ) {
84- username , password , errHe := a .MaybeGetUserPassFromRequest (authHdr )
124+ username , password , errHe := a .MaybeGetUserPassFromRequest (& authHdr )
85125 if errHe != nil {
86126 return nil , errHe
87127 }
@@ -119,7 +159,7 @@ func (a AuthHandler) getBucketAgent(ctx context.Context, bucketName string) (*go
119159}
120160
121161func (a AuthHandler ) GetMemdOboAgent (
122- ctx context.Context , authHdr string , bucketName string ,
162+ ctx context.Context , authHdr * string , bucketName string ,
123163) (* gocbcorex.Agent , string , * Status ) {
124164 oboUser , _ , errHe := a .GetOboUserFromRequest (ctx , authHdr )
125165 if errHe != nil {
0 commit comments