Repository navigation
Expand file tree
/
Copy pathalgkeys.go
More file actions
129 lines (121 loc) · 4.18 KB
/
Copy pathalgkeys.go
File metadata and controls
129 lines (121 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package jwt
import (
"crypto"
"crypto/ed25519"
"errors"
"fmt"
"github.com/ubgo/auth"
)
// Errors of AlgorithmKeys.
var (
// ErrNoAlgorithmKey is a Sign whose SignRequest.Alg names an algorithm
// that neither the signing key nor an AlgorithmKeys key signs, better-
// auth's descriptive error for an algorithm absent from keyPairConfig and
// keyPairConfigs.
ErrNoAlgorithmKey = errors.New("jwt: no signing key for this algorithm; add one with jwt.AlgorithmKeys")
// ErrAlgorithmKeys is a configuration error: an AlgorithmKeys key that is
// nil or a short Ed25519 key, or one key given twice (counting the
// signing key), which would publish one key id twice.
ErrAlgorithmKeys = errors.New("jwt: AlgorithmKeys holds valid keys, each once and none the signing key")
// ErrNoSigningKey is a Sign whose SignRequest.KeyID names no key the
// plugin signs with (the signing key or an AlgorithmKeys key), or one
// of another algorithm than SignRequest.Alg.
ErrNoSigningKey = errors.New("jwt: no signing key with this key id")
)
// AlgorithmKeys adds signing keys beside the signing key: a token signed by
// Sign with SignRequest.Alg set to an algorithm uses the first key of it (the
// signing key before these), SignRequest.KeyID picks one by key id, and every
// one is published in the key set (after the signer's keys, before
// PreviousKeys), so a verifier finds it by kid. The
// session token route and Token keep signing with the main key. Several
// keys may share an algorithm; one key given twice (the signing key
// included) fails auth.New with ErrAlgorithmKeys.
// This plugin stores no keys, so the app passes them (GenerateKey makes one)
// from its secret store.
//
//ds:def id=algorithmkeys-h7tn2xqc owner=@khanakia stability=stable
func AlgorithmKeys(keys ...crypto.Signer) Option {
return func(p *Plugin) {
if auth.RefuseNilEach(&p.optErrs, "jwt.AlgorithmKeys", keys) {
return
}
for _, k := range keys {
if ek, ok := k.(ed25519.PrivateKey); ok && len(ek) != ed25519.PrivateKeySize {
p.algKeysErr = fmt.Errorf("%w: a short Ed25519 key", ErrAlgorithmKeys)
continue
}
p.algKeys = append(p.algKeys, keySigner{key: k})
}
}
}
// validateAlgorithmKeys checks AlgorithmKeys against each other and the
// signing key.
func (p *Plugin) validateAlgorithmKeys() error {
if p.algKeysErr != nil {
return p.algKeysErr
}
seen := map[string]bool{}
if keys := p.signer.PublicKeys(); len(keys) > 0 {
seen[KeyID(keys[0])] = true
}
for _, s := range p.algKeys {
if _, err := AlgOf(s.key.Public()); err != nil {
return err
}
kid := KeyID(s.key.Public())
if seen[kid] {
return fmt.Errorf("%w: key %s given twice", ErrAlgorithmKeys, kid)
}
seen[kid] = true
}
return nil
}
// signerFor is the Signer that signs alg: the main signer for "" or its own
// algorithm, else the AlgorithmKeys key of alg, else ErrNoAlgorithmKey.
func (p *Plugin) signerFor(alg Alg) (Signer, error) {
if alg == "" {
return p.signer, nil
}
if keys := p.signer.PublicKeys(); len(keys) > 0 {
if main, err := AlgOf(keys[0]); err == nil && main == alg {
return p.signer, nil
}
}
for _, s := range p.algKeys {
if a, err := AlgOf(s.key.Public()); err == nil && a == alg {
return s, nil
}
}
return nil, fmt.Errorf("%w: %s", ErrNoAlgorithmKey, alg)
}
// signerForKey is the Signer whose current key has id kid, checked against
// alg when one is given; signerFor(alg) for an empty kid.
func (p *Plugin) signerForKey(alg Alg, kid string) (Signer, error) {
if kid == "" {
return p.signerFor(alg)
}
candidates := []Signer{p.signer}
for _, s := range p.algKeys {
candidates = append(candidates, s)
}
for _, s := range candidates {
keys := s.PublicKeys()
if len(keys) == 0 || KeyID(keys[0]) != kid {
continue
}
if a, _ := AlgOf(keys[0]); alg != "" && a != alg {
return nil, fmt.Errorf("%w: %s is not a %s key", ErrNoSigningKey, kid, alg)
}
return s, nil
}
return nil, fmt.Errorf("%w: %s", ErrNoSigningKey, kid)
}
// algKeyPublics is the public half of every AlgorithmKeys key, for the key
// set.
func (p *Plugin) algKeyPublics() []crypto.PublicKey {
out := make([]crypto.PublicKey, 0, len(p.algKeys))
for _, s := range p.algKeys {
out = append(out, s.key.Public())
}
return out
}