Skip to content

Commit

Permalink
minor cleanup for new release
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-hanna committed May 4, 2017
1 parent d32304c commit 1893362
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 89 deletions.
83 changes: 38 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,45 @@ type ServiceInterface interface {

## Quickstart
~~~go
package main

import (
...
)

var sesh *sessions.Service

var issueSession = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userSession, err := sesh.IssueUserSession("fakeUserID", "", w)
if err != nil {
log.Printf("Err issuing user session: %v\n", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
log.Printf("In issue; user's session: %v\n", userSession)

w.WriteHeader(http.StatusOK)
})
// issue a new session and write the session to the ResponseWriter
userSession, err := sesh.IssueUserSession("fakeUserID", "", w)
if err != nil {
log.Printf("Err issuing user session: %v\n", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

func main() {
seshStore := store.New(store.Options{})
...

// e.g. `$ openssl rand -base64 64`
seshAuth, err := auth.New(auth.Options{
Key: []byte("DOZDgBdMhGLImnk0BGYgOUI+h1n7U+OdxcZPctMbeFCsuAom2aFU4JPV4Qj11hbcb5yaM4WDuNP/3B7b+BnFhw=="),
})
if err != nil {
log.Fatal(err)
}
// Fetch a pointer to a valid user session from a request. A nil pointer indicates no or invalid session
userSession, err := sesh.GetUserSession(r)
if err != nil {
log.Printf("Err fetching user session: %v\n", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// nil session pointers indicate a 401 unauthorized
if session == nil {
http.Error(w, "Unathorized", http.StatusUnauthorized)
return
}

seshTransport := transport.New(transport.Options{
HTTPOnly: true,
Secure: false, // note: can't use secure cookies in development!
})
...

sesh = sessions.New(seshStore, seshAuth, seshTransport, sessions.Options{})
// Extend session expiry. Note that session expiry's need to be manually extended
if err := sesh.ExtendUserSession(userSession, r, w); err != nil {
log.Printf("Err extending user session: %v\n", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

http.HandleFunc("/issue", issueSession)
...

log.Println("Listening on localhost:8080")
log.Fatal(http.ListenAndServe("127.0.0.1:8080", nil))
// Invalidate a user session, deleting it from redis and expiring the cookie on the ResponseWriter
if err := sesh.ClearUserSession(userSession, w); err != nil {
log.Printf("Err clearing user session: %v\n", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
~~~

Expand Down Expand Up @@ -281,8 +278,7 @@ var requiresSession = http.HandlerFunc(func(w http.ResponseWriter, r *http.Reque
}

// note that session expiry's need to be manually extended
err = sesh.ExtendUserSession(userSession, r, w)
if err != nil {
if err = sesh.ExtendUserSession(userSession, r, w); err != nil {
log.Printf("Err extending user session: %v\n", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
Expand Down Expand Up @@ -334,8 +330,7 @@ var clearSession = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request)
return
}

err = sesh.ClearUserSession(userSession, w)
if err != nil {
if err = sesh.ClearUserSession(userSession, w); err != nil {
log.Printf("Err clearing user session: %v\n", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
Expand All @@ -360,11 +355,9 @@ func main() {
seshStore := store.New(store.Options{})

// e.g. `$ openssl rand -base64 64`
authKey := "DOZDgBdMhGLImnk0BGYgOUI+h1n7U+OdxcZPctMbeFCsuAom2aFU4JPV4Qj11hbcb5yaM4WDuNP/3B7b+BnFhw=="
authOptions := auth.Options{
Key: []byte(authKey),
}
seshAuth, err := auth.New(authOptions)
seshAuth, err := auth.New(auth.Options{
Key: []byte("DOZDgBdMhGLImnk0BGYgOUI+h1n7U+OdxcZPctMbeFCsuAom2aFU4JPV4Qj11hbcb5yaM4WDuNP/3B7b+BnFhw=="),
})
if err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ func setup() error {
seshStore = store.New(store.Options{})

// e.g. `$ openssl rand -base64 64`
authKey := "DOZDgBdMhGLImnk0BGYgOUI+h1n7U+OdxcZPctMbeFCsuAom2aFU4JPV4Qj11hbcb5yaM4WDuNP/3B7b+BnFhw=="
seshAuth, err := auth.New(auth.Options{
Key: []byte(authKey),
Key: []byte("DOZDgBdMhGLImnk0BGYgOUI+h1n7U+OdxcZPctMbeFCsuAom2aFU4JPV4Qj11hbcb5yaM4WDuNP/3B7b+BnFhw=="),
})
if err != nil {
return err
Expand Down
9 changes: 3 additions & 6 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func (s *Service) IssueUserSession(userID string, json string, w http.ResponseWr
}

// save the session in the store
err = s.store.SaveUserSession(userSession)
if err != nil {
if err = s.store.SaveUserSession(userSession); err != nil {
return nil, err
}

Expand All @@ -67,8 +66,7 @@ func (s *Service) IssueUserSession(userID string, json string, w http.ResponseWr
// This method should be called when a user logs out, for example.
func (s *Service) ClearUserSession(userSession *user.Session, w http.ResponseWriter) error {
// delete the session from the store
err := s.store.DeleteUserSession(userSession.ID)
if err != nil {
if err := s.store.DeleteUserSession(userSession.ID); err != nil {
return err
}

Expand Down Expand Up @@ -114,8 +112,7 @@ func (s *Service) ExtendUserSession(userSession *user.Session, r *http.Request,
userSession.ExpiresAt = newExpiresAt

// save the session in the store with the extended expiry
err := s.store.SaveUserSession(userSession)
if err != nil {
if err := s.store.SaveUserSession(userSession); err != nil {
return err
}

Expand Down
18 changes: 6 additions & 12 deletions service_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ var (
return
}

err = sesh.ClearUserSession(userSession, w)
if err != nil {
if err = sesh.ClearUserSession(userSession, w); err != nil {
if testing.Verbose() {
log.Printf("Err clearing user session: %v\n", err)
}
Expand Down Expand Up @@ -239,15 +238,13 @@ func generateKey() (string, error) {
}

func TestMain(m *testing.M) {
err := setup()
if err != nil {
if err := setup(); err != nil {
log.Fatal("Err setting up e2e tests", err)
}

code := m.Run()

err = shutdown()
if err != nil {
if err := shutdown(); err != nil {
log.Fatal("Err shutting down e2e tests", err)
}

Expand All @@ -261,9 +258,8 @@ func setup() error {
seshStore = store.New(store.Options{})

// e.g. `$ openssl rand -base64 64`
authKey := "DOZDgBdMhGLImnk0BGYgOUI+h1n7U+OdxcZPctMbeFCsuAom2aFU4JPV4Qj11hbcb5yaM4WDuNP/3B7b+BnFhw=="
seshAuth, err := auth.New(auth.Options{
Key: []byte(authKey),
Key: []byte("DOZDgBdMhGLImnk0BGYgOUI+h1n7U+OdxcZPctMbeFCsuAom2aFU4JPV4Qj11hbcb5yaM4WDuNP/3B7b+BnFhw=="),
})
if err != nil {
return err
Expand All @@ -282,8 +278,7 @@ func setup() error {
c := seshStore.Pool.Get()
defer c.Close()

_, err = c.Do("PING")
if err != nil {
if _, err = c.Do("PING"); err != nil {
return err
}

Expand All @@ -299,8 +294,7 @@ func shutdown() error {
aLongTimeAgo := time.Now().Add(-1000 * time.Hour)

for idx := range issuedSessionIDs {
_, err := c.Do("EXPIREAT", issuedSessionIDs[idx], aLongTimeAgo.Unix())
if err != nil {
if _, err := c.Do("EXPIREAT", issuedSessionIDs[idx], aLongTimeAgo.Unix()); err != nil {
return errors.New("Could not delete issued session id. Error: " + err.Error())
}
}
Expand Down
36 changes: 12 additions & 24 deletions store/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ var (
)

func TestMain(m *testing.M) {
err := setup()
if err != nil {
if err := setup(); err != nil {
log.Fatal("Err setting up integration tests")
}

code := m.Run()

err = shutdown()
if err != nil {
if err := shutdown(); err != nil {
log.Fatal("Err shutting down integration tests")
}

Expand All @@ -70,33 +68,27 @@ func setup() error {
defer c.Close()

// VALID USER
_, err := c.Do("HMSET", validUserSession.ID, "UserID", validUserSession.UserID, "JSON", validUserSession.JSON, "ExpiresAtSeconds", validUserSession.ExpiresAt.Unix())
if err != nil {
if _, err := c.Do("HMSET", validUserSession.ID, "UserID", validUserSession.UserID, "JSON", validUserSession.JSON, "ExpiresAtSeconds", validUserSession.ExpiresAt.Unix()); err != nil {
return errors.New("Could not set valid user")
}
_, err = c.Do("EXPIREAT", validUserSession.ID, validUserSession.ExpiresAt.Unix())
if err != nil {
if _, err := c.Do("EXPIREAT", validUserSession.ID, validUserSession.ExpiresAt.Unix()); err != nil {
return errors.New("Could not set expiry for valid user")
}

// INVALID USER
// note: the invalid user doesn't have JSON!
_, err = c.Do("HMSET", inValidUserSession.ID, "UserID", inValidUserSession.UserID, "ExpiresAtSeconds", inValidUserSession.ExpiresAt.Unix())
if err != nil {
if _, err := c.Do("HMSET", inValidUserSession.ID, "UserID", inValidUserSession.UserID, "ExpiresAtSeconds", inValidUserSession.ExpiresAt.Unix()); err != nil {
return errors.New("Could not set valid user")
}
_, err = c.Do("EXPIREAT", inValidUserSession.ID, inValidUserSession.ExpiresAt.Unix())
if err != nil {
if _, err := c.Do("EXPIREAT", inValidUserSession.ID, inValidUserSession.ExpiresAt.Unix()); err != nil {
return errors.New("Could not set expiry for valid user")
}

// EXPIRED USER
_, err = c.Do("HMSET", expiredUserSession.ID, "UserID", expiredUserSession.UserID, "JSON", expiredUserSession.JSON, "ExpiresAtSeconds", expiredUserSession.ExpiresAt.Unix())
if err != nil {
if _, err := c.Do("HMSET", expiredUserSession.ID, "UserID", expiredUserSession.UserID, "JSON", expiredUserSession.JSON, "ExpiresAtSeconds", expiredUserSession.ExpiresAt.Unix()); err != nil {
return errors.New("Could not set valid user")
}
_, err = c.Do("EXPIREAT", expiredUserSession.ID, expiredUserSession.ExpiresAt.Unix())
if err != nil {
if _, err := c.Do("EXPIREAT", expiredUserSession.ID, expiredUserSession.ExpiresAt.Unix()); err != nil {
return errors.New("Could not set expiry for valid user")
}

Expand All @@ -112,26 +104,22 @@ func shutdown() error {
aLongTimeAgo := time.Now().Add(-1000 * time.Hour)

// VALID USER
_, err := c.Do("EXPIREAT", validUserSession.ID, aLongTimeAgo.Unix())
if err != nil {
if _, err := c.Do("EXPIREAT", validUserSession.ID, aLongTimeAgo.Unix()); err != nil {
return errors.New("Could not set EXPIREAT for validUserSession")
}

// VALID USER
_, err = c.Do("EXPIREAT", validUserSessionForSaving.ID, aLongTimeAgo.Unix())
if err != nil {
if _, err := c.Do("EXPIREAT", validUserSessionForSaving.ID, aLongTimeAgo.Unix()); err != nil {
return errors.New("Could not set EXPIREAT for validUserSessionForSaving")
}

// INVALID USER
_, err = c.Do("EXPIREAT", inValidUserSession.ID, aLongTimeAgo.Unix())
if err != nil {
if _, err := c.Do("EXPIREAT", inValidUserSession.ID, aLongTimeAgo.Unix()); err != nil {
return errors.New("Could not set EXPIREAT for invaludUserSession")
}

// EXPIRED USER
_, err = c.Do("EXPIREAT", expiredUserSession.ID, aLongTimeAgo.Unix())
if err != nil {
if _, err := c.Do("EXPIREAT", expiredUserSession.ID, aLongTimeAgo.Unix()); err != nil {
return errors.New("Could not set EXPIREAT for expiredUserSession")
}

Expand Down

0 comments on commit 1893362

Please sign in to comment.