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

mendersoftware / useradm / 1645624551

13 Sep 2024 11:58AM UTC coverage: 70.971% (-14.3%) from 85.226%
1645624551

push

gitlab-ci

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

Chore/deprecate

2792 of 3934 relevant lines covered (70.97%)

40.83 hits per line

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

90.91
/model/token.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

15
package model
16

17
import (
18
        "encoding/json"
19
        "time"
20

21
        validation "github.com/go-ozzo/ozzo-validation/v4"
22
        "github.com/mendersoftware/go-lib-micro/mongo/oid"
23

24
        "github.com/mendersoftware/useradm/jwt"
25
)
26

27
type TokenRequest struct {
28
        Name      *string `json:"name"`
29
        ExpiresIn int64   `json:"expires_in"`
30
}
31

32
const defaultTokenMaxExpiration = 31536000
33

34
func (tr TokenRequest) Validate(maxExpiration int) error {
17✔
35
        if maxExpiration <= 0 {
17✔
36
                maxExpiration = defaultTokenMaxExpiration
×
37
        }
×
38
        return validation.ValidateStruct(&tr,
17✔
39
                validation.Field(&tr.Name, validation.Required, lessThan4096),
17✔
40
                validation.Field(&tr.ExpiresIn, validation.Min(0), validation.Max(maxExpiration)))
17✔
41
}
42

43
type PersonalAccessToken struct {
44
        // system-generated user ID
45
        ID oid.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
46
        // token name
47
        Name *string `json:"name,omitempty" on:"name,omitempty"`
48
        // timestamp of the last usage
49
        LastUsed *time.Time `json:"last_used,omitempty" bson:"last_used,omitempty"`
50
        // the absolute time when the token expires.
51
        ExpirationDate *jwt.Time `json:"expiration_date,omitempty" bson:"exp,omitempty"`
52
        // CreatedTs is the absolute time the token was created.
53
        CreatedTs jwt.Time `json:"created_ts,omitempty" bson:"iat,omitempty"`
54
}
55

56
type apiToken struct {
57
        // system-generated user ID
58
        ID oid.ObjectID `json:"id,omitempty"`
59
        // token name
60
        Name *string `json:"name,omitempty"`
61
        // timestamp of the last usage
62
        LastUsed *time.Time `json:"last_used,omitempty"`
63
        // the absolute time when the token expires
64
        ExpirationDate *time.Time `json:"expiration_date,omitempty"`
65
        // timestamp of the token creation
66
        CreatedTs *time.Time `json:"created_ts,omitempty"`
67
}
68

69
func newApiToken(t PersonalAccessToken) apiToken {
1✔
70
        var expiration *time.Time
1✔
71
        if t.ExpirationDate != nil {
2✔
72
                expiration = &t.ExpirationDate.Time
1✔
73
        }
1✔
74
        return apiToken{
1✔
75
                t.ID,
1✔
76
                t.Name,
1✔
77
                t.LastUsed,
1✔
78
                expiration,
1✔
79
                &t.CreatedTs.Time,
1✔
80
        }
1✔
81
}
82

83
func (t PersonalAccessToken) MarshalJSON() ([]byte, error) {
1✔
84
        return json.Marshal(newApiToken(t))
1✔
85
}
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

© 2025 Coveralls, Inc