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

mendersoftware / mender-server / 1807452801

08 May 2025 01:22PM UTC coverage: 65.386% (+0.1%) from 65.27%
1807452801

Pull #653

gitlab-ci

bahaa-ghazal
refactor(inventory): Migrate from ant0nie/go-json-rest to gin-gonic/gin

Ticket: MEN-8236
Changelog: Title
Signed-off-by: Bahaa Aldeen Ghazal <bahaa.ghazal@northern.tech>
Pull Request #653: refactor(inventory): Migrate from ant0nie/go-json-rest to gin-gonic/gin

476 of 525 new or added lines in 6 files covered. (90.67%)

62 existing lines in 9 files now uncovered.

31949 of 48862 relevant lines covered (65.39%)

1.37 hits per line

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

81.82
/backend/pkg/identity/token.go
1
// Copyright 2024 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 identity
15

16
import (
17
        "encoding/base64"
18
        "encoding/json"
19
        "net/http"
20
        "strings"
21

22
        "github.com/pkg/errors"
23

24
        "github.com/mendersoftware/mender-server/pkg/addons"
25
)
26

27
type Identity struct {
28
        Subject  string         `json:"sub" valid:"required"`
29
        Tenant   string         `json:"mender.tenant,omitempty"`
30
        IsUser   bool           `json:"mender.user,omitempty"`
31
        IsDevice bool           `json:"mender.device,omitempty"`
32
        Plan     string         `json:"mender.plan,omitempty"`
33
        Addons   []addons.Addon `json:"mender.addons,omitempty"`
34
        Trial    bool           `json:"mender.trial"`
35
}
36

37
// ExtractJWTFromHeader inspect the Authorization header for a Bearer token and
38
// if not present looks for a "JWT" cookie.
39
func ExtractJWTFromHeader(r *http.Request) (jwt string, err error) {
8✔
40
        auth := r.Header.Get("Authorization")
8✔
41
        if auth == "" {
12✔
42
                jwtCookie, err := r.Cookie("JWT")
4✔
43
                if err != nil {
7✔
44
                        return "", errors.New("Authorization not present in header")
3✔
45
                }
3✔
46
                jwt = jwtCookie.Value
1✔
47
        } else {
8✔
48
                auths := strings.Split(auth, " ")
8✔
49

8✔
50
                if len(auths) != 2 {
10✔
51
                        return "", errors.Errorf("malformed Authorization header")
2✔
52
                }
2✔
53

54
                if !strings.EqualFold(auths[0], "Bearer") {
10✔
55
                        return "", errors.Errorf("unknown Authorization method %s", auths[0])
2✔
56
                }
2✔
57
                jwt = auths[1]
8✔
58
        }
59
        return jwt, nil
8✔
60
}
61

62
// Generate identity information from given JWT by extracting subject and tenant claims.
63
// Note that this function does not perform any form of token signature
64
// verification.
65
func ExtractIdentity(token string) (id Identity, err error) {
8✔
66
        var (
8✔
67
                claims []byte
8✔
68
                jwt    []string
8✔
69
        )
8✔
70
        jwt = strings.Split(token, ".")
8✔
71
        if len(jwt) != 3 {
10✔
72
                return id, errors.New("identity: incorrect token format")
2✔
73
        }
2✔
74
        claims, err = base64.RawURLEncoding.DecodeString(jwt[1])
8✔
75
        if err != nil {
8✔
76
                return id, errors.Wrap(err,
×
77
                        "identity: failed to decode base64 JWT claims")
×
78
        }
×
79
        err = json.Unmarshal(claims, &id)
8✔
80
        if err != nil {
8✔
UNCOV
81
                return id, errors.Wrap(err,
×
UNCOV
82
                        "identity: failed to decode JSON JWT claims")
×
UNCOV
83
        }
×
84
        return id, id.Validate()
8✔
85
}
86

87
func (id Identity) Validate() error {
8✔
88
        if id.Subject == "" {
8✔
89
                return errors.New("identity: claim \"sub\" is required")
×
90
        }
×
91
        return nil
8✔
92
}
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