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

mendersoftware / deviceauth / 1571619161

13 Sep 2024 11:01AM UTC coverage: 81.326%. Remained the same
1571619161

push

gitlab-ci

web-flow
Merge pull request #727 from mzedel/chore/deprecate

Chore/deprecate

4834 of 5944 relevant lines covered (81.33%)

40.67 hits per line

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

88.0
/api/http/model_pre_auth_req.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 http
15

16
import (
17
        "encoding/json"
18
        "io"
19

20
        validation "github.com/go-ozzo/ozzo-validation/v4"
21
        "github.com/google/uuid"
22

23
        "github.com/mendersoftware/deviceauth/model"
24
        "github.com/mendersoftware/deviceauth/utils"
25
)
26

27
type preAuthReq struct {
28
        Force  bool                   `json:"force" valid:"-"`
29
        IdData map[string]interface{} `json:"identity_data" valid:"-"`
30
        PubKey string                 `json:"pubkey" valid:"required"`
31
}
32

33
func parsePreAuthReq(source io.Reader) (*preAuthReq, error) {
9✔
34
        jd := json.NewDecoder(source)
9✔
35

9✔
36
        var req preAuthReq
9✔
37

9✔
38
        if err := jd.Decode(&req); err != nil {
11✔
39
                return nil, err
2✔
40
        }
2✔
41

42
        if err := req.validate(); err != nil {
10✔
43
                return nil, err
3✔
44
        }
3✔
45

46
        return &req, nil
4✔
47
}
48

49
func (r *preAuthReq) validate() error {
7✔
50
        err := validation.ValidateStruct(r,
7✔
51
                validation.Field(&r.IdData, validation.Required),
7✔
52
                validation.Field(&r.PubKey, validation.Required),
7✔
53
        )
7✔
54
        if err != nil {
9✔
55
                return err
2✔
56
        }
2✔
57

58
        _, err = json.Marshal(r.IdData)
5✔
59
        if err != nil {
5✔
60
                return err
×
61
        }
×
62

63
        //normalize key
64
        key, err := utils.ParsePubKey(r.PubKey)
5✔
65
        if err != nil {
6✔
66
                return err
1✔
67
        }
1✔
68

69
        serialized, err := utils.SerializePubKey(key)
4✔
70
        if err != nil {
4✔
71
                return err
×
72
        }
×
73

74
        r.PubKey = serialized
4✔
75

4✔
76
        return nil
4✔
77
}
78

79
func (r *preAuthReq) getDbModel() (*model.PreAuthReq, error) {
4✔
80
        enc, err := json.Marshal(r.IdData)
4✔
81
        if err != nil {
4✔
82
                return nil, err
×
83
        }
×
84

85
        dId := uuid.New()
4✔
86
        asId := uuid.New()
4✔
87

4✔
88
        return &model.PreAuthReq{
4✔
89
                Force:     r.Force,
4✔
90
                DeviceId:  dId.String(),
4✔
91
                AuthSetId: asId.String(),
4✔
92
                IdData:    string(enc),
4✔
93
                PubKey:    r.PubKey,
4✔
94
        }, nil
4✔
95
}
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