Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 13 additions & 5 deletions encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/json"
"errors"
"fmt"
"sync/atomic"

"github.com/golang-jwt/jwt"
"gopkg.in/square/go-jose.v2"
Expand Down Expand Up @@ -48,7 +49,7 @@ const DefaultKey = `MIIEowIBAAKCAQEAtI1Jf2zmfwLzpAjVarORtjKtmCHQtgNxqWDdVNVa` +
type Keypair struct {
PrivateKey *rsa.PrivateKey
PublicKey *rsa.PublicKey
Kid string
Kid atomic.Value
}

// NewKeypair makes a Keypair off the provided rsa.PrivateKey or returns
Expand Down Expand Up @@ -98,8 +99,14 @@ func DefaultKeypair() (*Keypair, error) {

// If not manually set, computes the JWT headers' `kid`
func (k *Keypair) KeyID() (string, error) {
if k.Kid != "" {
return k.Kid, nil
var kid string
existingKid := k.Kid.Load()
if existingKid != nil {
kid = existingKid.(string)
}

if kid != "" {
return kid, nil
}

publicKeyDERBytes, err := x509.MarshalPKIXPublicKey(k.PublicKey)
Expand All @@ -113,9 +120,10 @@ func (k *Keypair) KeyID() (string, error) {
}
publicKeyDERHash := hasher.Sum(nil)

k.Kid = base64.RawURLEncoding.EncodeToString(publicKeyDERHash)
newKeyID := base64.RawURLEncoding.EncodeToString(publicKeyDERHash)
k.Kid.Store(newKeyID)

return k.Kid, nil
return newKeyID, nil
}

// JWKS is the JSON JWKS representation of the rsa.PublicKey
Expand Down
4 changes: 2 additions & 2 deletions encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func TestKeypair_SignJWTVerifyJWT(t *testing.T) {
assert.Equal(t, audience, claims["aud"])
assert.Equal(t, issuer, claims["iss"])

alice.Kid = "WRONG"
alice.Kid.Store("WRONG")
_, err = alice.VerifyJWT(tokenStr)
assert.Error(t, err)

const customKid = "USER_DEFINED"
bob.Kid = customKid
bob.Kid.Store(customKid)
kidTokenStr, err := bob.SignJWT(standardClaims)
assert.NoError(t, err)

Expand Down
6 changes: 6 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mockoidc
import (
"errors"
"strings"
"sync"
"time"

"github.com/golang-jwt/jwt"
Expand All @@ -21,6 +22,7 @@ type Session struct {

// SessionStore manages our Session objects
type SessionStore struct {
sync.RWMutex
Store map[string]*Session
CodeQueue *CodeQueue
}
Expand Down Expand Up @@ -55,14 +57,18 @@ func (ss *SessionStore) NewSession(scope string, nonce string, user User, codeCh
CodeChallenge: codeChallenge,
CodeChallengeMethod: codeChallengeMethod,
}
ss.Lock()
ss.Store[sessionID] = session
ss.Unlock()

return session, nil
}

// GetSessionByID looks up the Session
func (ss *SessionStore) GetSessionByID(id string) (*Session, error) {
ss.RLock()
session, ok := ss.Store[id]
ss.RUnlock()
if !ok {
return nil, errors.New("session not found")
}
Expand Down
2 changes: 2 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func TestSessionStore_NewSession(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, session.Scopes, []string{"openid", "email", "profile"})
assert.Equal(t, len(ss.Store), 1)
ss.RLock()
assert.Equal(t, ss.Store[session.SessionID], session)
ss.RUnlock()
assert.Equal(t, session.CodeChallenge, "sum")
assert.Equal(t, session.CodeChallengeMethod, "S256")
}
Expand Down