HttpLogin sends a HTTP request to the server and returns the access JWT and refresh JWT extracted from the HTTP response
(params *LoginParams)
| 457 | // and returns the access JWT and refresh JWT extracted from |
| 458 | // the HTTP response |
| 459 | func HttpLogin(params *LoginParams) (*HttpToken, error) { |
| 460 | loginPayload := api.LoginRequest{} |
| 461 | if len(params.RefreshJwt) > 0 { |
| 462 | loginPayload.RefreshToken = params.RefreshJwt |
| 463 | } else { |
| 464 | loginPayload.Userid = params.UserID |
| 465 | loginPayload.Password = params.Passwd |
| 466 | } |
| 467 | |
| 468 | login := `mutation login($userId: String, $password: String, $namespace: Int, $refreshToken: String) { |
| 469 | login(userId: $userId, password: $password, namespace: $namespace, refreshToken: $refreshToken) { |
| 470 | response { |
| 471 | accessJWT |
| 472 | refreshJWT |
| 473 | } |
| 474 | } |
| 475 | }` |
| 476 | |
| 477 | gqlParams := GraphQLParams{ |
| 478 | Query: login, |
| 479 | Variables: map[string]interface{}{ |
| 480 | "userId": params.UserID, |
| 481 | "password": params.Passwd, |
| 482 | "namespace": params.Namespace, |
| 483 | "refreshToken": params.RefreshJwt, |
| 484 | }, |
| 485 | } |
| 486 | body, err := json.Marshal(gqlParams) |
| 487 | if err != nil { |
| 488 | return nil, errors.Wrapf(err, "unable to marshal body") |
| 489 | } |
| 490 | |
| 491 | req, err := http.NewRequest("POST", params.Endpoint, bytes.NewBuffer(body)) |
| 492 | if err != nil { |
| 493 | return nil, errors.Wrapf(err, "unable to create request") |
| 494 | } |
| 495 | req.Header.Set("Content-Type", "application/json") |
| 496 | |
| 497 | client := &http.Client{} |
| 498 | resp, err := client.Do(req) |
| 499 | if err != nil { |
| 500 | return nil, errors.Wrapf(err, "login through curl failed") |
| 501 | } |
| 502 | defer func() { |
| 503 | if err := resp.Body.Close(); err != nil { |
| 504 | glog.Warningf("error closing body: %v", err) |
| 505 | } |
| 506 | }() |
| 507 | |
| 508 | respBody, err := io.ReadAll(resp.Body) |
| 509 | if err != nil { |
| 510 | return nil, errors.Wrapf(err, "unable to read from response") |
| 511 | } |
| 512 | if resp.StatusCode != http.StatusOK { |
| 513 | return nil, errors.New(fmt.Sprintf("got non 200 response from the server with %s ", |
| 514 | string(respBody))) |
| 515 | } |
| 516 |