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

mendersoftware / mender-cli / 1619209074

10 Jan 2025 08:54AM CUT coverage: 31.535%. Remained the same
1619209074

Pull #267

gitlab-ci

mender-test-bot
chore(master): release 1.13.0

Signed-off-by: mender-test-bot <mender@northern.tech>
Pull Request #267: chore(master): release 1.13.0

795 of 2521 relevant lines covered (31.54%)

1.68 hits per line

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

77.08
/client/useradm/client.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package useradm
15

16
import (
17
        "bytes"
18
        "context"
19
        "fmt"
20
        "io/ioutil"
21
        "net/http"
22
        "net/http/httputil"
23
        "time"
24

25
        "github.com/pkg/errors"
26

27
        "github.com/mendersoftware/mender-cli/client"
28
        "github.com/mendersoftware/mender-cli/log"
29
)
30

31
const (
32
        loginUrl = "/api/management/v1/useradm/auth/login"
33
        timeout  = 10 * time.Second
34
)
35

36
type Client struct {
37
        url      string
38
        loginUrl string
39
        client   *http.Client
40
}
41

42
func NewClient(url string, skipVerify bool) *Client {
5✔
43
        return &Client{
5✔
44
                url:      url,
5✔
45
                loginUrl: client.JoinURL(url, loginUrl),
5✔
46
                client:   client.NewHttpClient(skipVerify),
5✔
47
        }
5✔
48
}
5✔
49

50
func (c *Client) Login(user, pass string, token string) ([]byte, error) {
5✔
51
        var reader *bytes.Reader
5✔
52
        var req *http.Request
5✔
53
        var err error
5✔
54

5✔
55
        if len(token) > 1 {
5✔
56
                tokenJson := "{\"token2fa\":\"" + token + "\"}"
×
57
                reader = bytes.NewReader([]byte(tokenJson))
×
58
                req, err = http.NewRequest(http.MethodPost, c.loginUrl, reader)
×
59
        } else {
5✔
60
                req, err = http.NewRequest(http.MethodPost, c.loginUrl, nil)
5✔
61
        }
5✔
62
        if err != nil {
5✔
63
                return nil, err
×
64
        }
×
65

66
        ctx, cancel := context.WithTimeout(context.Background(), timeout)
5✔
67
        defer cancel()
5✔
68

5✔
69
        req.SetBasicAuth(user, pass)
5✔
70
        req.Header.Set("Content-Type", "application/json")
5✔
71

5✔
72
        reqDump, _ := httputil.DumpRequest(req, true)
5✔
73
        log.Verbf("sending request: \n%v", string(reqDump))
5✔
74

5✔
75
        rsp, err := c.client.Do(req.WithContext(ctx))
5✔
76
        if err != nil {
5✔
77
                return nil, errors.Wrap(err, "POST /auth/login request failed")
×
78
        }
×
79
        defer rsp.Body.Close()
5✔
80

5✔
81
        rspDump, _ := httputil.DumpResponse(rsp, true)
5✔
82
        log.Verbf("response: \n%v\n", string(rspDump))
5✔
83

5✔
84
        body, err := ioutil.ReadAll(rsp.Body)
5✔
85
        if err != nil {
5✔
86
                return nil, errors.Wrap(err, "can't read request body")
×
87
        }
×
88

89
        if rsp.StatusCode != http.StatusOK {
5✔
90
                return nil, errors.New(fmt.Sprintf("login failed with status %d", rsp.StatusCode))
×
91
        }
×
92

93
        return body, nil
5✔
94
}
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

© 2025 Coveralls, Inc