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

mendersoftware / useradm / 1030325529

09 Oct 2023 11:24AM UTC coverage: 89.205% (-0.3%) from 89.534%
1030325529

Pull #383

gitlab-ci

tranchitella
feat: support for Ed25519 server keys for signing the JWT tokens

Ticket: MEN-5676
Changelog: Title

Signed-off-by: Fabio Tranchitella <fabio.tranchitella@northern.tech>
Pull Request #383: feat: support for Ed25519 server keys for signing the JWT tokens

97 of 118 new or added lines in 7 files covered. (82.2%)

2 existing lines in 1 file now uncovered.

2661 of 2983 relevant lines covered (89.21%)

118.85 hits per line

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

92.59
/jwt/jwt_rsa.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/rsa"
18

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

23
// JWTHandlerRS256 is an RS256-specific JWTHandler
24
type JWTHandlerRS256 struct {
25
        privKey *rsa.PrivateKey
26
}
27

28
func NewJWTHandlerRS256(privKey *rsa.PrivateKey) *JWTHandlerRS256 {
10✔
29
        return &JWTHandlerRS256{
10✔
30
                privKey: privKey,
10✔
31
        }
10✔
32
}
10✔
33

34
func (j *JWTHandlerRS256) ToJWT(token *Token) (string, error) {
172✔
35
        //generate
172✔
36
        jt := jwt.NewWithClaims(jwt.SigningMethodRS256, &token.Claims)
172✔
37

172✔
38
        //sign
172✔
39
        data, err := jt.SignedString(j.privKey)
172✔
40
        return data, err
172✔
41
}
172✔
42

43
func (j *JWTHandlerRS256) FromJWT(tokstr string) (*Token, error) {
154✔
44
        jwttoken, err := jwt.ParseWithClaims(tokstr, &Claims{},
154✔
45
                func(token *jwt.Token) (interface{}, error) {
304✔
46
                        if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
150✔
NEW
47
                                return nil, errors.New("unexpected signing method: " + token.Method.Alg())
×
NEW
48
                        }
×
49
                        return &j.privKey.PublicKey, nil
150✔
50
                },
51
        )
52

53
        if err == nil {
297✔
54
                token := Token{}
143✔
55
                if claims, ok := jwttoken.Claims.(*Claims); ok && jwttoken.Valid {
286✔
56
                        token.Claims = *claims
143✔
57
                        return &token, nil
143✔
58
                }
143✔
59
        }
60

61
        return nil, ErrTokenInvalid
11✔
62
}
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