-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow JWT signing method to be configurable.
This change creates a new `Signer` interface which encapsulates jwt.SigningMethod + the key material use to sign JWT tokens. This allows clients to do is modify how JWT tokens are signed by passing in their own Signer. In particular, I'm interested in coupling this with something like https://github.com/golang-jwt/jwt#extensions to allow for JWT signing backed by KMS systems (Vault, Cloud KMS, etc). Also introduces a new `AppsTransportOptions` to make it easier to make new transport creation options without needing to make new funcs each time. For now only added `WithSigner`, but we could easily extend this out to other config options (Client, BaseURL, etc.)
- Loading branch information
Showing
3 changed files
with
99 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ghinstallation | ||
|
||
import ( | ||
"crypto/rsa" | ||
|
||
jwt "github.com/golang-jwt/jwt/v4" | ||
) | ||
|
||
// Signer is a JWT token signer. This is a wrapper around [jwt.SigningMethod] with predetermined | ||
// key material. | ||
type Signer interface { | ||
// Sign signs the given claims and returns a JWT token string, as specified | ||
// by [jwt.Token.SignedString] | ||
Sign(claims jwt.Claims) (string, error) | ||
} | ||
|
||
// RSASigner signs JWT tokens using RSA keys. | ||
type RSASigner struct { | ||
method *jwt.SigningMethodRSA | ||
key *rsa.PrivateKey | ||
} | ||
|
||
func NewRSASigner(method *jwt.SigningMethodRSA, key *rsa.PrivateKey) *RSASigner { | ||
return &RSASigner{ | ||
method: method, | ||
key: key, | ||
} | ||
} | ||
|
||
// Sign signs the JWT claims with the RSA key. | ||
func (s *RSASigner) Sign(claims jwt.Claims) (string, error) { | ||
return jwt.NewWithClaims(s.method, claims).SignedString(s.key) | ||
} |