func (s *Server) GetAuthToken(w http.ResponseWriter, r *http.Request) { authorization := r.Header.Get("Authorization") authSegs := strings.SplitN(authorization, " ", 2) if len(authSegs) != 2 { w.WriteHeader(http.StatusBadRequest) } var token atc.AuthToken if strings.ToLower(authSegs[0]) == strings.ToLower(auth.TokenTypeBearer) { token.Type = authSegs[0] token.Value = authSegs[1] } else { tokenType, tokenValue, err := s.tokenGenerator.GenerateToken(time.Now().Add(tokenDuration)) if err != nil { w.WriteHeader(http.StatusInternalServerError) return } token.Type = string(tokenType) token.Value = string(tokenValue) } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(token) }
func (s *Server) GetAuthToken(w http.ResponseWriter, r *http.Request) { logger := s.logger.Session("get-auth-token") authorization := r.Header.Get("Authorization") authSegs := strings.SplitN(authorization, " ", 2) if len(authSegs) != 2 { logger.Debug("malformed-authorization-header") w.WriteHeader(http.StatusBadRequest) } var token atc.AuthToken if strings.ToLower(authSegs[0]) == strings.ToLower(auth.TokenTypeBearer) { token.Type = authSegs[0] token.Value = authSegs[1] } else { teamName := atc.DefaultTeamName team, found, err := s.db.GetTeamByName(teamName) if err != nil { logger.Error("get-team-by-name", err) w.WriteHeader(http.StatusInternalServerError) return } if !found { logger.Info("cannot-find-team-by-name", lager.Data{ "teamName": teamName, }) w.WriteHeader(http.StatusUnauthorized) return } tokenType, tokenValue, err := s.tokenGenerator.GenerateToken(time.Now().Add(tokenDuration), team.Name, team.ID, team.Admin) if err != nil { logger.Error("generate-token", err) w.WriteHeader(http.StatusInternalServerError) return } token.Type = string(tokenType) token.Value = string(tokenValue) } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(token) }
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 }
func (command *LoginCommand) loginWith(method atc.AuthMethod, client concourse.Client) 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 if command.Username != "" { username = command.Username } else { err := interact.NewInteraction("username").Resolve(interact.Required(&username)) if err != nil { return err } } var password string if command.Password != "" { password = command.Password } else { var interactivePassword interact.Password err := interact.NewInteraction("password").Resolve(interact.Required(&interactivePassword)) if err != nil { return err } password = string(interactivePassword) } newUnauthedClient := rc.NewUnauthenticatedClient(client.URL(), command.Insecure) basicAuthClient := concourse.NewClient( newUnauthedClient.URL(), &http.Client{ Transport: basicAuthTransport{ username: username, password: password, base: newUnauthedClient.HTTPClient().Transport, }, }, ) var err error token, err = basicAuthClient.AuthToken() if err != nil { return err } } return command.saveTarget( client.URL(), &rc.TargetToken{ Type: token.Type, Value: token.Value, }, ) }