-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathmanager.go
108 lines (82 loc) · 2.72 KB
/
manager.go
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
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package jwk
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/ory/hydra/v2/aead"
"github.com/ory/hydra/v2/x"
"github.com/ory/x/errorsx"
jose "github.com/go-jose/go-jose/v3"
"github.com/gofrs/uuid"
"github.com/ory/fosite"
)
var ErrUnsupportedKeyAlgorithm = &fosite.RFC6749Error{
CodeField: http.StatusBadRequest,
ErrorField: http.StatusText(http.StatusBadRequest),
DescriptionField: "Unsupported key algorithm",
}
var ErrUnsupportedEllipticCurve = &fosite.RFC6749Error{
CodeField: http.StatusBadRequest,
ErrorField: http.StatusText(http.StatusBadRequest),
DescriptionField: "Unsupported elliptic curve",
}
var ErrMinimalRsaKeyLength = &fosite.RFC6749Error{
CodeField: http.StatusBadRequest,
ErrorField: http.StatusText(http.StatusBadRequest),
DescriptionField: "Unsupported RSA key length",
}
type (
Manager interface {
GenerateAndPersistKeySet(ctx context.Context, set, kid, alg, use string) (*jose.JSONWebKeySet, error)
AddKey(ctx context.Context, set string, key *jose.JSONWebKey) error
AddKeySet(ctx context.Context, set string, keys *jose.JSONWebKeySet) error
UpdateKey(ctx context.Context, set string, key *jose.JSONWebKey) error
UpdateKeySet(ctx context.Context, set string, keys *jose.JSONWebKeySet) error
GetKey(ctx context.Context, set, kid string) (*jose.JSONWebKeySet, error)
GetKeySet(ctx context.Context, set string) (*jose.JSONWebKeySet, error)
DeleteKey(ctx context.Context, set, kid string) error
DeleteKeySet(ctx context.Context, set string) error
}
SQLData struct {
ID uuid.UUID `db:"pk"`
NID uuid.UUID `json:"-" db:"nid"`
// This field is deprecated and will be removed
PKDeprecated int64 `json:"-" db:"pk_deprecated"`
Set string `db:"sid"`
KID string `db:"kid"`
Version int `db:"version"`
CreatedAt time.Time `db:"created_at"`
Key string `db:"keydata"`
}
SQLDataRows []SQLData
)
func (d SQLData) TableName() string {
return "hydra_jwk"
}
func (d SQLDataRows) ToJWK(ctx context.Context, r interface {
KeyCipher() *aead.AESGCM
}) (keys *jose.JSONWebKeySet, err error) {
if len(d) == 0 {
return nil, errors.Wrap(x.ErrNotFound, "")
}
keys = &jose.JSONWebKeySet{Keys: []jose.JSONWebKey{}}
for _, d := range d {
key, err := r.KeyCipher().Decrypt(ctx, d.Key, nil)
if err != nil {
return nil, errorsx.WithStack(err)
}
var c jose.JSONWebKey
if err := json.Unmarshal(key, &c); err != nil {
return nil, errorsx.WithStack(err)
}
keys.Keys = append(keys.Keys, c)
}
if len(keys.Keys) == 0 {
return nil, errorsx.WithStack(x.ErrNotFound)
}
return keys, nil
}