(ctx context.Context)
| 205 | } |
| 206 | |
| 207 | func tryAuth(ctx context.Context) (*scalingo.User, string, error) { |
| 208 | var login string |
| 209 | var err error |
| 210 | |
| 211 | for login == "" { |
| 212 | appio.Infof("Username or email: ") |
| 213 | _, err := fmt.Scanln(&login) |
| 214 | if err != nil { |
| 215 | if strings.Contains(err.Error(), "unexpected newline") { |
| 216 | continue |
| 217 | } |
| 218 | return nil, "", errors.Wrapf(ctx, err, "read username") |
| 219 | } |
| 220 | login = strings.TrimRight(login, "\n") |
| 221 | } |
| 222 | |
| 223 | password, err := term.Password(ctx, " Password: ") |
| 224 | if err != nil { |
| 225 | return nil, "", errors.Wrapf(ctx, err, "read password") |
| 226 | } |
| 227 | fmt.Printf("\n") |
| 228 | |
| 229 | otpRequired := false |
| 230 | retryAuth := true |
| 231 | |
| 232 | c, err := ScalingoUnauthenticatedAuthClient(ctx) |
| 233 | if err != nil { |
| 234 | return nil, "", errors.Wrapf(ctx, err, "fail to create an unauthenticated Scalingo client") |
| 235 | } |
| 236 | |
| 237 | loginParams := scalingo.LoginParams{} |
| 238 | var apiToken scalingo.Token |
| 239 | for retryAuth { |
| 240 | loginParams.Identifier = login |
| 241 | loginParams.Password = password |
| 242 | |
| 243 | var otp string |
| 244 | if otpRequired { |
| 245 | appio.Infof("OTP: ") |
| 246 | fmt.Scan(&otp) |
| 247 | loginParams.OTP = otp |
| 248 | } |
| 249 | |
| 250 | hostname, err := os.Hostname() |
| 251 | if err != nil { |
| 252 | return nil, "", errors.Wrapf(ctx, err, "fail to get current hostname") |
| 253 | } |
| 254 | |
| 255 | apiToken, err = c.TokenCreateWithLogin(ctx, scalingo.TokenCreateParams{ |
| 256 | Name: "Scalingo CLI - " + hostname, |
| 257 | }, loginParams) |
| 258 | if err != nil { |
| 259 | if !otpRequired && scalingohttp.IsOTPRequired(err) { |
| 260 | otpRequired = true |
| 261 | } else { |
| 262 | return nil, "", errors.Wrapf(ctx, err, "fail to create API token") |
| 263 | } |
| 264 | } else { |
no test coverage detected