2015-05-21 18:14:46 +00:00
|
|
|
package auth
|
2015-05-08 23:29:23 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-10 17:38:30 +00:00
|
|
|
"context"
|
2015-05-08 23:29:23 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2015-05-21 18:14:46 +00:00
|
|
|
|
2023-10-24 16:26:38 +00:00
|
|
|
"github.com/distribution/distribution/v3/internal/client"
|
|
|
|
"github.com/distribution/distribution/v3/internal/client/auth/challenge"
|
|
|
|
"github.com/distribution/distribution/v3/internal/client/transport"
|
2015-05-08 23:29:23 +00:00
|
|
|
)
|
|
|
|
|
2016-03-18 08:12:27 +00:00
|
|
|
var (
|
|
|
|
// ErrNoBasicAuthCredentials is returned if a request can't be authorized with
|
|
|
|
// basic auth due to lack of credentials.
|
|
|
|
ErrNoBasicAuthCredentials = errors.New("no basic auth credentials")
|
|
|
|
|
|
|
|
// ErrNoToken is returned if a request is successful but the body does not
|
|
|
|
// contain an authorization token.
|
|
|
|
ErrNoToken = errors.New("authorization server did not include a token in the response")
|
|
|
|
)
|
2016-02-11 00:34:50 +00:00
|
|
|
|
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/").
|
2016-11-08 01:13:56 +00:00
|
|
|
func NewAuthorizer(manager challenge.Manager, 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 {
|
2016-11-08 01:13:56 +00:00
|
|
|
challenges challenge.Manager
|
2015-05-11 18:31:22 +00:00
|
|
|
handlers []AuthenticationHandler
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 18:14:46 +00:00
|
|
|
func (ea *endpointAuthorizer) ModifyRequest(req *http.Request) error {
|
2016-07-13 00:15:56 +00:00
|
|
|
pingPath := req.URL.Path
|
|
|
|
if v2Root := strings.Index(req.URL.Path, "/v2/"); v2Root != -1 {
|
|
|
|
pingPath = pingPath[:v2Root+4]
|
|
|
|
} else if v1Root := strings.Index(req.URL.Path, "/v1/"); v1Root != -1 {
|
|
|
|
pingPath = pingPath[:v1Root] + "/v2/"
|
|
|
|
} else {
|
2015-05-08 23:29:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ping := url.URL{
|
|
|
|
Host: req.URL.Host,
|
|
|
|
Scheme: req.URL.Scheme,
|
2016-07-13 00:15:56 +00:00
|
|
|
Path: pingPath,
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-11-08 01:13:56 +00:00
|
|
|
for _, c := range challenges {
|
|
|
|
if c.Scheme != handler.Scheme() {
|
2015-06-30 17:56:29 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-11-08 01:13:56 +00:00
|
|
|
if err := handler.AuthorizeRequest(req, c.Parameters); err != nil {
|
2015-06-30 17:56:29 +00:00
|
|
|
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
|
|
|
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
|
2017-08-15 01:30:46 +00:00
|
|
|
|
|
|
|
logger Logger
|
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
|
2016-11-16 01:14:31 +00:00
|
|
|
Class string
|
2016-03-04 19:32:48 +00:00
|
|
|
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 {
|
2016-11-16 01:14:31 +00:00
|
|
|
repoType := "repository"
|
2016-12-05 23:10:29 +00:00
|
|
|
// Keep existing format for image class to maintain backwards compatibility
|
|
|
|
// with authorization servers which do not support the expanded grammar.
|
|
|
|
if rs.Class != "" && rs.Class != "image" {
|
2016-11-16 01:14:31 +00:00
|
|
|
repoType = fmt.Sprintf("%s(%s)", repoType, rs.Class)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%s:%s", repoType, rs.Repository, strings.Join(rs.Actions, ","))
|
2016-03-04 19:32:48 +00:00
|
|
|
}
|
|
|
|
|
2016-07-13 00:15:56 +00:00
|
|
|
// RegistryScope represents a token scope for access
|
|
|
|
// to resources in the registry.
|
|
|
|
type RegistryScope struct {
|
|
|
|
Name string
|
|
|
|
Actions []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string representation of the user
|
|
|
|
// using the scope grammar
|
|
|
|
func (rs RegistryScope) String() string {
|
|
|
|
return fmt.Sprintf("registry:%s:%s", rs.Name, strings.Join(rs.Actions, ","))
|
|
|
|
}
|
|
|
|
|
2017-08-15 01:30:46 +00:00
|
|
|
// Logger defines the injectable logging interface, used on TokenHandlers.
|
|
|
|
type Logger interface {
|
|
|
|
Debugf(format string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func logDebugf(logger Logger, format string, args ...interface{}) {
|
|
|
|
if logger == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Debugf(format, args...)
|
|
|
|
}
|
|
|
|
|
2016-03-04 19:32:48 +00:00
|
|
|
// 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
|
2017-08-15 01:30:46 +00:00
|
|
|
Logger Logger
|
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{},
|
2017-08-15 01:30:46 +00:00
|
|
|
logger: options.Logger,
|
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
|
|
|
|
2022-08-10 17:38:30 +00:00
|
|
|
token, err := th.getToken(req.Context(), params, additionalScopes...)
|
2016-03-04 23:13:27 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-10 17:38:30 +00:00
|
|
|
func (th *tokenHandler) getToken(ctx context.Context, 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 {
|
2017-08-23 23:24:24 +00:00
|
|
|
if hasScope(scopes, scope) {
|
|
|
|
continue
|
|
|
|
}
|
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 {
|
2022-08-10 17:38:30 +00:00
|
|
|
token, expiration, err := th.fetchToken(ctx, 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
|
|
|
}
|
|
|
|
|
2017-08-23 23:24:24 +00:00
|
|
|
func hasScope(scopes []string, scope string) bool {
|
|
|
|
for _, s := range scopes {
|
|
|
|
if s == scope {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-10 17:38:30 +00:00
|
|
|
func (th *tokenHandler) fetchTokenWithOAuth(ctx context.Context, realm *url.URL, refreshToken, service string, scopes []string) (token string, expiration time.Time, err error) {
|
2016-03-04 08:34:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-10 17:38:30 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, realm.String(), strings.NewReader(form.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return "", time.Time{}, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
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
|
|
|
}
|
2016-03-04 08:34:17 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2023-09-08 12:33:46 +00:00
|
|
|
if err := client.HandleHTTPResponseError(resp); err != nil {
|
2016-03-04 08:34:17 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-04-14 10:05:59 +00:00
|
|
|
if tr.AccessToken == "" {
|
|
|
|
return "", time.Time{}, ErrNoToken
|
|
|
|
}
|
|
|
|
|
2016-03-04 08:34:17 +00:00
|
|
|
if tr.RefreshToken != "" && tr.RefreshToken != refreshToken {
|
|
|
|
th.creds.SetRefreshToken(realm, service, tr.RefreshToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.ExpiresIn < minimumTokenLifetimeSeconds {
|
|
|
|
// The default/minimum lifetime.
|
|
|
|
tr.ExpiresIn = minimumTokenLifetimeSeconds
|
2017-08-15 01:30:46 +00:00
|
|
|
logDebugf(th.logger, "Increasing token expiration to: %d seconds", tr.ExpiresIn)
|
2016-03-04 08:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2022-08-10 17:38:30 +00:00
|
|
|
func (th *tokenHandler) fetchTokenWithBasicAuth(ctx context.Context, realm *url.URL, service string, scopes []string) (token string, expiration time.Time, err error) {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, 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()
|
|
|
|
|
2023-09-08 12:33:46 +00:00
|
|
|
if err := client.HandleHTTPResponseError(resp); err != nil {
|
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-18 08:12:27 +00:00
|
|
|
return "", time.Time{}, ErrNoToken
|
2015-09-30 15:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tr.ExpiresIn < minimumTokenLifetimeSeconds {
|
|
|
|
// The default/minimum lifetime.
|
|
|
|
tr.ExpiresIn = minimumTokenLifetimeSeconds
|
2017-08-15 01:30:46 +00:00
|
|
|
logDebugf(th.logger, "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
|
|
|
|
}
|
|
|
|
|
2022-08-10 17:38:30 +00:00
|
|
|
func (th *tokenHandler) fetchToken(ctx context.Context, 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 {
|
2022-08-10 17:38:30 +00:00
|
|
|
return th.fetchTokenWithOAuth(ctx, realmURL, refreshToken, service, scopes)
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 17:38:30 +00:00
|
|
|
return th.fetchTokenWithBasicAuth(ctx, 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
|
|
|
}
|