You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

46 line
1.1 KiB

  1. package apiclient
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "github.com/AFASystems/presence/internal/pkg/config"
  9. )
  10. type response struct {
  11. Token string `json:"access_token"`
  12. }
  13. func GetToken(ctx context.Context, cfg *config.Config, client *http.Client) (string, error) {
  14. formData := url.Values{}
  15. formData.Set("grant_type", "password")
  16. formData.Set("client_id", "Fastapi")
  17. formData.Set("client_secret", "wojuoB7Z5xhlPFrF2lIxJSSdVHCApEgC")
  18. formData.Set("username", "core")
  19. formData.Set("password", "C0r3_us3r_Cr3d3nt14ls")
  20. formData.Set("audience", "Fastapi")
  21. req, err := http.NewRequest("POST", "https://10.251.0.30:10002/realms/API.Server.local/protocol/openid-connect/token", strings.NewReader(formData.Encode()))
  22. if err != nil {
  23. return "", err
  24. }
  25. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  26. req = req.WithContext(ctx)
  27. res, err := client.Do(req)
  28. if err != nil {
  29. return "", err
  30. }
  31. var j response
  32. if err := json.NewDecoder(res.Body).Decode(&j); err != nil {
  33. return "", err
  34. }
  35. return j.Token, nil
  36. }