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

mendersoftware / useradm / 1565757771

29 Nov 2024 07:56AM UTC coverage: 87.019%. Remained the same
1565757771

push

gitlab-ci

web-flow
Merge pull request #434 from alfrunes/1.22.x

chore(deps): Upgrade golang to latest

2869 of 3297 relevant lines covered (87.02%)

131.0 hits per line

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

65.12
/jwt/jwt_ed25519.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 jwt
15

16
import (
17
        "crypto/ed25519"
18
        "strconv"
19

20
        "github.com/golang-jwt/jwt/v4"
21
        "github.com/pkg/errors"
22

23
        "github.com/mendersoftware/useradm/common"
24
)
25

26
// JWTHandlerEd25519 is an Ed25519-specific JWTHandler
27
type JWTHandlerEd25519 struct {
28
        privKey      map[int]*ed25519.PrivateKey
29
        currentKeyId int
30
}
31

32
func NewJWTHandlerEd25519(privKey *ed25519.PrivateKey, keyId int) *JWTHandlerEd25519 {
10✔
33
        return &JWTHandlerEd25519{
10✔
34
                privKey:      map[int]*ed25519.PrivateKey{keyId: privKey},
10✔
35
                currentKeyId: keyId,
10✔
36
        }
10✔
37
}
10✔
38

39
func (j *JWTHandlerEd25519) ToJWT(token *Token) (string, error) {
2✔
40
        //generate
2✔
41
        jt := jwt.NewWithClaims(jwt.SigningMethodEdDSA, &token.Claims)
2✔
42
        jt.Header["kid"] = token.KeyId
2✔
43
        if _, exists := j.privKey[token.KeyId]; !exists {
2✔
44
                return "", common.ErrKeyIdNotFound
×
45
        }
×
46
        //sign
47
        data, err := jt.SignedString(j.privKey[token.KeyId])
2✔
48
        return data, err
2✔
49
}
50

51
func (j *JWTHandlerEd25519) FromJWT(tokstr string) (*Token, error) {
6✔
52
        jwttoken, err := jwt.ParseWithClaims(tokstr, &Claims{},
6✔
53
                func(token *jwt.Token) (interface{}, error) {
11✔
54
                        keyId := common.KeyIdZero
5✔
55
                        if _, ok := token.Header["kid"]; ok {
5✔
56
                                if _, isFloat := token.Header["kid"].(float64); isFloat {
×
57
                                        keyId = int(token.Header["kid"].(float64))
×
58
                                }
×
59
                                if _, isInt := token.Header["kid"].(int64); isInt {
×
60
                                        keyId = int(token.Header["kid"].(int64))
×
61
                                }
×
62
                                if _, isInt := token.Header["kid"].(int); isInt {
×
63
                                        keyId = token.Header["kid"].(int)
×
64
                                }
×
65
                        }
66
                        if _, ok := token.Method.(*jwt.SigningMethodEd25519); !ok {
5✔
67
                                return nil, errors.New("unexpected signing method: " + token.Method.Alg())
×
68
                        }
×
69
                        if _, exists := j.privKey[keyId]; !exists {
5✔
70
                                return nil, errors.New("cannot find the key with id " + strconv.Itoa(keyId))
×
71
                        }
×
72
                        return j.privKey[keyId].Public(), nil
5✔
73
                },
74
        )
75

76
        if err == nil {
9✔
77
                token := Token{}
3✔
78
                if claims, ok := jwttoken.Claims.(*Claims); ok && jwttoken.Valid {
6✔
79
                        token.Claims = *claims
3✔
80
                        return &token, nil
3✔
81
                }
3✔
82
        }
83

84
        return nil, ErrTokenInvalid
3✔
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