github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/uaa/resources.go (about)

     1  package uaa
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/api/uaa/internal"
     9  )
    10  
    11  //go:generate counterfeiter . UAAEndpointStore
    12  
    13  type UAAEndpointStore interface {
    14  	SetUAAEndpoint(uaaEndpoint string)
    15  }
    16  
    17  // SetupSettings represents configuration for establishing a connection to a UAA/Authentication server.
    18  type SetupSettings struct {
    19  	// DialTimeout is the DNS timeout used to make all requests to the Cloud
    20  	// Controller.
    21  	DialTimeout time.Duration
    22  
    23  	// SkipSSLValidation controls whether a client verifies the server's
    24  	// certificate chain and host name. If SkipSSLValidation is true, TLS accepts
    25  	// any certificate presented by the server and any host name in that
    26  	// certificate for *all* client requests going forward.
    27  	//
    28  	// In this mode, TLS is susceptible to man-in-the-middle attacks. This should
    29  	// be used only for testing.
    30  	SkipSSLValidation bool
    31  
    32  	// BootstrapURL is a fully qualified URL to a UAA/Authentication server.
    33  	BootstrapURL string
    34  }
    35  
    36  // AuthInfo represents a GET response from a login server
    37  type AuthInfo struct {
    38  	Links struct {
    39  		UAA string `json:"uaa"`
    40  	} `json:"links"`
    41  }
    42  
    43  // SetupResources configures the client to use the specified settings and diescopers the UAA and Authentication resources
    44  func (client *Client) SetupResources(store UAAEndpointStore, bootstrapURL string) error {
    45  	request, err := client.newRequest(requestOptions{
    46  		Method: http.MethodGet,
    47  		URL:    fmt.Sprintf("%s/login", bootstrapURL),
    48  	})
    49  
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	info := AuthInfo{} // Explicitly initializing
    55  	response := Response{
    56  		Result: &info,
    57  	}
    58  
    59  	err = client.connection.Make(request, &response)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	UAALink := info.Links.UAA
    65  	if UAALink == "" {
    66  		UAALink = bootstrapURL
    67  	}
    68  	store.SetUAAEndpoint(UAALink)
    69  
    70  	resources := map[string]string{
    71  		"uaa": UAALink,
    72  		"authorization_endpoint": bootstrapURL,
    73  	}
    74  
    75  	client.router = internal.NewRouter(internal.APIRoutes, resources)
    76  
    77  	return nil
    78  }