Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOVER-196 http_opa should use bearer token and omit others #35

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions http_opa/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
)

const (
DefaultRequestIDKey = "X-Request-ID"
DefaultRequestIDKey = "X-Request-ID"
BearerPrefix = "Bearer "
AuthorizationHeaderName = "Authorization"
)

// GetRequestIdFromRequest fetches requestid from http request
Expand All @@ -21,15 +23,15 @@ func GetRequestIdFromRequest(r *http.Request) string {
return uuid.NewString()
}

// GetBearerFromRequest fetches requestid from http request
// GetBearerFromRequest fetches the first available bearer token from the request header.
func GetBearerFromRequest(r *http.Request) (string, error) {
authHead := r.Header.Get("Authorization")
if len(authHead) == 0 {
return authHead, exception.ErrAbstrAuthHeaderMissing
authHead := r.Header.Values(AuthorizationHeaderName)
for _, auth := range authHead {
token, isBearer := strings.CutPrefix(auth, BearerPrefix)
if isBearer {
return token, nil
}
}
authHeadArr := strings.Split(authHead, " ")
if len(authHeadArr) != 2 {
return authHead, exception.ErrAbstrAuthHeaderMalformed
}
return authHeadArr[1], nil

return "", exception.ErrAbstrAuthHeaderMissing
}
66 changes: 66 additions & 0 deletions http_opa/util/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package util

import (
"net/http"
"testing"

"github.com/infobloxopen/atlas-authz-middleware/v2/http_opa/exception"
)

func TestGetBearerFromRequest(t *testing.T) {
tests := []struct {
name string
request *http.Request
expectedBearer string
expectedError error
}{
{
name: "Bearer token is present",
request: &http.Request{
Header: http.Header{
"Authorization": []string{"Bearer token"},
},
},
expectedBearer: "token",
},
{
name: "Bearer token is not present",
request: &http.Request{
Header: http.Header{
"Authorization": []string{"Basic token"},
},
},
expectedError: exception.ErrAbstrAuthHeaderMissing,
},
{
name: "Bearer token is empty",
request: &http.Request{
Header: http.Header{
"Authorization": []string{""},
},
},
expectedError: exception.ErrAbstrAuthHeaderMissing,
},
{
name: "Bearer token is present along with other headers",
request: &http.Request{
Header: http.Header{
"Authorization": []string{"Basic basic.token", "Bearer bearer.token"},
},
},
expectedBearer: "bearer.token",
kanaksinghal marked this conversation as resolved.
Show resolved Hide resolved
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
bearer, err := GetBearerFromRequest(test.request)
if bearer != test.expectedBearer {
t.Errorf("Expected bearer: %s, but got: %s", test.expectedBearer, bearer)
}
if err != test.expectedError {
t.Errorf("Expected error: %v, but got: %v", test.expectedError, err)
}
})
}
}
Loading