Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Fix tests #34

Merged
merged 1 commit into from
Jun 15, 2018
Merged
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
1 change: 1 addition & 0 deletions cmd/gangway/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestEnvionmentOverrides(t *testing.T) {
os.Setenv("GANGWAY_CLIENT_ID", "foo")
os.Setenv("GANGWAY_CLIENT_SECRET", "bar")
os.Setenv("GANGWAY_REDIRECT_URL", "https://foo.baz/callback")
os.Setenv("GANGWAY_SESSION_SECURITY_KEY", "testing")
cfg, err := NewConfig("")
if err != nil {
t.Errorf("Failed to test config overrides with error: %s", err)
Expand Down
13 changes: 11 additions & 2 deletions cmd/gangway/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import (
"testing"
)

func testInit() {
cfg = &Config{
SessionSecurityKey: "test",
}
initSessionStore()
}

func TestHomeHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
Expand All @@ -37,13 +44,13 @@ func TestHomeHandler(t *testing.T) {
}

func TestCallbackHandler(t *testing.T) {
testInit()

req, err := http.NewRequest("GET", "/callback", nil)
if err != nil {
t.Fatal(err)
}

initSessionStore()

rr := httptest.NewRecorder()
handler := http.HandlerFunc(callbackHandler)

Expand All @@ -55,6 +62,8 @@ func TestCallbackHandler(t *testing.T) {
}

func TestUnauthedCommandlineHandlerRedirect(t *testing.T) {
testInit()

req, err := http.NewRequest("GET", "/commandline", nil)
if err != nil {
t.Fatal(err)
Expand Down
12 changes: 8 additions & 4 deletions cmd/gangway/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import (
"crypto/sha256"
"net/http"

"golang.org/x/crypto/pbkdf2"

"github.com/gorilla/sessions"
"golang.org/x/crypto/pbkdf2"
)

const salt = "MkmfuPNHnZBBivy0L0aW"

func initSessionStore() {
func generateSessionKeys() ([]byte, []byte) {
// Take the configured security key and generate 96 bytes of data. This is
// used as the signing and encryption keys for the cookie store. For details
// on the PBKDF2 function: https://en.wikipedia.org/wiki/PBKDF2
Expand All @@ -34,7 +33,12 @@ func initSessionStore() {
[]byte(salt),
4096, 96, sha256.New)

sessionStore = sessions.NewCookieStore(b[0:64], b[64:96])
return b[0:64], b[64:96]
}

func initSessionStore() {

sessionStore = sessions.NewCookieStore(generateSessionKeys())
}

func cleanupSession(w http.ResponseWriter, r *http.Request) {
Expand Down
15 changes: 6 additions & 9 deletions cmd/gangway/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ import (
log "github.com/sirupsen/logrus"
)

func TestGenerateRandomString(t *testing.T) {
rs := generateRandomString(48)
rs2 := generateRandomString(48)
func TestGenerateSessionKeys(t *testing.T) {
cfg.SessionSecurityKey = "testing"

if rs == "" {
t.Errorf("Received an empty string")
return
}
if rs == rs2 {
t.Errorf("Generated the same string two times in a row. String is not random.")
b1, b2 := generateSessionKeys()

if len(b1) != 64 || len(b2) != 32 {
t.Errorf("Wrong byte length's returned")
return
}
}
Expand Down