Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b5d939e
feat: add jwt pkg
sreya Oct 1, 2024
6025c7b
update make gen
sreya Oct 2, 2024
8b235be
Refactor JWT package to modularize key functions
sreya Oct 2, 2024
843de38
Remove unused JWT test file from repository
sreya Oct 2, 2024
099544f
Refactor JWT key functions and add tests
sreya Oct 2, 2024
acc4db3
Rename VerifyFn to ParseFn in JWT tests
sreya Oct 2, 2024
b4973a8
Remove unused JWE test file
sreya Oct 2, 2024
f7d7c95
Refactor JWT test structs to use public field names
sreya Oct 2, 2024
3ba8ad3
Refactor JWT to use new crypto key management system
sreya Oct 3, 2024
73c902c
Refactor JWT package for improved modularity and clarity
sreya Oct 3, 2024
e348a7a
mv dir
sreya Oct 3, 2024
c7489b4
update references
sreya Oct 3, 2024
d890ea2
refactor interfaces
sreya Oct 3, 2024
67ccd5c
refactor dbkeycache
sreya Oct 3, 2024
1a81c7a
Refactor JWT utility options for flexibility
sreya Oct 3, 2024
e529c4a
Enhance key generation and JWT error messages
sreya Oct 3, 2024
437e587
Update cryptographic key length requirements
sreya Oct 3, 2024
54214e2
Refactor key provider interfaces in JWT utilities
sreya Oct 3, 2024
93603a2
Refactor dbCache to remove feature validation
sreya Oct 3, 2024
e654a65
Refactor cryptokeys and jwtutils interfaces and logic
sreya Oct 3, 2024
0efabfd
Remove unused test code and mock cleanup
sreya Oct 3, 2024
e065356
Remove cryptokeys keycachemock from Makefile
sreya Oct 3, 2024
938bdda
Add feature validation to dbCache key methods
sreya Oct 3, 2024
48b1b3b
fmt
sreya Oct 3, 2024
1dd2205
Add initialization comment for db key cache timer
sreya Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor cryptokeys and jwtutils interfaces and logic
- Enhance comments for key interfaces to clarify usage
  and considerations for time validity and clock skew.
- Refactor JWE/JWS logic to simplify serialization and
  deserialization processes, ensuring more efficient and
  concise handling of JWTs. Implement compact serialization
  and remove unnecessary base64 encoding.
  • Loading branch information
sreya committed Oct 3, 2024
commit e654a6552ca36914b689228eb1b4b07041f90a83
14 changes: 14 additions & 0 deletions coderd/cryptokeys/keycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@ var (
)

type EncryptionKeycache interface {
// EncryptingKey returns the latest valid key for encrypting payloads. A valid
// key is one that is both past its start time and before its deletion time.
EncryptingKey(ctx context.Context) (id string, key interface{}, err error)
// DecryptingKey returns the key with the provided id which maps to its sequence
// number. The key is valid for decryption as long as it is not deleted or past
// its deletion date. We must allow for keys prior to their start time to
// account for clock skew between peers (one key may be past its start time on
// one machine while another is not).
DecryptingKey(ctx context.Context, id string) (key interface{}, err error)
io.Closer
}

type SigningKeycache interface {
// SigningKey returns the latest valid key for signing. A valid key is one
// that is both past its start time and before its deletion time.
SigningKey(ctx context.Context) (id string, key interface{}, err error)
// VerifyingKey returns the key with the provided id which should map to its
// sequence number. The key is valid for verifying as long as it is not deleted
// or past its deletion date. We must allow for keys prior to their start time
// to account for clock skew between peers (one key may be past its start time
// on one machine while another is not).
VerifyingKey(ctx context.Context, id string) (key interface{}, err error)
io.Closer
}
20 changes: 8 additions & 12 deletions coderd/jwtutils/jwe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jwtutils

import (
"context"
"encoding/base64"
"encoding/json"
"time"

Expand Down Expand Up @@ -58,15 +57,17 @@ func Encrypt(ctx context.Context, e EncryptKeyProvider, claims Claims) (string,
return "", xerrors.Errorf("encrypt: %w", err)
}

serialized := []byte(encrypted.FullSerialize())
return base64.RawURLEncoding.EncodeToString(serialized), nil
compact, err := encrypted.CompactSerialize()
if err != nil {
return "", xerrors.Errorf("compact serialize: %w", err)
}

return compact, nil
}

// DecryptOptions are options for decrypting a JWE.
type DecryptOptions struct {
RegisteredClaims jwt.Expected

// The following should only be used for JWEs.
RegisteredClaims jwt.Expected
KeyAlgorithm jose.KeyAlgorithm
ContentEncryptionAlgorithm jose.ContentEncryption
}
Expand All @@ -85,12 +86,7 @@ func Decrypt(ctx context.Context, d DecryptKeyProvider, token string, claims Cla
opt(&options)
}

encrypted, err := base64.RawURLEncoding.DecodeString(token)
if err != nil {
return xerrors.Errorf("decode: %w", err)
}

object, err := jose.ParseEncrypted(string(encrypted),
object, err := jose.ParseEncrypted(token,
[]jose.KeyAlgorithm{options.KeyAlgorithm},
[]jose.ContentEncryption{options.ContentEncryptionAlgorithm},
)
Expand Down
4 changes: 1 addition & 3 deletions coderd/jwtutils/jws.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ func Sign(ctx context.Context, s SigningKeyProvider, claims Claims) (string, err

// VerifyOptions are options for verifying a JWT.
type VerifyOptions struct {
RegisteredClaims jwt.Expected

// The following are only used for JWSs.
RegisteredClaims jwt.Expected
SignatureAlgorithm jose.SignatureAlgorithm
}

Expand Down
Loading