2016-04-27 11:59:43 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
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 (
|
2016-06-21 10:39:03 +02:00
|
|
|
"fmt"
|
2016-04-27 11:59:43 +02:00
|
|
|
"net/http"
|
2016-09-29 10:17:18 +02:00
|
|
|
"time"
|
2016-04-27 11:59:43 +02:00
|
|
|
|
|
|
|
au "github.com/docker/distribution/registry/client/auth"
|
2016-06-27 08:37:26 +02:00
|
|
|
"github.com/vmware/harbor/utils"
|
2016-09-29 10:17:18 +02:00
|
|
|
"github.com/vmware/harbor/utils/registry"
|
2016-04-27 11:59:43 +02:00
|
|
|
)
|
|
|
|
|
2016-06-21 10:39:03 +02:00
|
|
|
// Authorizer authorizes requests according to the schema
|
|
|
|
type Authorizer interface {
|
2016-04-28 12:49:59 +02:00
|
|
|
// Scheme : basic, bearer
|
|
|
|
Scheme() string
|
2016-06-21 10:39:03 +02:00
|
|
|
//Authorize adds basic auth or token auth to the header of request
|
|
|
|
Authorize(req *http.Request, params map[string]string) error
|
2016-04-27 11:59:43 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 10:39:03 +02:00
|
|
|
// AuthorizerStore holds a authorizer list, which will authorize request.
|
|
|
|
// And it implements interface Modifier
|
|
|
|
type AuthorizerStore struct {
|
|
|
|
authorizers []Authorizer
|
|
|
|
challenges []au.Challenge
|
2016-04-27 11:59:43 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 10:39:03 +02:00
|
|
|
// NewAuthorizerStore ...
|
2016-06-22 06:03:50 +02:00
|
|
|
func NewAuthorizerStore(endpoint string, insecure bool, authorizers ...Authorizer) (*AuthorizerStore, error) {
|
2016-06-21 10:39:03 +02:00
|
|
|
endpoint = utils.FormatEndpoint(endpoint)
|
|
|
|
|
2016-06-22 06:03:50 +02:00
|
|
|
client := &http.Client{
|
2016-09-29 10:17:18 +02:00
|
|
|
Transport: registry.GetHTTPTransport(insecure),
|
|
|
|
Timeout: 30 * time.Second,
|
2016-06-22 06:03:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Get(buildPingURL(endpoint))
|
2016-06-21 10:39:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-04-27 11:59:43 +02:00
|
|
|
}
|
2016-07-14 11:50:25 +02:00
|
|
|
defer resp.Body.Close()
|
2016-06-21 10:39:03 +02:00
|
|
|
|
|
|
|
challenges := ParseChallengeFromResponse(resp)
|
|
|
|
return &AuthorizerStore{
|
|
|
|
authorizers: authorizers,
|
|
|
|
challenges: challenges,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildPingURL(endpoint string) string {
|
|
|
|
return fmt.Sprintf("%s/v2/", endpoint)
|
2016-04-27 11:59:43 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 10:39:03 +02:00
|
|
|
// Modify adds authorization to the request
|
|
|
|
func (a *AuthorizerStore) Modify(req *http.Request) error {
|
|
|
|
for _, challenge := range a.challenges {
|
|
|
|
for _, authorizer := range a.authorizers {
|
|
|
|
if authorizer.Scheme() == challenge.Scheme {
|
|
|
|
if err := authorizer.Authorize(req, challenge.Parameters); err != nil {
|
2016-04-27 11:59:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|