From 8fb5713e3a8fd9d06012b27ce1d67a607d9816f5 Mon Sep 17 00:00:00 2001 From: Prabhjot Singh Sethi Date: Thu, 22 May 2025 15:29:29 +0000 Subject: [PATCH] Make const public and provide separate variable for grpc header Signed-off-by: Prabhjot Singh Sethi --- auth/auth.go | 8 ++++---- auth/auth_test.go | 4 ++-- auth/const.go | 5 ++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 163775c..cc8158c 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -41,13 +41,13 @@ func SetAuthInfoHeader(r *http.Request, info *AuthInfo) error { return errors.Wrapf(errors.InvalidArgument, "failed to generate user info: %s", err) } val := base64.RawURLEncoding.EncodeToString(b) - r.Header.Set(httpClientAuthContext, val) + r.Header.Set(HttpClientAuthContext, val) return nil } // gets Auth Info Header available in the Http Request func GetAuthInfoHeader(r *http.Request) (*AuthInfo, error) { - val := r.Header.Get(httpClientAuthContext) + val := r.Header.Get(HttpClientAuthContext) if val == "" { return nil, errors.Wrapf(errors.NotFound, "Auth info not available in the http request") } @@ -84,7 +84,7 @@ func extractHeader(ctx context.Context, header string) (string, error) { // Processes the headers available in context, to validate that the authentication is already performed func ProcessAuthInfo(ctx context.Context) (context.Context, error) { - val, err := extractHeader(ctx, httpClientAuthContext) + val, err := extractHeader(ctx, GrpcClientAuthContext) if err != nil { return ctx, errors.Wrapf(errors.Unauthorized, "failed to extract auth info header: %s", err) } @@ -118,5 +118,5 @@ func GetAuthInfoFromContext(ctx context.Context) (*AuthInfo, error) { // delete the Auth info header from the given HTTP request func DeleteAuthInfoHeader(r *http.Request) { - r.Header.Del(httpClientAuthContext) + r.Header.Del(HttpClientAuthContext) } diff --git a/auth/auth_test.go b/auth/auth_test.go index d0eda1d..ae23b12 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -21,8 +21,8 @@ func Test_ErrorValidations(t *testing.T) { SessionID: "abc", } SetAuthInfoHeader(r, info) - fmt.Printf("Got - Encoded Auth Info: %s\n", r.Header[httpClientAuthContext][0]) - if r.Header[httpClientAuthContext][0] != "eyJyZWFsbSI6InJvb3QiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhZG1pbiIsImVtYWlsIjoiYWRtaW5AZXhhbXBsZS5jb20iLCJuYW1lIjoiVGVzdCBBZG1pbiIsInNpZCI6ImFiYyJ9" { + fmt.Printf("Got - Encoded Auth Info: %s\n", r.Header[HttpClientAuthContext][0]) + if r.Header[HttpClientAuthContext][0] != "eyJyZWFsbSI6InJvb3QiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhZG1pbiIsImVtYWlsIjoiYWRtaW5AZXhhbXBsZS5jb20iLCJuYW1lIjoiVGVzdCBBZG1pbiIsInNpZCI6ImFiYyJ9" { t.Errorf("failed to set the auth info in the header, found invalid value in header") } found, err := GetAuthInfoHeader(r) diff --git a/auth/const.go b/auth/const.go index 54764bd..6edb598 100644 --- a/auth/const.go +++ b/auth/const.go @@ -13,5 +13,8 @@ const ( // This is usually Added by Auth Gateway, if present it // indicates that authentication is successfully performed // by Auth Gateway. - httpClientAuthContext = "Auth-Info" + HttpClientAuthContext = "Auth-Info" + + // grpc gateway will typically move the header to lowercase + GrpcClientAuthContext = "auth-info" )