Skip to content

Commit

Permalink
Merge pull request dexidp#187 from bcwaldon/relo-jose-sig
Browse files Browse the repository at this point in the history
jose: collapse jose/sig into jose pkg
  • Loading branch information
bcwaldon committed Feb 4, 2015
2 parents 2fed42c + dd21d77 commit 0300995
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 62 deletions.
20 changes: 20 additions & 0 deletions jose/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,23 @@ func (j *JWT) Encode() string {
s := encodeSegment(j.Signature)
return strings.Join([]string{d, s}, ".")
}

func NewSignedJWT(claims map[string]interface{}, s Signer) (*JWT, error) {
header := JOSEHeader{
HeaderKeyAlgorithm: s.Alg(),
HeaderKeyID: s.ID(),
}

jwt, err := NewJWT(header, Claims(claims))
if err != nil {
return nil, err
}

sig, err := s.Sign([]byte(jwt.Data()))
if err != nil {
return nil, err
}
jwt.Signature = sig

return &jwt, nil
}
6 changes: 2 additions & 4 deletions jose/sig/interface.go → jose/sig.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package sig
package jose

import (
"fmt"
"strings"

"github.com/coreos-inc/auth/jose"
)

type Verifier interface {
Expand All @@ -18,7 +16,7 @@ type Signer interface {
Sign(data []byte) (sig []byte, err error)
}

func NewVerifier(jwk jose.JWK) (Verifier, error) {
func NewVerifier(jwk JWK) (Verifier, error) {
if strings.ToUpper(jwk.Type) != "RSA" {
return nil, fmt.Errorf("unsupported key type %q", jwk.Type)
}
Expand Down
25 changes: 0 additions & 25 deletions jose/sig/jwt.go

This file was deleted.

6 changes: 2 additions & 4 deletions jose/sig/hmac.go → jose/sig_hmac.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sig
package jose

import (
"bytes"
Expand All @@ -8,8 +8,6 @@ import (
"errors"
"fmt"
"strings"

"github.com/coreos-inc/auth/jose"
)

type VerifierHMAC struct {
Expand All @@ -22,7 +20,7 @@ type SignerHMAC struct {
VerifierHMAC
}

func NewVerifierHMAC(jwk jose.JWK) (*VerifierHMAC, error) {
func NewVerifierHMAC(jwk JWK) (*VerifierHMAC, error) {
if strings.ToUpper(jwk.Alg) != "HS256" {
return nil, fmt.Errorf("unsupported key algorithm %q", jwk.Alg)
}
Expand Down
12 changes: 5 additions & 7 deletions jose/sig/hmac_test.go → jose/sig_hmac_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package sig
package jose

import (
"bytes"
"encoding/base64"
"testing"

"github.com/coreos-inc/auth/jose"
)

var hmacTestCases = []struct {
data string
sig string
jwk jose.JWK
jwk JWK
valid bool
desc string
}{
{
"test",
"Aymga2LNFrM-tnkr6MYLFY2Jou46h2_Omogeu0iMCRQ=",
jose.JWK{
JWK{
ID: "fake-key",
Alg: "HS256",
Secret: []byte("secret"),
Expand All @@ -29,7 +27,7 @@ var hmacTestCases = []struct {
{
"test",
"Aymga2LNFrM-tnkr6MYLFY2Jou46h2_Omogeu0iMCRQ=",
jose.JWK{
JWK{
ID: "different-key",
Alg: "HS256",
Secret: []byte("secret"),
Expand All @@ -40,7 +38,7 @@ var hmacTestCases = []struct {
{
"test sig and non-matching data",
"Aymga2LNFrM-tnkr6MYLFY2Jou46h2_Omogeu0iMCRQ=",
jose.JWK{
JWK{
ID: "fake-key",
Alg: "HS256",
Secret: []byte("secret"),
Expand Down
6 changes: 2 additions & 4 deletions jose/sig/rsa.go → jose/sig_rsa.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package sig
package jose

import (
"crypto"
"crypto/rand"
"crypto/rsa"
"fmt"
"strings"

"github.com/coreos-inc/auth/jose"
)

type VerifierRSA struct {
Expand All @@ -21,7 +19,7 @@ type SignerRSA struct {
VerifierRSA
}

func NewVerifierRSA(jwk jose.JWK) (*VerifierRSA, error) {
func NewVerifierRSA(jwk JWK) (*VerifierRSA, error) {
if strings.ToUpper(jwk.Alg) != "RS256" {
return nil, fmt.Errorf("unsupported key algorithm %q", jwk.Alg)
}
Expand Down
9 changes: 4 additions & 5 deletions key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/coreos-inc/auth/jose"
josesig "github.com/coreos-inc/auth/jose/sig"
)

func NewPublicKey(jwk jose.JWK) *PublicKey {
Expand All @@ -23,8 +22,8 @@ func (k *PublicKey) ID() string {
return k.jwk.ID
}

func (k *PublicKey) Verifier() (josesig.Verifier, error) {
return josesig.NewVerifierRSA(k.jwk)
func (k *PublicKey) Verifier() (jose.Verifier, error) {
return jose.NewVerifierRSA(k.jwk)
}

type PrivateKey struct {
Expand All @@ -36,8 +35,8 @@ func (k *PrivateKey) ID() string {
return k.KeyID
}

func (k *PrivateKey) Signer() josesig.Signer {
return josesig.NewSignerRSA(k.ID(), *k.PrivateKey)
func (k *PrivateKey) Signer() jose.Signer {
return jose.NewSignerRSA(k.ID(), *k.PrivateKey)
}

func (k *PrivateKey) JWK() jose.JWK {
Expand Down
5 changes: 2 additions & 3 deletions key/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"github.com/jonboulle/clockwork"

"github.com/coreos-inc/auth/jose"
josesig "github.com/coreos-inc/auth/jose/sig"
"github.com/coreos-inc/auth/pkg/health"
)

type PrivateKeyManager interface {
ExpiresAt() time.Time
Signer() (josesig.Signer, error)
Signer() (jose.Signer, error)
JWKs() ([]jose.JWK, error)
PublicKeys() ([]PublicKey, error)

Expand All @@ -40,7 +39,7 @@ func (m *privateKeyManager) ExpiresAt() time.Time {
return m.keySet.ExpiresAt()
}

func (m *privateKeyManager) Signer() (josesig.Signer, error) {
func (m *privateKeyManager) Signer() (jose.Signer, error) {
if err := m.Healthy(); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions server/auth_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

josesig "github.com/coreos-inc/auth/jose/sig"
"github.com/coreos-inc/auth/jose"
"github.com/coreos-inc/auth/key"
"github.com/coreos-inc/auth/oidc"
)
Expand Down Expand Up @@ -40,7 +40,7 @@ func TestClientToken(t *testing.T) {

makeToken := func(iss, sub, aud string, iat, exp time.Time) string {
claims := oidc.NewClaims(iss, sub, aud, iat, exp)
jwt, err := josesig.NewSignedJWT(claims, signer)
jwt, err := jose.NewSignedJWT(claims, signer)
if err != nil {
t.Fatalf("Failed to generate JWT, error=%v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/coreos-inc/auth/connector"
"github.com/coreos-inc/auth/jose"
josesig "github.com/coreos-inc/auth/jose/sig"
"github.com/coreos-inc/auth/key"
"github.com/coreos-inc/auth/oauth2"
"github.com/coreos-inc/auth/oidc"
Expand Down Expand Up @@ -219,7 +218,7 @@ func (s *Server) ClientCredsToken(creds oidc.ClientCredentials) (*jose.JWT, erro
claims := oidc.NewClaims(s.IssuerURL.String(), creds.ID, creds.ID, now, exp)
claims.Add("name", creds.ID)

jwt, err := josesig.NewSignedJWT(claims, signer)
jwt, err := jose.NewSignedJWT(claims, signer)
if err != nil {
log.Errorf("Failed to generate ID token: %v", err)
return nil, oauth2.NewError(oauth2.ErrorServerError)
Expand Down Expand Up @@ -259,7 +258,7 @@ func (s *Server) CodeToken(creds oidc.ClientCredentials, sessionKey string) (*jo
return nil, oauth2.NewError(oauth2.ErrorServerError)
}

jwt, err := josesig.NewSignedJWT(ses.Claims(s.IssuerURL.String()), signer)
jwt, err := jose.NewSignedJWT(ses.Claims(s.IssuerURL.String()), signer)
if err != nil {
log.Errorf("Failed to generate ID token: %v", err)
return nil, oauth2.NewError(oauth2.ErrorServerError)
Expand Down
7 changes: 3 additions & 4 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/coreos-inc/auth/jose"
josesig "github.com/coreos-inc/auth/jose/sig"
"github.com/coreos-inc/auth/key"
"github.com/coreos-inc/auth/oauth2"
"github.com/coreos-inc/auth/oidc"
Expand All @@ -18,15 +17,15 @@ import (
type StaticKeyManager struct {
key.PrivateKeyManager
expiresAt time.Time
signer josesig.Signer
signer jose.Signer
keys []jose.JWK
}

func (m *StaticKeyManager) ExpiresAt() time.Time {
return m.expiresAt
}

func (m *StaticKeyManager) Signer() (josesig.Signer, error) {
func (m *StaticKeyManager) Signer() (jose.Signer, error) {
return m.signer, nil
}

Expand Down Expand Up @@ -312,7 +311,7 @@ func TestServerTokenFail(t *testing.T) {
signerFixture := &StaticSigner{sig: []byte("beer"), err: nil}

tests := []struct {
signer josesig.Signer
signer jose.Signer
argCC oidc.ClientCredentials
argKey string
err string
Expand Down
2 changes: 1 addition & 1 deletion test
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ COVER=${COVER:-"-cover"}

source ./build

TESTABLE="connector db integration jose jose/sig key oauth2 oidc pkg/crypto pkg/flag pkg/health pkg/http pkg/net pkg/time server session"
TESTABLE="connector db integration jose key oauth2 oidc pkg/crypto pkg/flag pkg/health pkg/http pkg/net pkg/time server session"
FORMATTABLE="$TESTABLE cmd/authctl cmd/authd-worker cmd/authd-overlord examples/app functional pkg/log"

# user has not provided PKG override
Expand Down

0 comments on commit 0300995

Please sign in to comment.