github.com/lestrrat-go/jwx/v2@v2.0.21/jwk/symmetric_gen.go (about) 1 // Code generated by tools/cmd/genjwk/main.go. DO NOT EDIT. 2 3 package jwk 4 5 import ( 6 "bytes" 7 "context" 8 "fmt" 9 "sort" 10 "sync" 11 12 "github.com/lestrrat-go/iter/mapiter" 13 "github.com/lestrrat-go/jwx/v2/cert" 14 "github.com/lestrrat-go/jwx/v2/internal/base64" 15 "github.com/lestrrat-go/jwx/v2/internal/iter" 16 "github.com/lestrrat-go/jwx/v2/internal/json" 17 "github.com/lestrrat-go/jwx/v2/internal/pool" 18 "github.com/lestrrat-go/jwx/v2/jwa" 19 ) 20 21 const ( 22 SymmetricOctetsKey = "k" 23 ) 24 25 type SymmetricKey interface { 26 Key 27 FromRaw([]byte) error 28 Octets() []byte 29 } 30 31 type symmetricKey struct { 32 algorithm *jwa.KeyAlgorithm // https://tools.ietf.org/html/rfc7517#section-4.4 33 keyID *string // https://tools.ietf.org/html/rfc7515#section-4.1.4 34 keyOps *KeyOperationList // https://tools.ietf.org/html/rfc7517#section-4.3 35 keyUsage *string // https://tools.ietf.org/html/rfc7517#section-4.2 36 octets []byte 37 x509CertChain *cert.Chain // https://tools.ietf.org/html/rfc7515#section-4.1.6 38 x509CertThumbprint *string // https://tools.ietf.org/html/rfc7515#section-4.1.7 39 x509CertThumbprintS256 *string // https://tools.ietf.org/html/rfc7515#section-4.1.8 40 x509URL *string // https://tools.ietf.org/html/rfc7515#section-4.1.5 41 privateParams map[string]interface{} 42 mu *sync.RWMutex 43 dc json.DecodeCtx 44 } 45 46 var _ SymmetricKey = &symmetricKey{} 47 var _ Key = &symmetricKey{} 48 49 func newSymmetricKey() *symmetricKey { 50 return &symmetricKey{ 51 mu: &sync.RWMutex{}, 52 privateParams: make(map[string]interface{}), 53 } 54 } 55 56 func (h symmetricKey) KeyType() jwa.KeyType { 57 return jwa.OctetSeq 58 } 59 60 func (h *symmetricKey) Algorithm() jwa.KeyAlgorithm { 61 if h.algorithm != nil { 62 return *(h.algorithm) 63 } 64 return jwa.InvalidKeyAlgorithm("") 65 } 66 67 func (h *symmetricKey) KeyID() string { 68 if h.keyID != nil { 69 return *(h.keyID) 70 } 71 return "" 72 } 73 74 func (h *symmetricKey) KeyOps() KeyOperationList { 75 if h.keyOps != nil { 76 return *(h.keyOps) 77 } 78 return nil 79 } 80 81 func (h *symmetricKey) KeyUsage() string { 82 if h.keyUsage != nil { 83 return *(h.keyUsage) 84 } 85 return "" 86 } 87 88 func (h *symmetricKey) Octets() []byte { 89 return h.octets 90 } 91 92 func (h *symmetricKey) X509CertChain() *cert.Chain { 93 return h.x509CertChain 94 } 95 96 func (h *symmetricKey) X509CertThumbprint() string { 97 if h.x509CertThumbprint != nil { 98 return *(h.x509CertThumbprint) 99 } 100 return "" 101 } 102 103 func (h *symmetricKey) X509CertThumbprintS256() string { 104 if h.x509CertThumbprintS256 != nil { 105 return *(h.x509CertThumbprintS256) 106 } 107 return "" 108 } 109 110 func (h *symmetricKey) X509URL() string { 111 if h.x509URL != nil { 112 return *(h.x509URL) 113 } 114 return "" 115 } 116 117 func (h *symmetricKey) makePairs() []*HeaderPair { 118 h.mu.RLock() 119 defer h.mu.RUnlock() 120 121 var pairs []*HeaderPair 122 pairs = append(pairs, &HeaderPair{Key: "kty", Value: jwa.OctetSeq}) 123 if h.algorithm != nil { 124 pairs = append(pairs, &HeaderPair{Key: AlgorithmKey, Value: *(h.algorithm)}) 125 } 126 if h.keyID != nil { 127 pairs = append(pairs, &HeaderPair{Key: KeyIDKey, Value: *(h.keyID)}) 128 } 129 if h.keyOps != nil { 130 pairs = append(pairs, &HeaderPair{Key: KeyOpsKey, Value: *(h.keyOps)}) 131 } 132 if h.keyUsage != nil { 133 pairs = append(pairs, &HeaderPair{Key: KeyUsageKey, Value: *(h.keyUsage)}) 134 } 135 if h.octets != nil { 136 pairs = append(pairs, &HeaderPair{Key: SymmetricOctetsKey, Value: h.octets}) 137 } 138 if h.x509CertChain != nil { 139 pairs = append(pairs, &HeaderPair{Key: X509CertChainKey, Value: h.x509CertChain}) 140 } 141 if h.x509CertThumbprint != nil { 142 pairs = append(pairs, &HeaderPair{Key: X509CertThumbprintKey, Value: *(h.x509CertThumbprint)}) 143 } 144 if h.x509CertThumbprintS256 != nil { 145 pairs = append(pairs, &HeaderPair{Key: X509CertThumbprintS256Key, Value: *(h.x509CertThumbprintS256)}) 146 } 147 if h.x509URL != nil { 148 pairs = append(pairs, &HeaderPair{Key: X509URLKey, Value: *(h.x509URL)}) 149 } 150 for k, v := range h.privateParams { 151 pairs = append(pairs, &HeaderPair{Key: k, Value: v}) 152 } 153 return pairs 154 } 155 156 func (h *symmetricKey) PrivateParams() map[string]interface{} { 157 return h.privateParams 158 } 159 160 func (h *symmetricKey) Get(name string) (interface{}, bool) { 161 h.mu.RLock() 162 defer h.mu.RUnlock() 163 switch name { 164 case KeyTypeKey: 165 return h.KeyType(), true 166 case AlgorithmKey: 167 if h.algorithm == nil { 168 return nil, false 169 } 170 return *(h.algorithm), true 171 case KeyIDKey: 172 if h.keyID == nil { 173 return nil, false 174 } 175 return *(h.keyID), true 176 case KeyOpsKey: 177 if h.keyOps == nil { 178 return nil, false 179 } 180 return *(h.keyOps), true 181 case KeyUsageKey: 182 if h.keyUsage == nil { 183 return nil, false 184 } 185 return *(h.keyUsage), true 186 case SymmetricOctetsKey: 187 if h.octets == nil { 188 return nil, false 189 } 190 return h.octets, true 191 case X509CertChainKey: 192 if h.x509CertChain == nil { 193 return nil, false 194 } 195 return h.x509CertChain, true 196 case X509CertThumbprintKey: 197 if h.x509CertThumbprint == nil { 198 return nil, false 199 } 200 return *(h.x509CertThumbprint), true 201 case X509CertThumbprintS256Key: 202 if h.x509CertThumbprintS256 == nil { 203 return nil, false 204 } 205 return *(h.x509CertThumbprintS256), true 206 case X509URLKey: 207 if h.x509URL == nil { 208 return nil, false 209 } 210 return *(h.x509URL), true 211 default: 212 v, ok := h.privateParams[name] 213 return v, ok 214 } 215 } 216 217 func (h *symmetricKey) Set(name string, value interface{}) error { 218 h.mu.Lock() 219 defer h.mu.Unlock() 220 return h.setNoLock(name, value) 221 } 222 223 func (h *symmetricKey) setNoLock(name string, value interface{}) error { 224 switch name { 225 case "kty": 226 return nil 227 case AlgorithmKey: 228 switch v := value.(type) { 229 case string, jwa.SignatureAlgorithm, jwa.ContentEncryptionAlgorithm: 230 var tmp = jwa.KeyAlgorithmFrom(v) 231 h.algorithm = &tmp 232 case fmt.Stringer: 233 s := v.String() 234 var tmp = jwa.KeyAlgorithmFrom(s) 235 h.algorithm = &tmp 236 default: 237 return fmt.Errorf(`invalid type for %s key: %T`, AlgorithmKey, value) 238 } 239 return nil 240 case KeyIDKey: 241 if v, ok := value.(string); ok { 242 h.keyID = &v 243 return nil 244 } 245 return fmt.Errorf(`invalid value for %s key: %T`, KeyIDKey, value) 246 case KeyOpsKey: 247 var acceptor KeyOperationList 248 if err := acceptor.Accept(value); err != nil { 249 return fmt.Errorf(`invalid value for %s key: %w`, KeyOpsKey, err) 250 } 251 h.keyOps = &acceptor 252 return nil 253 case KeyUsageKey: 254 switch v := value.(type) { 255 case KeyUsageType: 256 switch v { 257 case ForSignature, ForEncryption: 258 tmp := v.String() 259 h.keyUsage = &tmp 260 default: 261 return fmt.Errorf(`invalid key usage type %s`, v) 262 } 263 case string: 264 h.keyUsage = &v 265 default: 266 return fmt.Errorf(`invalid key usage type %s`, v) 267 } 268 case SymmetricOctetsKey: 269 if v, ok := value.([]byte); ok { 270 h.octets = v 271 return nil 272 } 273 return fmt.Errorf(`invalid value for %s key: %T`, SymmetricOctetsKey, value) 274 case X509CertChainKey: 275 if v, ok := value.(*cert.Chain); ok { 276 h.x509CertChain = v 277 return nil 278 } 279 return fmt.Errorf(`invalid value for %s key: %T`, X509CertChainKey, value) 280 case X509CertThumbprintKey: 281 if v, ok := value.(string); ok { 282 h.x509CertThumbprint = &v 283 return nil 284 } 285 return fmt.Errorf(`invalid value for %s key: %T`, X509CertThumbprintKey, value) 286 case X509CertThumbprintS256Key: 287 if v, ok := value.(string); ok { 288 h.x509CertThumbprintS256 = &v 289 return nil 290 } 291 return fmt.Errorf(`invalid value for %s key: %T`, X509CertThumbprintS256Key, value) 292 case X509URLKey: 293 if v, ok := value.(string); ok { 294 h.x509URL = &v 295 return nil 296 } 297 return fmt.Errorf(`invalid value for %s key: %T`, X509URLKey, value) 298 default: 299 if h.privateParams == nil { 300 h.privateParams = map[string]interface{}{} 301 } 302 h.privateParams[name] = value 303 } 304 return nil 305 } 306 307 func (k *symmetricKey) Remove(key string) error { 308 k.mu.Lock() 309 defer k.mu.Unlock() 310 switch key { 311 case AlgorithmKey: 312 k.algorithm = nil 313 case KeyIDKey: 314 k.keyID = nil 315 case KeyOpsKey: 316 k.keyOps = nil 317 case KeyUsageKey: 318 k.keyUsage = nil 319 case SymmetricOctetsKey: 320 k.octets = nil 321 case X509CertChainKey: 322 k.x509CertChain = nil 323 case X509CertThumbprintKey: 324 k.x509CertThumbprint = nil 325 case X509CertThumbprintS256Key: 326 k.x509CertThumbprintS256 = nil 327 case X509URLKey: 328 k.x509URL = nil 329 default: 330 delete(k.privateParams, key) 331 } 332 return nil 333 } 334 335 func (k *symmetricKey) Clone() (Key, error) { 336 return cloneKey(k) 337 } 338 339 func (k *symmetricKey) DecodeCtx() json.DecodeCtx { 340 k.mu.RLock() 341 defer k.mu.RUnlock() 342 return k.dc 343 } 344 345 func (k *symmetricKey) SetDecodeCtx(dc json.DecodeCtx) { 346 k.mu.Lock() 347 defer k.mu.Unlock() 348 k.dc = dc 349 } 350 351 func (h *symmetricKey) UnmarshalJSON(buf []byte) error { 352 h.mu.Lock() 353 defer h.mu.Unlock() 354 h.algorithm = nil 355 h.keyID = nil 356 h.keyOps = nil 357 h.keyUsage = nil 358 h.octets = nil 359 h.x509CertChain = nil 360 h.x509CertThumbprint = nil 361 h.x509CertThumbprintS256 = nil 362 h.x509URL = nil 363 dec := json.NewDecoder(bytes.NewReader(buf)) 364 LOOP: 365 for { 366 tok, err := dec.Token() 367 if err != nil { 368 return fmt.Errorf(`error reading token: %w`, err) 369 } 370 switch tok := tok.(type) { 371 case json.Delim: 372 // Assuming we're doing everything correctly, we should ONLY 373 // get either '{' or '}' here. 374 if tok == '}' { // End of object 375 break LOOP 376 } else if tok != '{' { 377 return fmt.Errorf(`expected '{', but got '%c'`, tok) 378 } 379 case string: // Objects can only have string keys 380 switch tok { 381 case KeyTypeKey: 382 val, err := json.ReadNextStringToken(dec) 383 if err != nil { 384 return fmt.Errorf(`error reading token: %w`, err) 385 } 386 if val != jwa.OctetSeq.String() { 387 return fmt.Errorf(`invalid kty value for RSAPublicKey (%s)`, val) 388 } 389 case AlgorithmKey: 390 var s string 391 if err := dec.Decode(&s); err != nil { 392 return fmt.Errorf(`failed to decode value for key %s: %w`, AlgorithmKey, err) 393 } 394 alg := jwa.KeyAlgorithmFrom(s) 395 h.algorithm = &alg 396 case KeyIDKey: 397 if err := json.AssignNextStringToken(&h.keyID, dec); err != nil { 398 return fmt.Errorf(`failed to decode value for key %s: %w`, KeyIDKey, err) 399 } 400 case KeyOpsKey: 401 var decoded KeyOperationList 402 if err := dec.Decode(&decoded); err != nil { 403 return fmt.Errorf(`failed to decode value for key %s: %w`, KeyOpsKey, err) 404 } 405 h.keyOps = &decoded 406 case KeyUsageKey: 407 if err := json.AssignNextStringToken(&h.keyUsage, dec); err != nil { 408 return fmt.Errorf(`failed to decode value for key %s: %w`, KeyUsageKey, err) 409 } 410 case SymmetricOctetsKey: 411 if err := json.AssignNextBytesToken(&h.octets, dec); err != nil { 412 return fmt.Errorf(`failed to decode value for key %s: %w`, SymmetricOctetsKey, err) 413 } 414 case X509CertChainKey: 415 var decoded cert.Chain 416 if err := dec.Decode(&decoded); err != nil { 417 return fmt.Errorf(`failed to decode value for key %s: %w`, X509CertChainKey, err) 418 } 419 h.x509CertChain = &decoded 420 case X509CertThumbprintKey: 421 if err := json.AssignNextStringToken(&h.x509CertThumbprint, dec); err != nil { 422 return fmt.Errorf(`failed to decode value for key %s: %w`, X509CertThumbprintKey, err) 423 } 424 case X509CertThumbprintS256Key: 425 if err := json.AssignNextStringToken(&h.x509CertThumbprintS256, dec); err != nil { 426 return fmt.Errorf(`failed to decode value for key %s: %w`, X509CertThumbprintS256Key, err) 427 } 428 case X509URLKey: 429 if err := json.AssignNextStringToken(&h.x509URL, dec); err != nil { 430 return fmt.Errorf(`failed to decode value for key %s: %w`, X509URLKey, err) 431 } 432 default: 433 if dc := h.dc; dc != nil { 434 if localReg := dc.Registry(); localReg != nil { 435 decoded, err := localReg.Decode(dec, tok) 436 if err == nil { 437 h.setNoLock(tok, decoded) 438 continue 439 } 440 } 441 } 442 decoded, err := registry.Decode(dec, tok) 443 if err == nil { 444 h.setNoLock(tok, decoded) 445 continue 446 } 447 return fmt.Errorf(`could not decode field %s: %w`, tok, err) 448 } 449 default: 450 return fmt.Errorf(`invalid token %T`, tok) 451 } 452 } 453 if h.octets == nil { 454 return fmt.Errorf(`required field k is missing`) 455 } 456 return nil 457 } 458 459 func (h symmetricKey) MarshalJSON() ([]byte, error) { 460 data := make(map[string]interface{}) 461 fields := make([]string, 0, 9) 462 for _, pair := range h.makePairs() { 463 fields = append(fields, pair.Key.(string)) 464 data[pair.Key.(string)] = pair.Value 465 } 466 467 sort.Strings(fields) 468 buf := pool.GetBytesBuffer() 469 defer pool.ReleaseBytesBuffer(buf) 470 buf.WriteByte('{') 471 enc := json.NewEncoder(buf) 472 for i, f := range fields { 473 if i > 0 { 474 buf.WriteRune(',') 475 } 476 buf.WriteRune('"') 477 buf.WriteString(f) 478 buf.WriteString(`":`) 479 v := data[f] 480 switch v := v.(type) { 481 case []byte: 482 buf.WriteRune('"') 483 buf.WriteString(base64.EncodeToString(v)) 484 buf.WriteRune('"') 485 default: 486 if err := enc.Encode(v); err != nil { 487 return nil, fmt.Errorf(`failed to encode value for field %s: %w`, f, err) 488 } 489 buf.Truncate(buf.Len() - 1) 490 } 491 } 492 buf.WriteByte('}') 493 ret := make([]byte, buf.Len()) 494 copy(ret, buf.Bytes()) 495 return ret, nil 496 } 497 498 func (h *symmetricKey) Iterate(ctx context.Context) HeaderIterator { 499 pairs := h.makePairs() 500 ch := make(chan *HeaderPair, len(pairs)) 501 go func(ctx context.Context, ch chan *HeaderPair, pairs []*HeaderPair) { 502 defer close(ch) 503 for _, pair := range pairs { 504 select { 505 case <-ctx.Done(): 506 return 507 case ch <- pair: 508 } 509 } 510 }(ctx, ch, pairs) 511 return mapiter.New(ch) 512 } 513 514 func (h *symmetricKey) Walk(ctx context.Context, visitor HeaderVisitor) error { 515 return iter.WalkMap(ctx, h, visitor) 516 } 517 518 func (h *symmetricKey) AsMap(ctx context.Context) (map[string]interface{}, error) { 519 return iter.AsMap(ctx, h) 520 }