• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

UiPath / uipathcli / 11233377525

08 Oct 2024 10:16AM UTC coverage: 90.395% (-0.04%) from 90.437%
11233377525

push

github

thschmitt
Upgrade GitHub Action to latest versions

Some versions of the GitHub Actions are deprecated and getting removed.
Upgrading all actions to the latest versions.

4282 of 4737 relevant lines covered (90.39%)

1.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.7
/auth/identity_client.go
1
package auth
2

3
import (
4
        "crypto/tls"
5
        "encoding/json"
6
        "fmt"
7
        "io"
8
        "net/http"
9
        "net/url"
10
        "strings"
11

12
        "github.com/UiPath/uipathcli/cache"
13
        "github.com/UiPath/uipathcli/utils"
14
)
15

16
type identityClient struct {
17
        cache cache.Cache
18
}
19

20
const TokenRoute = "/connect/token"
21

22
func (c identityClient) GetToken(tokenRequest tokenRequest) (*tokenResponse, error) {
1✔
23
        form := url.Values{}
1✔
24
        form.Add("grant_type", tokenRequest.GrantType)
1✔
25
        form.Add("scope", tokenRequest.Scopes)
1✔
26
        form.Add("client_id", tokenRequest.ClientId)
1✔
27
        form.Add("client_secret", tokenRequest.ClientSecret)
1✔
28
        form.Add("code", tokenRequest.Code)
1✔
29
        form.Add("code_verifier", tokenRequest.CodeVerifier)
1✔
30
        form.Add("redirect_uri", tokenRequest.RedirectUri)
1✔
31
        for key, value := range tokenRequest.Properties {
2✔
32
                form.Add(key, value)
1✔
33
        }
1✔
34

35
        cacheKey := c.cacheKey(tokenRequest)
1✔
36
        token, expiresIn := c.cache.Get(cacheKey)
1✔
37
        if token != "" {
2✔
38
                return newTokenResponse(token, expiresIn), nil
1✔
39
        }
1✔
40

41
        response, err := c.retrieveToken(tokenRequest.BaseUri, form, tokenRequest.Insecure)
1✔
42
        if err != nil {
2✔
43
                return nil, err
1✔
44
        }
1✔
45
        c.cache.Set(cacheKey, response.AccessToken, response.ExpiresIn)
1✔
46
        return response, nil
1✔
47
}
48

49
func (c identityClient) retrieveToken(baseUri url.URL, form url.Values, insecure bool) (*tokenResponse, error) {
1✔
50
        var response *tokenResponse
1✔
51
        err := utils.Retry(func() error {
2✔
52
                var err error
1✔
53
                response, err = c.send(baseUri, form, insecure)
1✔
54
                return err
1✔
55
        })
1✔
56
        return response, err
1✔
57
}
58

59
func (c identityClient) send(baseUri url.URL, form url.Values, insecure bool) (*tokenResponse, error) {
1✔
60
        uri := baseUri.JoinPath(TokenRoute)
1✔
61
        request, err := http.NewRequest("POST", uri.String(), strings.NewReader(form.Encode()))
1✔
62
        if err != nil {
1✔
63
                return nil, fmt.Errorf("Error preparing request: %w", err)
×
64
        }
×
65
        request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
1✔
66

1✔
67
        transport := &http.Transport{
1✔
68
                TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, //nolint // This is user configurable and disabled by default
1✔
69
        }
1✔
70
        client := http.Client{Transport: transport}
1✔
71
        response, err := client.Do(request)
1✔
72
        if err != nil {
1✔
73
                return nil, utils.Retryable(fmt.Errorf("Error sending request: %w", err))
×
74
        }
×
75
        defer response.Body.Close()
1✔
76
        bytes, err := io.ReadAll(response.Body)
1✔
77
        if err != nil {
1✔
78
                return nil, utils.Retryable(fmt.Errorf("Error reading response: %w", err))
×
79
        }
×
80
        if response.StatusCode >= 500 {
2✔
81
                return nil, utils.Retryable(fmt.Errorf("Token service returned status code '%v' and body '%v'", response.StatusCode, string(bytes)))
1✔
82
        }
1✔
83
        if response.StatusCode != http.StatusOK {
2✔
84
                return nil, fmt.Errorf("Token service returned status code '%v' and body '%v'", response.StatusCode, string(bytes))
1✔
85
        }
1✔
86

87
        var result tokenResponse
1✔
88
        err = json.Unmarshal(bytes, &result)
1✔
89
        if err != nil {
1✔
90
                return nil, fmt.Errorf("Error parsing json response: %w", err)
×
91
        }
×
92
        return &result, nil
1✔
93
}
94

95
func (c identityClient) cacheKey(tokenRequest tokenRequest) string {
1✔
96
        return fmt.Sprintf("token|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s",
1✔
97
                tokenRequest.BaseUri.Scheme,
1✔
98
                tokenRequest.BaseUri.Hostname(),
1✔
99
                tokenRequest.GrantType,
1✔
100
                tokenRequest.Scopes,
1✔
101
                tokenRequest.ClientId,
1✔
102
                tokenRequest.ClientSecret,
1✔
103
                tokenRequest.Code,
1✔
104
                tokenRequest.CodeVerifier,
1✔
105
                tokenRequest.RedirectUri,
1✔
106
                c.cacheKeyProperties(tokenRequest.Properties))
1✔
107
}
1✔
108

109
func (c identityClient) cacheKeyProperties(properties map[string]string) string {
1✔
110
        values := []string{}
1✔
111
        for key, value := range properties {
2✔
112
                values = append(values, key+"="+value)
1✔
113
        }
1✔
114
        return strings.Join(values, ",")
1✔
115
}
116

117
func newIdentityClient(cache cache.Cache) *identityClient {
1✔
118
        return &identityClient{cache}
1✔
119
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc