2015-05-21 18:14:46 +00:00
|
|
|
package auth
|
2015-05-08 23:29:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2015-05-21 18:14:46 +00:00
|
|
|
|
2015-09-30 15:47:01 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-07-24 23:14:04 +00:00
|
|
|
"github.com/docker/distribution/registry/client"
|
2015-05-21 18:14:46 +00:00
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2015-05-08 23:29:23 +00:00
|
|
|
)
|
|
|
|
|
2016-02-11 00:34:50 +00:00
|
|
|
// ErrNoBasicAuthCredentials is returned if a request can't be authorized with
|
|
|
|
// basic auth due to lack of credentials.
|
|
|
|
var ErrNoBasicAuthCredentials = errors.New("no basic auth credentials")
|
|
|
|
|
2016-03-09 20:44:55 +00:00
|
|
|
const defaultClientID = "registry-client"
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
// AuthenticationHandler is an interface for authorizing a request from
|
|
|
|
// params from a "WWW-Authenicate" header for a single scheme.
|
|
|
|
type AuthenticationHandler interface {
|
2015-05-20 17:09:37 +00:00
|
|
|
// Scheme returns the scheme as expected from the "WWW-Authenicate" header.
|
2015-05-11 18:31:22 +00:00
|
|
|
Scheme() string
|
2015-05-20 17:09:37 +00:00
|
|
|
|
|
|
|
// AuthorizeRequest adds the authorization header to a request (if needed)
|
|
|
|
// using the parameters from "WWW-Authenticate" method. The parameters
|
|
|
|
// values depend on the scheme.
|
2015-05-11 18:31:22 +00:00
|
|
|
AuthorizeRequest(req *http.Request, params map[string]string) error
|
|
|
|
}
|
|
|
|
|
2015-05-08 23:29:23 +00:00
|
|
|
// CredentialStore is an interface for getting credentials for
|
|
|
|
// a given URL
|
|
|
|
type CredentialStore interface {
|
|
|
|
// Basic returns basic auth for the given URL
|
|
|
|
Basic(*url.URL) (string, string)
|
2016-03-04 08:34:17 +00:00
|
|
|
|
|
|
|
// RefreshToken returns a refresh token for the
|
|
|
|
// given URL and service
|
|
|
|
RefreshToken(*url.URL, string) string
|
|
|
|
|
|
|
|
// SetRefreshToken sets the refresh token if none
|
|
|
|
// is provided for the given url and service
|
|
|
|
SetRefreshToken(realm *url.URL, service, token string)
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
// NewAuthorizer creates an authorizer which can handle multiple authentication
|
|
|
|
// schemes. The handlers are tried in order, the higher priority authentication
|
2015-05-21 18:14:46 +00:00
|
|
|
// methods should be first. The challengeMap holds a list of challenges for
|
|
|
|
// a given root API endpoint (for example "https://registry-1.docker.io/v2/").
|
2015-06-30 17:56:29 +00:00
|
|
|
func NewAuthorizer(manager ChallengeManager, handlers ...AuthenticationHandler) transport.RequestModifier {
|
2015-05-21 18:14:46 +00:00
|
|
|
return &endpointAuthorizer{
|
2015-06-30 17:56:29 +00:00
|
|
|
challenges: manager,
|
2015-05-11 18:31:22 +00:00
|
|
|
handlers: handlers,
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 18:14:46 +00:00
|
|
|
type endpointAuthorizer struct {
|
2015-06-30 17:56:29 +00:00
|
|
|
challenges ChallengeManager
|
2015-05-11 18:31:22 +00:00
|
|
|
handlers []AuthenticationHandler
|
2015-05-12 19:04:18 +00:00
|
|
|
transport http.RoundTripper
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 18:14:46 +00:00
|
|
|
func (ea *endpointAuthorizer) ModifyRequest(req *http.Request) error {
|
2015-05-08 23:29:23 +00:00
|
|
|
v2Root := strings.Index(req.URL.Path, "/v2/")
|
2015-05-21 18:14:46 +00:00
|
|
|
if v2Root == -1 {
|
2015-05-08 23:29:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ping := url.URL{
|
|
|
|
Host: req.URL.Host,
|
|
|
|
Scheme: req.URL.Scheme,
|
|
|
|
Path: req.URL.Path[:v2Root+4],
|
|
|
|
}
|
|
|
|
|
2016-02-26 22:18:09 +00:00
|
|
|
challenges, err := ea.challenges.GetChallenges(ping)
|
2015-06-30 17:56:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 17:56:29 +00:00
|
|
|
if len(challenges) > 0 {
|
|
|
|
for _, handler := range ea.handlers {
|
|
|
|
for _, challenge := range challenges {
|
|
|
|
if challenge.Scheme != handler.Scheme() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := handler.AuthorizeRequest(req, challenge.Parameters); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-11 18:31:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 23:29:23 +00:00
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
return nil
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:47:01 +00:00
|
|
|
// This is the minimum duration a token can last (in seconds).
|
|
|
|
// A token must not live less than 60 seconds because older versions
|
|
|
|
// of the Docker client didn't read their expiration from the token
|
|
|
|
// response and assumed 60 seconds. So to remain compatible with
|
|
|
|
// those implementations, a token must live at least this long.
|
|
|
|
const minimumTokenLifetimeSeconds = 60
|
|
|
|
|
|
|
|
// Private interface for time used by this package to enable tests to provide their own implementation.
|
|
|
|
type clock interface {
|
|
|
|
Now() time.Time
|
|
|
|
}
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
type tokenHandler struct {
|
2015-05-12 19:04:18 +00:00
|
|
|
header http.Header
|
|
|
|
creds CredentialStore
|
|
|
|
transport http.RoundTripper
|
2015-09-30 15:47:01 +00:00
|
|
|
clock clock
|
2015-05-08 23:29:23 +00:00
|
|
|
|
2016-03-04 22:32:51 +00:00
|
|
|
offlineAccess bool
|
|
|
|
forceOAuth bool
|
|
|
|
clientID string
|
|
|
|
scopes []Scope
|
2016-03-04 19:32:48 +00:00
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
tokenLock sync.Mutex
|
|
|
|
tokenCache string
|
|
|
|
tokenExpiration time.Time
|
2016-03-04 19:32:48 +00:00
|
|
|
}
|
2016-01-05 19:13:27 +00:00
|
|
|
|
2016-03-04 19:32:48 +00:00
|
|
|
// Scope is a type which is serializable to a string
|
|
|
|
// using the allow scope grammar.
|
|
|
|
type Scope interface {
|
|
|
|
String() string
|
2015-05-11 18:31:22 +00:00
|
|
|
}
|
2015-05-08 23:29:23 +00:00
|
|
|
|
2016-03-04 19:32:48 +00:00
|
|
|
// RepositoryScope represents a token scope for access
|
|
|
|
// to a repository.
|
|
|
|
type RepositoryScope struct {
|
|
|
|
Repository string
|
|
|
|
Actions []string
|
2015-05-11 18:31:22 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 19:32:48 +00:00
|
|
|
// String returns the string representation of the repository
|
|
|
|
// using the scope grammar
|
|
|
|
func (rs RepositoryScope) String() string {
|
|
|
|
return fmt.Sprintf("repository:%s:%s", rs.Repository, strings.Join(rs.Actions, ","))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokenHandlerOptions is used to configure a new token handler
|
|
|
|
type TokenHandlerOptions struct {
|
|
|
|
Transport http.RoundTripper
|
|
|
|
Credentials CredentialStore
|
|
|
|
|
2016-03-04 22:32:51 +00:00
|
|
|
OfflineAccess bool
|
|
|
|
ForceOAuth bool
|
|
|
|
ClientID string
|
|
|
|
Scopes []Scope
|
2015-05-14 16:54:23 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:47:01 +00:00
|
|
|
// An implementation of clock for providing real time data.
|
|
|
|
type realClock struct{}
|
|
|
|
|
|
|
|
// Now implements clock
|
|
|
|
func (realClock) Now() time.Time { return time.Now() }
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
// NewTokenHandler creates a new AuthenicationHandler which supports
|
|
|
|
// fetching tokens from a remote token server.
|
2015-05-21 18:14:46 +00:00
|
|
|
func NewTokenHandler(transport http.RoundTripper, creds CredentialStore, scope string, actions ...string) AuthenticationHandler {
|
2016-03-04 19:32:48 +00:00
|
|
|
// Create options...
|
|
|
|
return NewTokenHandlerWithOptions(TokenHandlerOptions{
|
|
|
|
Transport: transport,
|
|
|
|
Credentials: creds,
|
|
|
|
Scopes: []Scope{
|
|
|
|
RepositoryScope{
|
|
|
|
Repository: scope,
|
|
|
|
Actions: actions,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-09-30 15:47:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 19:32:48 +00:00
|
|
|
// NewTokenHandlerWithOptions creates a new token handler using the provided
|
|
|
|
// options structure.
|
|
|
|
func NewTokenHandlerWithOptions(options TokenHandlerOptions) AuthenticationHandler {
|
|
|
|
handler := &tokenHandler{
|
2016-03-04 22:32:51 +00:00
|
|
|
transport: options.Transport,
|
|
|
|
creds: options.Credentials,
|
|
|
|
offlineAccess: options.OfflineAccess,
|
|
|
|
forceOAuth: options.ForceOAuth,
|
|
|
|
clientID: options.ClientID,
|
|
|
|
scopes: options.Scopes,
|
|
|
|
clock: realClock{},
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
2016-03-04 19:32:48 +00:00
|
|
|
|
|
|
|
return handler
|
2015-05-11 18:31:22 +00:00
|
|
|
}
|
2015-05-08 23:29:23 +00:00
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
func (th *tokenHandler) client() *http.Client {
|
2015-05-12 19:04:18 +00:00
|
|
|
return &http.Client{
|
2015-05-14 16:54:23 +00:00
|
|
|
Transport: th.transport,
|
|
|
|
Timeout: 15 * time.Second,
|
2015-05-12 19:04:18 +00:00
|
|
|
}
|
2015-05-11 18:31:22 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
func (th *tokenHandler) Scheme() string {
|
2015-05-11 18:31:22 +00:00
|
|
|
return "bearer"
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
func (th *tokenHandler) AuthorizeRequest(req *http.Request, params map[string]string) error {
|
2016-01-05 19:13:27 +00:00
|
|
|
var additionalScopes []string
|
|
|
|
if fromParam := req.URL.Query().Get("from"); fromParam != "" {
|
2016-03-04 19:32:48 +00:00
|
|
|
additionalScopes = append(additionalScopes, RepositoryScope{
|
|
|
|
Repository: fromParam,
|
|
|
|
Actions: []string{"pull"},
|
2016-01-05 19:13:27 +00:00
|
|
|
}.String())
|
|
|
|
}
|
2016-03-04 23:13:27 +00:00
|
|
|
|
|
|
|
token, err := th.getToken(params, additionalScopes...)
|
|
|
|
if err != nil {
|
2015-05-11 18:31:22 +00:00
|
|
|
return err
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 23:13:27 +00:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
2015-05-11 18:31:22 +00:00
|
|
|
|
2015-05-08 23:29:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:13:27 +00:00
|
|
|
func (th *tokenHandler) getToken(params map[string]string, additionalScopes ...string) (string, error) {
|
2015-05-14 16:54:23 +00:00
|
|
|
th.tokenLock.Lock()
|
|
|
|
defer th.tokenLock.Unlock()
|
2016-03-04 19:32:48 +00:00
|
|
|
scopes := make([]string, 0, len(th.scopes)+len(additionalScopes))
|
|
|
|
for _, scope := range th.scopes {
|
|
|
|
scopes = append(scopes, scope.String())
|
|
|
|
}
|
2016-01-05 19:13:27 +00:00
|
|
|
var addedScopes bool
|
|
|
|
for _, scope := range additionalScopes {
|
2016-03-04 19:32:48 +00:00
|
|
|
scopes = append(scopes, scope)
|
|
|
|
addedScopes = true
|
2016-01-05 19:13:27 +00:00
|
|
|
}
|
2016-03-04 19:32:48 +00:00
|
|
|
|
2015-09-30 15:47:01 +00:00
|
|
|
now := th.clock.Now()
|
2016-01-05 19:13:27 +00:00
|
|
|
if now.After(th.tokenExpiration) || addedScopes {
|
2016-03-04 19:32:48 +00:00
|
|
|
token, expiration, err := th.fetchToken(params, scopes)
|
2015-05-08 23:29:23 +00:00
|
|
|
if err != nil {
|
2016-03-04 23:13:27 +00:00
|
|
|
return "", err
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
2016-03-04 08:34:17 +00:00
|
|
|
|
|
|
|
// do not update cache for added scope tokens
|
|
|
|
if !addedScopes {
|
|
|
|
th.tokenCache = token
|
|
|
|
th.tokenExpiration = expiration
|
|
|
|
}
|
2016-03-04 23:13:27 +00:00
|
|
|
|
|
|
|
return token, nil
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 23:13:27 +00:00
|
|
|
return th.tokenCache, nil
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 08:34:17 +00:00
|
|
|
type postTokenResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
IssuedAt time.Time `json:"issued_at"`
|
|
|
|
Scope string `json:"scope"`
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 08:34:17 +00:00
|
|
|
func (th *tokenHandler) fetchTokenWithOAuth(realm *url.URL, refreshToken, service string, scopes []string) (token string, expiration time.Time, err error) {
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("scope", strings.Join(scopes, " "))
|
|
|
|
form.Set("service", service)
|
|
|
|
|
2016-03-04 19:32:48 +00:00
|
|
|
clientID := th.clientID
|
|
|
|
if clientID == "" {
|
|
|
|
// Use default client, this is a required field
|
2016-03-09 20:44:55 +00:00
|
|
|
clientID = defaultClientID
|
2016-03-04 19:32:48 +00:00
|
|
|
}
|
|
|
|
form.Set("client_id", clientID)
|
2016-03-04 08:34:17 +00:00
|
|
|
|
|
|
|
if refreshToken != "" {
|
|
|
|
form.Set("grant_type", "refresh_token")
|
|
|
|
form.Set("refresh_token", refreshToken)
|
|
|
|
} else if th.creds != nil {
|
|
|
|
form.Set("grant_type", "password")
|
|
|
|
username, password := th.creds.Basic(realm)
|
|
|
|
form.Set("username", username)
|
|
|
|
form.Set("password", password)
|
|
|
|
|
|
|
|
// attempt to get a refresh token
|
|
|
|
form.Set("access_type", "offline")
|
|
|
|
} else {
|
|
|
|
// refuse to do oauth without a grant type
|
|
|
|
return "", time.Time{}, fmt.Errorf("no supported grant type")
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 08:34:17 +00:00
|
|
|
resp, err := th.client().PostForm(realm.String(), form)
|
2015-05-08 23:29:23 +00:00
|
|
|
if err != nil {
|
2016-03-04 08:34:17 +00:00
|
|
|
return "", time.Time{}, err
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
2016-03-04 08:34:17 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if !client.SuccessStatus(resp.StatusCode) {
|
|
|
|
err := client.HandleErrorResponse(resp)
|
|
|
|
return "", time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
|
|
|
|
var tr postTokenResponse
|
|
|
|
if err = decoder.Decode(&tr); err != nil {
|
|
|
|
return "", time.Time{}, fmt.Errorf("unable to decode token response: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.RefreshToken != "" && tr.RefreshToken != refreshToken {
|
|
|
|
th.creds.SetRefreshToken(realm, service, tr.RefreshToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.ExpiresIn < minimumTokenLifetimeSeconds {
|
|
|
|
// The default/minimum lifetime.
|
|
|
|
tr.ExpiresIn = minimumTokenLifetimeSeconds
|
|
|
|
logrus.Debugf("Increasing token expiration to: %d seconds", tr.ExpiresIn)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.IssuedAt.IsZero() {
|
|
|
|
// issued_at is optional in the token response.
|
|
|
|
tr.IssuedAt = th.clock.Now().UTC()
|
|
|
|
}
|
|
|
|
|
|
|
|
return tr.AccessToken, tr.IssuedAt.Add(time.Duration(tr.ExpiresIn) * time.Second), nil
|
|
|
|
}
|
2015-05-08 23:29:23 +00:00
|
|
|
|
2016-03-04 08:34:17 +00:00
|
|
|
type getTokenResponse struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
IssuedAt time.Time `json:"issued_at"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *tokenHandler) fetchTokenWithBasicAuth(realm *url.URL, service string, scopes []string) (token string, expiration time.Time, err error) {
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", realm.String(), nil)
|
2015-05-08 23:29:23 +00:00
|
|
|
if err != nil {
|
2016-03-04 08:34:17 +00:00
|
|
|
return "", time.Time{}, err
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reqParams := req.URL.Query()
|
|
|
|
|
|
|
|
if service != "" {
|
|
|
|
reqParams.Add("service", service)
|
|
|
|
}
|
|
|
|
|
2016-03-04 08:34:17 +00:00
|
|
|
for _, scope := range scopes {
|
2016-01-05 19:13:27 +00:00
|
|
|
reqParams.Add("scope", scope)
|
|
|
|
}
|
|
|
|
|
2016-03-04 22:32:51 +00:00
|
|
|
if th.offlineAccess {
|
|
|
|
reqParams.Add("offline_token", "true")
|
2016-03-09 20:44:55 +00:00
|
|
|
clientID := th.clientID
|
|
|
|
if clientID == "" {
|
|
|
|
clientID = defaultClientID
|
|
|
|
}
|
|
|
|
reqParams.Add("client_id", clientID)
|
2016-03-04 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
if th.creds != nil {
|
2016-03-04 08:34:17 +00:00
|
|
|
username, password := th.creds.Basic(realm)
|
2015-05-08 23:29:23 +00:00
|
|
|
if username != "" && password != "" {
|
|
|
|
reqParams.Add("account", username)
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req.URL.RawQuery = reqParams.Encode()
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
resp, err := th.client().Do(req)
|
2015-05-08 23:29:23 +00:00
|
|
|
if err != nil {
|
2016-03-04 08:34:17 +00:00
|
|
|
return "", time.Time{}, err
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if !client.SuccessStatus(resp.StatusCode) {
|
2015-12-08 22:24:03 +00:00
|
|
|
err := client.HandleErrorResponse(resp)
|
2016-03-04 08:34:17 +00:00
|
|
|
return "", time.Time{}, err
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
|
2016-03-04 08:34:17 +00:00
|
|
|
var tr getTokenResponse
|
|
|
|
if err = decoder.Decode(&tr); err != nil {
|
|
|
|
return "", time.Time{}, fmt.Errorf("unable to decode token response: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.RefreshToken != "" && th.creds != nil {
|
|
|
|
th.creds.SetRefreshToken(realm, service, tr.RefreshToken)
|
2015-09-30 15:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// `access_token` is equivalent to `token` and if both are specified
|
|
|
|
// the choice is undefined. Canonicalize `access_token` by sticking
|
|
|
|
// things in `token`.
|
|
|
|
if tr.AccessToken != "" {
|
|
|
|
tr.Token = tr.AccessToken
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tr.Token == "" {
|
2016-03-04 08:34:17 +00:00
|
|
|
return "", time.Time{}, errors.New("authorization server did not include a token in the response")
|
2015-09-30 15:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tr.ExpiresIn < minimumTokenLifetimeSeconds {
|
|
|
|
// The default/minimum lifetime.
|
|
|
|
tr.ExpiresIn = minimumTokenLifetimeSeconds
|
2016-02-03 18:42:32 +00:00
|
|
|
logrus.Debugf("Increasing token expiration to: %d seconds", tr.ExpiresIn)
|
2015-09-30 15:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tr.IssuedAt.IsZero() {
|
|
|
|
// issued_at is optional in the token response.
|
2016-03-04 08:34:17 +00:00
|
|
|
tr.IssuedAt = th.clock.Now().UTC()
|
|
|
|
}
|
|
|
|
|
|
|
|
return tr.Token, tr.IssuedAt.Add(time.Duration(tr.ExpiresIn) * time.Second), nil
|
|
|
|
}
|
|
|
|
|
2016-03-04 19:32:48 +00:00
|
|
|
func (th *tokenHandler) fetchToken(params map[string]string, scopes []string) (token string, expiration time.Time, err error) {
|
2016-03-04 08:34:17 +00:00
|
|
|
realm, ok := params["realm"]
|
|
|
|
if !ok {
|
|
|
|
return "", time.Time{}, errors.New("no realm specified for token auth challenge")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(dmcgowan): Handle empty scheme and relative realm
|
|
|
|
realmURL, err := url.Parse(realm)
|
|
|
|
if err != nil {
|
|
|
|
return "", time.Time{}, fmt.Errorf("invalid token auth challenge realm: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
service := params["service"]
|
|
|
|
|
|
|
|
var refreshToken string
|
|
|
|
|
|
|
|
if th.creds != nil {
|
|
|
|
refreshToken = th.creds.RefreshToken(realmURL, service)
|
|
|
|
}
|
|
|
|
|
2016-03-04 19:32:48 +00:00
|
|
|
if refreshToken != "" || th.forceOAuth {
|
2016-03-04 08:34:17 +00:00
|
|
|
return th.fetchTokenWithOAuth(realmURL, refreshToken, service, scopes)
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 08:34:17 +00:00
|
|
|
return th.fetchTokenWithBasicAuth(realmURL, service, scopes)
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
2015-05-11 18:31:22 +00:00
|
|
|
|
|
|
|
type basicHandler struct {
|
|
|
|
creds CredentialStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBasicHandler creaters a new authentiation handler which adds
|
|
|
|
// basic authentication credentials to a request.
|
|
|
|
func NewBasicHandler(creds CredentialStore) AuthenticationHandler {
|
|
|
|
return &basicHandler{
|
|
|
|
creds: creds,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*basicHandler) Scheme() string {
|
|
|
|
return "basic"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bh *basicHandler) AuthorizeRequest(req *http.Request, params map[string]string) error {
|
|
|
|
if bh.creds != nil {
|
|
|
|
username, password := bh.creds.Basic(req.URL)
|
|
|
|
if username != "" && password != "" {
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 00:34:50 +00:00
|
|
|
return ErrNoBasicAuthCredentials
|
2015-05-11 18:31:22 +00:00
|
|
|
}
|