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

mendersoftware / useradm / 1808938521

08 May 2025 10:54AM UTC coverage: 59.747% (-27.3%) from 87.019%
1808938521

push

gitlab-ci

alfrunes
Merge `alfrunes:1.22.x` into `mendersoftware:1.22.x`

2363 of 3955 relevant lines covered (59.75%)

12.76 hits per line

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

0.0
/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 {
×
35
        if maxExpiration <= 0 {
×
36
                maxExpiration = defaultTokenMaxExpiration
×
37
        }
×
38
        return validation.ValidateStruct(&tr,
×
39
                validation.Field(&tr.Name, validation.Required, lessThan4096),
×
40
                validation.Field(&tr.ExpiresIn, validation.Min(0), validation.Max(maxExpiration)))
×
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 {
×
70
        var expiration *time.Time
×
71
        if t.ExpirationDate != nil {
×
72
                expiration = &t.ExpirationDate.Time
×
73
        }
×
74
        return apiToken{
×
75
                t.ID,
×
76
                t.Name,
×
77
                t.LastUsed,
×
78
                expiration,
×
79
                &t.CreatedTs.Time,
×
80
        }
×
81
}
82

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