func CommandTargetConnection(selectedTarget string, commandInsecure *bool) (concourse.Connection, error) { if isURL(selectedTarget) { return NewConnection(selectedTarget, false) } flyrc := filepath.Join(userHomeDir(), ".flyrc") flyTargets, err := loadTargets(flyrc) if err != nil { return nil, err } target, ok := flyTargets.Targets[selectedTarget] if !ok { return nil, fmt.Errorf("Unable to find target %s in %s", selectedTarget, flyrc) } var token *oauth2.Token if target.Token != nil { token = &oauth2.Token{ TokenType: target.Token.Type, AccessToken: target.Token.Value, } } var tlsConfig *tls.Config if commandInsecure != nil { tlsConfig = &tls.Config{InsecureSkipVerify: *commandInsecure} } else if target.Insecure { tlsConfig = &tls.Config{InsecureSkipVerify: true} } var transport http.RoundTripper transport = &http.Transport{ TLSClientConfig: tlsConfig, } if token != nil { transport = &oauth2.Transport{ Source: oauth2.StaticTokenSource(token), Base: transport, } } httpClient := &http.Client{ Transport: transport, } return concourse.NewConnection(target.API, httpClient) }
func NewConnection(atcURL string, insecure bool) (concourse.Connection, error) { var tlsConfig *tls.Config if insecure { tlsConfig = &tls.Config{InsecureSkipVerify: insecure} } var transport http.RoundTripper transport = &http.Transport{ TLSClientConfig: tlsConfig, } return concourse.NewConnection(atcURL, &http.Client{ Transport: transport, }) }
func (cf *clientFactory) Build(r *http.Request) concourse.Client { transport := authorizationTransport{ Authorization: r.Header.Get("Authorization"), Base: &http.Transport{ // disable connection pooling DisableKeepAlives: true, }, } httpClient := &http.Client{ Transport: transport, } connection, err := concourse.NewConnection(cf.apiEndpoint, httpClient) if err != nil { // TODO really just shouldn't have this error case in the first place panic(err) } return concourse.NewClient(connection) }
func (command *LoginCommand) loginWith(method atc.AuthMethod, connection concourse.Connection) error { var token atc.AuthToken switch method.Type { case atc.AuthTypeOAuth: fmt.Println("navigate to the following URL in your browser:") fmt.Println("") fmt.Printf(" %s\n", method.AuthURL) fmt.Println("") for { var tokenStr string err := interact.NewInteraction("enter token").Resolve(interact.Required(&tokenStr)) if err != nil { return err } segments := strings.SplitN(tokenStr, " ", 2) if len(segments) != 2 { fmt.Println("token must be of the format 'TYPE VALUE', e.g. 'Bearer ...'") continue } token.Type = segments[0] token.Value = segments[1] break } case atc.AuthTypeBasic: var username string err := interact.NewInteraction("username").Resolve(interact.Required(&username)) if err != nil { return err } var password interact.Password err = interact.NewInteraction("password").Resolve(interact.Required(&password)) if err != nil { return err } newUnauthedClient, err := rc.NewConnection(connection.URL(), command.Insecure) if err != nil { return err } basicAuthClient, err := concourse.NewConnection( newUnauthedClient.URL(), &http.Client{ Transport: basicAuthTransport{ username: username, password: string(password), base: newUnauthedClient.HTTPClient().Transport, }, }, ) if err != nil { return err } client := concourse.NewClient(basicAuthClient) token, err = client.AuthToken() if err != nil { return err } } err := rc.SaveTarget( Fly.Target, connection.URL(), command.Insecure, &rc.TargetToken{ Type: token.Type, Value: token.Value, }, ) if err != nil { return err } fmt.Println("token saved") return nil }