func (command *LoginCommand) Execute(args []string) error { var connection concourse.Connection var err error if command.ATCURL != "" { connection, err = rc.NewConnection(command.ATCURL, command.Insecure) } else { connection, err = rc.CommandTargetConnection(Fly.Target, &command.Insecure) } if err != nil { return err } client := concourse.NewClient(connection) authMethods, err := client.ListAuthMethods() if err != nil { return err } var chosenMethod atc.AuthMethod switch len(authMethods) { case 0: fmt.Println("no auth methods configured; updating target data") err := rc.SaveTarget( Fly.Target, connection.URL(), command.Insecure, &rc.TargetToken{}, ) if err != nil { return err } return nil case 1: chosenMethod = authMethods[0] default: choices := make([]interact.Choice, len(authMethods)) for i, method := range authMethods { choices[i] = interact.Choice{ Display: method.DisplayName, Value: method, } } err = interact.NewInteraction("choose an auth method", choices...).Resolve(&chosenMethod) if err != nil { return err } } return command.loginWith(chosenMethod, connection) }
func (command *LoginCommand) saveTarget(url string, token *rc.TargetToken) error { err := rc.SaveTarget( Fly.Target, url, command.Insecure, &rc.TargetToken{ Type: token.Type, Value: token.Value, }, ) if err != nil { return err } fmt.Println("target saved") return nil }
os.Setenv("USERPROFILE", tmpDir) } else { os.Setenv("HOME", tmpDir) } flyrc = filepath.Join(userHomeDir(), ".flyrc") targetName = "foo" token := rc.TargetToken{ Type: "Bearer", Value: "some-token", } err = rc.SaveTarget( rc.TargetName(targetName), atcServer.URL(), true, &token, ) Expect(err).ToNot(HaveOccurred()) (*(*expectedPlan.Do)[0].Aggregate)[0].Get.Source = atc.Source{ "uri": atcServer.URL() + "/api/v1/pipes/some-pipe-id", "authorization": "Bearer some-token", } }) AfterEach(func() { os.RemoveAll(tmpDir) }) It("connects with the auth token", func() {
flyrc = filepath.Join(userHomeDir(), ".flyrc") }) AfterEach(func() { os.RemoveAll(tmpDir) }) Describe("Insecure Flag", func() { Describe("when 'insecure' is set to false in the flyrc", func() { var targetName string BeforeEach(func() { targetName = "foo" err := rc.SaveTarget( targetName, "some api url", false, nil, ) Expect(err).ToNot(HaveOccurred()) }) It("returns the rc insecure flag as false", func() { returnedTarget, err := rc.SelectTarget(targetName) Expect(err).NotTo(HaveOccurred()) Expect(returnedTarget.Insecure).To(BeFalse()) }) }) Describe("when 'insecure' is set to true in the flyrc", func() { var targetName string BeforeEach(func() {
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 }