Skip to content

Commit

Permalink
oauth2: Add and enhance access/refresh token tests
Browse files Browse the repository at this point in the history
This patch introduces more tests for code and refresh flows and the JWT
strategy.

Signed-off-by: arekkas <aeneas@ory.am>
  • Loading branch information
arekkas committed Jul 23, 2018
1 parent d3b4e77 commit 388ee27
Show file tree
Hide file tree
Showing 11 changed files with 972 additions and 693 deletions.
6 changes: 6 additions & 0 deletions client/manager_0_sql_migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package client_test
import (
"fmt"
"log"
"sync"
"testing"

"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -116,6 +117,7 @@ var migrations = map[string]*migrate.MemoryMigrationSource{
}

func TestMigrations(t *testing.T) {
var m sync.Mutex
var dbs = map[string]*sqlx.DB{}
if testing.Short() {
return
Expand All @@ -127,14 +129,18 @@ func TestMigrations(t *testing.T) {
if err != nil {
log.Fatalf("Could not connect to database: %v", err)
}
m.Lock()
dbs["postgres"] = db
m.Unlock()
},
func() {
db, err := dockertest.ConnectToTestMySQL()
if err != nil {
log.Fatalf("Could not connect to database: %v", err)
}
m.Lock()
dbs["mysql"] = db
m.Unlock()
},
})

Expand Down
6 changes: 6 additions & 0 deletions client/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"flag"
"fmt"
"log"
"sync"
"testing"

_ "github.com/go-sql-driver/mysql"
Expand All @@ -35,6 +36,7 @@ import (
)

var clientManagers = map[string]Manager{}
var m sync.Mutex

func init() {
clientManagers["memory"] = NewMemoryManager(&fosite.BCrypt{})
Expand All @@ -61,7 +63,9 @@ func connectToMySQL() {
}

s := &SQLManager{DB: db, Hasher: &fosite.BCrypt{WorkFactor: 4}}
m.Lock()
clientManagers["mysql"] = s
m.Unlock()
}

func connectToPG() {
Expand All @@ -71,7 +75,9 @@ func connectToPG() {
}

s := &SQLManager{DB: db, Hasher: &fosite.BCrypt{WorkFactor: 4}}
m.Lock()
clientManagers["postgres"] = s
m.Unlock()
}

func TestCreateGetDeleteClient(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions consent/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"flag"
"fmt"
"log"
"sync"
"testing"
"time"

Expand All @@ -39,6 +40,7 @@ import (
"github.com/stretchr/testify/require"
)

var m sync.Mutex
var clientManager = client.NewMemoryManager(&fosite.BCrypt{WorkFactor: 8})
var fositeManager = oauth2.NewFositeMemoryStore(clientManager, time.Hour)
var managers = map[string]Manager{
Expand Down Expand Up @@ -152,7 +154,9 @@ func connectToPostgres(managers map[string]Manager, c client.Manager) {
return
}

m.Lock()
managers["postgres"] = s
m.Unlock()
}

func connectToMySQL(managers map[string]Manager, c client.Manager) {
Expand All @@ -168,7 +172,9 @@ func connectToMySQL(managers map[string]Manager, c client.Manager) {
return
}

m.Lock()
managers["mysql"] = s
m.Unlock()
}

func TestMain(m *testing.M) {
Expand Down
6 changes: 6 additions & 0 deletions jwk/manager_0_sql_migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package jwk_test
import (
"fmt"
"log"
"sync"
"testing"

"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -76,6 +77,7 @@ var migrations = &migrate.MemoryMigrationSource{
}

func TestMigrations(t *testing.T) {
var m sync.Mutex
var dbs = map[string]*sqlx.DB{}
if testing.Short() {
return
Expand All @@ -87,14 +89,18 @@ func TestMigrations(t *testing.T) {
if err != nil {
log.Fatalf("Could not connect to database: %v", err)
}
m.Lock()
dbs["postgres"] = db
m.Unlock()
},
func() {
db, err := dockertest.ConnectToTestMySQL()
if err != nil {
log.Fatalf("Could not connect to database: %v", err)
}
m.Lock()
dbs["mysql"] = db
m.Unlock()
},
})

Expand Down
8 changes: 8 additions & 0 deletions jwk/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"flag"
"fmt"
"log"
"sync"
"testing"

_ "github.com/go-sql-driver/mysql"
Expand All @@ -37,6 +38,7 @@ var managers = map[string]Manager{
"memory": new(MemoryManager),
}

var m sync.Mutex
var testGenerator = &RS256Generator{}

var encryptionKey, _ = RandomBytes(32)
Expand All @@ -62,7 +64,10 @@ func connectToPG() {
}

s := &SQLManager{DB: db, Cipher: &AEAD{Key: encryptionKey}}

m.Lock()
managers["postgres"] = s
m.Unlock()
}

func connectToMySQL() {
Expand All @@ -72,7 +77,10 @@ func connectToMySQL() {
}

s := &SQLManager{DB: db, Cipher: &AEAD{Key: encryptionKey}}

m.Lock()
managers["mysql"] = s
m.Unlock()
}

func TestManagerKey(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions oauth2/fosite_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"flag"
"fmt"
"log"
"sync"
"testing"
"time"

Expand All @@ -44,6 +45,7 @@ var clientManager = &client.MemoryManager{
Hasher: &fosite.BCrypt{},
}
var databases = make(map[string]*sqlx.DB)
var m sync.Mutex

func init() {
fositeStores["memory"] = NewFositeMemoryStore(nil, time.Hour)
Expand Down Expand Up @@ -74,8 +76,10 @@ func connectToPG() {
log.Fatalf("Could not create postgres schema: %v", err)
}

m.Lock()
databases["postgres"] = db
fositeStores["postgres"] = s
m.Unlock()
}

func connectToMySQL() {
Expand All @@ -89,8 +93,10 @@ func connectToMySQL() {
log.Fatalf("Could not create postgres schema: %v", err)
}

m.Lock()
databases["mysql"] = db
fositeStores["mysql"] = s
m.Unlock()
}

func TestCreateGetDeleteAuthorizeCodes(t *testing.T) {
Expand Down
3 changes: 0 additions & 3 deletions oauth2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/ory/hydra/client"
"github.com/ory/hydra/consent"
"github.com/ory/hydra/pkg"
"github.com/pborman/uuid"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -487,7 +486,6 @@ func (h *Handler) TokenHandler(w http.ResponseWriter, r *http.Request, _ httprou

session.Subject = accessRequest.GetClient().GetID()
session.ClientID = accessRequest.GetClient().GetID()
session.JTI = uuid.New()
session.KID = accessTokenKeyID
session.DefaultSession.Claims.Issuer = strings.TrimRight(h.IssuerURL, "/") + "/"
session.DefaultSession.Claims.IssuedAt = time.Now().UTC()
Expand Down Expand Up @@ -591,7 +589,6 @@ func (h *Handler) AuthHandler(w http.ResponseWriter, r *http.Request, _ httprout
Extra: session.Session.AccessToken,
// Here, we do not include the client because it's typically not the audience.
Audience: []string{},
JTI: uuid.New(),
KID: accessTokenKeyID,
ClientID: authorizeRequest.GetClient().GetID(),
})
Expand Down
Loading

0 comments on commit 388ee27

Please sign in to comment.