mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-22 10:15:35 +01:00
fix: correct Authorize of basic and berer authorizer (#14036)
Closes #13734 Signed-off-by: He Weiwei <hweiwei@vmware.com>
This commit is contained in:
parent
d4b7888098
commit
6a16d9a914
51
src/pkg/scan/rest/auth/api_key_auth_test.go
Normal file
51
src/pkg/scan/rest/auth/api_key_auth_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright Project Harbor Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_apiKeyAuthorizer_Authorize(t *testing.T) {
|
||||
type fields struct {
|
||||
typeID string
|
||||
accessCred string
|
||||
}
|
||||
type args struct {
|
||||
req *http.Request
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{"ok", fields{"X-ScannerAdapter-API-Key", "apikey"}, args{httptest.NewRequest("GET", "/", nil)}, false},
|
||||
{"empty cerd", fields{"X-ScannerAdapter-API-Key", ""}, args{httptest.NewRequest("GET", "/", nil)}, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
aa := &apiKeyAuthorizer{
|
||||
typeID: tt.fields.typeID,
|
||||
accessCred: tt.fields.accessCred,
|
||||
}
|
||||
if err := aa.Authorize(tt.args.req); (err != nil) != tt.wantErr {
|
||||
t.Errorf("apiKeyAuthorizer.Authorize() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -30,13 +30,10 @@ type basicAuthorizer struct {
|
||||
|
||||
// Authorize requests
|
||||
func (ba *basicAuthorizer) Authorize(req *http.Request) error {
|
||||
if len(ba.accessCred) == 0 {
|
||||
return errors.Errorf("%s:%s", ba.typeID, "missing access credential")
|
||||
}
|
||||
|
||||
if req != nil && len(ba.accessCred) > 0 {
|
||||
data := base64.StdEncoding.EncodeToString([]byte(ba.accessCred))
|
||||
req.Header.Add(authorization, fmt.Sprintf("%s %s", ba.typeID, data))
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.Errorf("%s: %s", ba.typeID, "missing data to authorize request")
|
||||
|
51
src/pkg/scan/rest/auth/basic_auth_test.go
Normal file
51
src/pkg/scan/rest/auth/basic_auth_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright Project Harbor Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_basicAuthorizer_Authorize(t *testing.T) {
|
||||
type fields struct {
|
||||
typeID string
|
||||
accessCred string
|
||||
}
|
||||
type args struct {
|
||||
req *http.Request
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{"ok", fields{"Basic", "username:password"}, args{httptest.NewRequest("GET", "/", nil)}, false},
|
||||
{"empty cerd", fields{"Basic", ""}, args{httptest.NewRequest("GET", "/", nil)}, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ba := &basicAuthorizer{
|
||||
typeID: tt.fields.typeID,
|
||||
accessCred: tt.fields.accessCred,
|
||||
}
|
||||
if err := ba.Authorize(tt.args.req); (err != nil) != tt.wantErr {
|
||||
t.Errorf("basicAuthorizer.Authorize() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -31,6 +31,7 @@ type bearerAuthorizer struct {
|
||||
func (ba *bearerAuthorizer) Authorize(req *http.Request) error {
|
||||
if req != nil && len(ba.accessCred) > 0 {
|
||||
req.Header.Add(authorization, fmt.Sprintf("%s %s", ba.typeID, ba.accessCred))
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.Errorf("%s: %s", ba.typeID, "missing data to authorize request")
|
||||
|
51
src/pkg/scan/rest/auth/bearer_auth_test.go
Normal file
51
src/pkg/scan/rest/auth/bearer_auth_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright Project Harbor Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_bearerAuthorizer_Authorize(t *testing.T) {
|
||||
type fields struct {
|
||||
typeID string
|
||||
accessCred string
|
||||
}
|
||||
type args struct {
|
||||
req *http.Request
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{"ok", fields{"Bearer", "bearer-token"}, args{httptest.NewRequest("GET", "/", nil)}, false},
|
||||
{"empty cerd", fields{"Bearer", ""}, args{httptest.NewRequest("GET", "/", nil)}, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ba := &bearerAuthorizer{
|
||||
typeID: tt.fields.typeID,
|
||||
accessCred: tt.fields.accessCred,
|
||||
}
|
||||
if err := ba.Authorize(tt.args.req); (err != nil) != tt.wantErr {
|
||||
t.Errorf("bearerAuthorizer.Authorize() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user