From 8932f2ad468d4a96dd231c3bd94560db5f548f93 Mon Sep 17 00:00:00 2001 From: Stavros Date: Sat, 16 May 2026 20:43:50 +0300 Subject: [PATCH] feat: ensure public key pairs with private key in oidc service --- internal/service/oidc_service.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/internal/service/oidc_service.go b/internal/service/oidc_service.go index af1a8320..9f90d94f 100644 --- a/internal/service/oidc_service.go +++ b/internal/service/oidc_service.go @@ -239,6 +239,16 @@ func NewOIDCService( } } + rPublicKey, ok := publicKey.(*rsa.PublicKey) + + if !ok { + return nil, fmt.Errorf("public key is not an rsa public key") + } + + if rPublicKey.N.Cmp(privateKey.N) != 0 || rPublicKey.E != privateKey.E { + return nil, fmt.Errorf("public key does not pair with private key") + } + // We will reorganize the client into a map with the client ID as the key clients := make(map[string]model.OIDCClientConfig) @@ -271,7 +281,7 @@ func NewOIDCService( clients: clients, privateKey: privateKey, - publicKey: publicKey.(*rsa.PublicKey), + publicKey: rPublicKey, issuer: issuer, } @@ -822,13 +832,13 @@ func (service *OIDCService) GetJWK() ([]byte, error) { hasher.Write(der) jwk := jose.JSONWebKey{ - Key: service.privateKey, + Key: service.publicKey, Algorithm: string(jose.RS256), Use: "sig", KeyID: base64.URLEncoding.EncodeToString(hasher.Sum(nil)), } - return jwk.Public().MarshalJSON() + return jwk.MarshalJSON() } func (service *OIDCService) ValidatePKCE(codeChallenge string, codeVerifier string) bool {