github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/uaa/client.go (about)

     1  // Package uaa is a GoLang library that interacts with CloudFoundry User
     2  // Account and Authentication (UAA) Server.
     3  //
     4  // It is currently designed to support UAA API X.X.X. However, it may include
     5  // features and endpoints of later API versions.
     6  package uaa
     7  
     8  import (
     9  	"fmt"
    10  	"runtime"
    11  	"time"
    12  
    13  	"code.cloudfoundry.org/cli/api/uaa/internal"
    14  	"github.com/tedsuo/rata"
    15  )
    16  
    17  // Client is the UAA client
    18  type Client struct {
    19  	URL    string
    20  	id     string
    21  	secret string
    22  
    23  	connection Connection
    24  	router     *rata.RequestGenerator
    25  	userAgent  string
    26  }
    27  
    28  // Config allows the Client to be configured
    29  type Config struct {
    30  	// AppName is the name of the application/process using the client.
    31  	AppName string
    32  
    33  	// AppVersion is the version of the application/process using the client.
    34  	AppVersion string
    35  
    36  	// DialTimeout is the DNS lookup timeout for the client. If not set, it is
    37  	// infinite.
    38  	DialTimeout time.Duration
    39  
    40  	// ClientID is the UAA client ID the client will use.
    41  	ClientID string
    42  
    43  	// ClientSecret is the UAA client secret the client will use.
    44  	ClientSecret string
    45  
    46  	// SkipSSLValidation controls whether a client verifies the server's
    47  	// certificate chain and host name. If SkipSSLValidation is true, TLS accepts
    48  	// any certificate presented by the server and any host name in that
    49  	// certificate for *all* client requests going forward.
    50  	//
    51  	// In this mode, TLS is susceptible to man-in-the-middle attacks. This should
    52  	// be used only for testing.
    53  	SkipSSLValidation bool
    54  
    55  	// URL is the api URL for the UAA target.
    56  	URL string
    57  }
    58  
    59  // NewClient returns a new UAA Client with the provided configuration
    60  func NewClient(config Config) *Client {
    61  	userAgent := fmt.Sprintf("%s/%s (%s; %s %s)",
    62  		config.AppName,
    63  		config.AppVersion,
    64  		runtime.Version(),
    65  		runtime.GOARCH,
    66  		runtime.GOOS,
    67  	)
    68  
    69  	client := Client{
    70  		URL:    config.URL,
    71  		id:     config.ClientID,
    72  		secret: config.ClientSecret,
    73  
    74  		router:     rata.NewRequestGenerator(config.URL, internal.Routes),
    75  		connection: NewConnection(config.SkipSSLValidation, config.DialTimeout),
    76  		userAgent:  userAgent,
    77  	}
    78  	client.WrapConnection(NewErrorWrapper())
    79  
    80  	return &client
    81  }