Skip to content

Commit

Permalink
renames
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Archibald committed Oct 8, 2016
1 parent 0a9bdbe commit aee02a8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 79 deletions.
32 changes: 16 additions & 16 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type loginData struct {
Password string
}

func auth(sessionStore AuthStorer, w http.ResponseWriter, r *http.Request) {
session, err := sessionStore.GetSession()
func auth(authStore AuthStorer, w http.ResponseWriter, r *http.Request) {
session, err := authStore.GetSession()
if err != nil {
http.Error(w, "Authentication required: "+err.Error(), http.StatusUnauthorized)
if a, ok := err.(*AuthError); ok {
Expand All @@ -23,8 +23,8 @@ func auth(sessionStore AuthStorer, w http.ResponseWriter, r *http.Request) {
}
}

func authBasic(sessionStore AuthStorer, w http.ResponseWriter, r *http.Request) {
session, err := sessionStore.GetBasicAuth()
func authBasic(authStore AuthStorer, w http.ResponseWriter, r *http.Request) {
session, err := authStore.GetBasicAuth()
if err != nil {
w.Header().Set("WWW-Authenticate", "Basic realm='Endfirst.com'")
http.Error(w, "Authentication required: "+err.Error(), http.StatusUnauthorized)
Expand All @@ -33,28 +33,28 @@ func authBasic(sessionStore AuthStorer, w http.ResponseWriter, r *http.Request)
}
}

func login(sessionStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(sessionStore.Login, w)
func login(authStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(authStore.Login, w)
}

func register(sessionStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(sessionStore.Register, w)
func register(authStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(authStore.Register, w)
}

func createProfile(sessionStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(sessionStore.CreateProfile, w)
func createProfile(authStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(authStore.CreateProfile, w)
}

func updateEmail(sessionStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(sessionStore.UpdateEmail, w)
func updateEmail(authStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(authStore.UpdateEmail, w)
}

func updatePassword(sessionStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(sessionStore.UpdatePassword, w)
func updatePassword(authStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(authStore.UpdatePassword, w)
}

func verifyEmail(sessionStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(sessionStore.VerifyEmail, w)
func verifyEmail(authStore AuthStorer, w http.ResponseWriter, r *http.Request) {
run(authStore.VerifyEmail, w)
}

func run(method func() error, w http.ResponseWriter) {
Expand Down
56 changes: 28 additions & 28 deletions authStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type RememberMeCookie struct {
ExpireTimeUTC time.Time
}

type SessionStore struct {
type AuthStore struct {
backend BackendQuerier
mailer Mailer
cookieStore CookieStorer
Expand All @@ -54,11 +54,11 @@ type SessionStore struct {

var emailRegex = regexp.MustCompile(`^(?i)[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$`)

func NewSessionStore(backend BackendQuerier, mailer Mailer, w http.ResponseWriter, r *http.Request, cookieKey []byte, cookiePrefix string, secureOnlyCookie bool) *SessionStore {
func NewAuthStore(backend BackendQuerier, mailer Mailer, w http.ResponseWriter, r *http.Request, cookieKey []byte, cookiePrefix string, secureOnlyCookie bool) *AuthStore {
emailCookieName = cookiePrefix + "Email"
sessionCookieName = cookiePrefix + "Session"
rememberMeCookieName = cookiePrefix + "RememberMe"
return &SessionStore{backend, mailer, NewCookieStore(w, r, cookieKey, secureOnlyCookie), r}
return &AuthStore{backend, mailer, NewCookieStore(w, r, cookieKey, secureOnlyCookie), r}
}

var emailCookieName = "Email"
Expand All @@ -72,7 +72,7 @@ const sessionExpireDuration time.Duration = time.Hour
const rememberMeRenewDuration time.Duration = time.Hour
const rememberMeExpireDuration time.Duration = time.Hour * 24 * 30 // 30 days

func (s *SessionStore) GetSession() (*UserLoginSession, error) {
func (s *AuthStore) GetSession() (*UserLoginSession, error) {
cookie, err := s.getSessionCookie()
if err != nil || cookie.SessionId == "" { // impossible to get the session if there is no cookie
return nil, NewAuthError("Session cookie not found", err)
Expand All @@ -96,7 +96,7 @@ func (s *SessionStore) GetSession() (*UserLoginSession, error) {
return session, nil
}

func (s *SessionStore) GetBasicAuth() (*UserLoginSession, error) {
func (s *AuthStore) GetBasicAuth() (*UserLoginSession, error) {
session, err := s.GetSession()
if err != nil {
if email, password, ok := s.r.BasicAuth(); ok {
Expand All @@ -111,7 +111,7 @@ func (s *SessionStore) GetBasicAuth() (*UserLoginSession, error) {
return session, nil
}

func (s *SessionStore) getRememberMe() (*UserLoginRememberMe, error) {
func (s *AuthStore) getRememberMe() (*UserLoginRememberMe, error) {
cookie, err := s.getRememberMeCookie()
if err != nil || cookie.Selector == "" { // impossible to get the remember Me if there is no cookie
return nil, NewAuthError("RememberMe cookie not found", err)
Expand Down Expand Up @@ -144,7 +144,7 @@ func (s *SessionStore) getRememberMe() (*UserLoginRememberMe, error) {
return rememberMe, nil
}

func (s *SessionStore) renewSession(sessionID, sessionHash string, renewTimeUTC, expireTimeUTC *time.Time) (*UserLoginSession, error) {
func (s *AuthStore) renewSession(sessionID, sessionHash string, renewTimeUTC, expireTimeUTC *time.Time) (*UserLoginSession, error) {
if renewTimeUTC.Before(time.Now().UTC()) && expireTimeUTC.After(time.Now().UTC()) {
session, err := s.backend.RenewSession(sessionHash, time.Now().UTC().Add(sessionRenewDuration))
if err != nil {
Expand Down Expand Up @@ -176,7 +176,7 @@ func (s *SessionStore) renewSession(sessionID, sessionHash string, renewTimeUTC,
return session, nil
}

func (s *SessionStore) Login() error {
func (s *AuthStore) Login() error {
credentials, err := getCredentials(s.r)
if err != nil {
return NewAuthError("Unable to get credentials", err)
Expand All @@ -185,7 +185,7 @@ func (s *SessionStore) Login() error {
return err
}

func (s *SessionStore) login(email, password string, rememberMe bool) (*UserLoginSession, error) {
func (s *AuthStore) login(email, password string, rememberMe bool) (*UserLoginSession, error) {
if !isValidEmail(email) {
return nil, NewAuthError("Please enter a valid email address.", nil)
}
Expand All @@ -206,7 +206,7 @@ func (s *SessionStore) login(email, password string, rememberMe bool) (*UserLogi
return s.createSession(login.LoginId, login.UserId, rememberMe)
}

func (s *SessionStore) createSession(loginId, userId int, rememberMe bool) (*UserLoginSession, error) {
func (s *AuthStore) createSession(loginId, userId int, rememberMe bool) (*UserLoginSession, error) {
var err error
var selector, token, tokenHash string
if rememberMe {
Expand Down Expand Up @@ -258,15 +258,15 @@ type SendVerifyParams struct {
RefererBaseUrl string
}

func (s *SessionStore) Register() error {
func (s *AuthStore) Register() error {
registration, err := getRegistration(s.r)
if err != nil {
return NewAuthError("Unable to get email", err)
}
return s.register(registration.Email)
}

func (s *SessionStore) register(email string) error {
func (s *AuthStore) register(email string) error {
if !isValidEmail(email) {
return NewAuthError("Invalid email", nil)
}
Expand Down Expand Up @@ -296,7 +296,7 @@ func getBaseUrl(url string) string {
return url[:protoIndex+3+firstSlash]
}

func (s *SessionStore) addUser(email string) (string, error) {
func (s *AuthStore) addUser(email string) (string, error) {
emailConfirmCode, emailConfimHash, err := generateStringAndHash()
if err != nil {
return "", NewLoggedError("Problem generating email confirmation code", err)
Expand All @@ -309,15 +309,15 @@ func (s *SessionStore) addUser(email string) (string, error) {
return emailConfirmCode, nil
}

func (s *SessionStore) CreateProfile() error {
func (s *AuthStore) CreateProfile() error {
profile, err := getProfile(s.r)
if err != nil {
return NewAuthError("Unable to get profile information from form", err)
}
return s.createProfile(profile.FullName, profile.Organization, profile.Password, profile.PicturePath)
}

func (s *SessionStore) createProfile(fullName, organization, password, picturePath string) error {
func (s *AuthStore) createProfile(fullName, organization, password, picturePath string) error {
emailCookie, err := s.getEmailCookie()
if err != nil || emailCookie.EmailVerificationCode == "" {
return NewLoggedError("Unable to get email verification cookie", err)
Expand Down Expand Up @@ -348,15 +348,15 @@ func (s *SessionStore) createProfile(fullName, organization, password, picturePa
return nil
}

func (s *SessionStore) VerifyEmail() error {
func (s *AuthStore) VerifyEmail() error {
verify, err := getVerificationCode(s.r)
if err != nil {
return NewAuthError("Unable to get verification email from JSON", err)
}
return s.verifyEmail(verify.EmailVerificationCode)
}

func (s *SessionStore) verifyEmail(emailVerificationCode string) error {
func (s *AuthStore) verifyEmail(emailVerificationCode string) error {
if !strings.HasSuffix(emailVerificationCode, "=") { // add back the "=" then decode
emailVerificationCode = emailVerificationCode + "="
}
Expand All @@ -382,45 +382,45 @@ func (s *SessionStore) verifyEmail(emailVerificationCode string) error {
return nil
}

func (s *SessionStore) UpdateEmail() error { return nil }
func (s *AuthStore) UpdateEmail() error { return nil }

func (s *SessionStore) UpdatePassword() error {
func (s *AuthStore) UpdatePassword() error {
return nil
}

func (s *SessionStore) getEmailCookie() (*EmailCookie, error) {
func (s *AuthStore) getEmailCookie() (*EmailCookie, error) {
email := &EmailCookie{}
return email, s.cookieStore.Get(emailCookieName, email)
}

func (s *SessionStore) getSessionCookie() (*SessionCookie, error) {
func (s *AuthStore) getSessionCookie() (*SessionCookie, error) {
session := &SessionCookie{}
return session, s.cookieStore.Get(sessionCookieName, session)
}

func (s *SessionStore) getRememberMeCookie() (*RememberMeCookie, error) {
func (s *AuthStore) getRememberMeCookie() (*RememberMeCookie, error) {
rememberMe := &RememberMeCookie{}
return rememberMe, s.cookieStore.Get(rememberMeCookieName, rememberMe)
}

func (s *SessionStore) deleteEmailCookie() {
func (s *AuthStore) deleteEmailCookie() {
s.cookieStore.Delete(emailCookieName)
}

func (s *SessionStore) deleteSessionCookie() {
func (s *AuthStore) deleteSessionCookie() {
s.cookieStore.Delete(sessionCookieName)
}

func (s *SessionStore) deleteRememberMeCookie() {
func (s *AuthStore) deleteRememberMeCookie() {
s.cookieStore.Delete(rememberMeCookieName)
}

func (s *SessionStore) saveEmailCookie(emailVerificationCode string, expireTimeUTC time.Time) error {
func (s *AuthStore) saveEmailCookie(emailVerificationCode string, expireTimeUTC time.Time) error {
cookie := EmailCookie{EmailVerificationCode: emailVerificationCode, ExpireTimeUTC: expireTimeUTC}
return s.cookieStore.PutWithExpire(emailCookieName, emailExpireMins, &cookie)
}

func (s *SessionStore) saveSessionCookie(sessionId string, renewTimeUTC, expireTimeUTC time.Time) error {
func (s *AuthStore) saveSessionCookie(sessionId string, renewTimeUTC, expireTimeUTC time.Time) error {
cookie := SessionCookie{SessionId: sessionId, RenewTimeUTC: renewTimeUTC, ExpireTimeUTC: expireTimeUTC}
err := s.cookieStore.Put(sessionCookieName, &cookie)
if err != nil {
Expand All @@ -429,7 +429,7 @@ func (s *SessionStore) saveSessionCookie(sessionId string, renewTimeUTC, expireT
return nil
}

func (s *SessionStore) saveRememberMeCookie(selector, token string, renewTimeUTC, expireTimeUTC time.Time) error {
func (s *AuthStore) saveRememberMeCookie(selector, token string, renewTimeUTC, expireTimeUTC time.Time) error {
cookie := RememberMeCookie{Selector: selector, Token: token, RenewTimeUTC: renewTimeUTC, ExpireTimeUTC: expireTimeUTC}
return s.cookieStore.Put(rememberMeCookieName, &cookie)
}
Expand Down
24 changes: 12 additions & 12 deletions authStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ import (
var futureTime time.Time = time.Now().Add(5 * time.Minute)
var pastTime time.Time = time.Now().Add(-5 * time.Minute)

func getStore(emailCookieToReturn *EmailCookie, sessionCookieToReturn *SessionCookie, rememberMeCookieToReturn *RememberMeCookie, hasCookieGetError, hasCookiePutError bool, backend *MockBackend) *SessionStore {
func getStore(emailCookieToReturn *EmailCookie, sessionCookieToReturn *SessionCookie, rememberMeCookieToReturn *RememberMeCookie, hasCookieGetError, hasCookiePutError bool, backend *MockBackend) *AuthStore {
cookieStore := NewMockCookieStore(map[string]interface{}{emailCookieName: emailCookieToReturn, sessionCookieName: sessionCookieToReturn, rememberMeCookieName: rememberMeCookieToReturn}, hasCookieGetError, hasCookiePutError)
return &SessionStore{backend, &TextMailer{}, cookieStore, &http.Request{}}
return &AuthStore{backend, &TextMailer{}, cookieStore, &http.Request{}}
}

func TestNewSessionStore(t *testing.T) {
func TestNewAuthStore(t *testing.T) {
w := httptest.NewRecorder()
r := &http.Request{}
b := &MockBackend{}
m := &TextMailer{}
actual := NewSessionStore(b, m, w, r, cookieKey, "prefix", false)
actual := NewAuthStore(b, m, w, r, cookieKey, "prefix", false)
if actual.backend != b || actual.cookieStore.(*CookieStore).w != w || actual.cookieStore.(*CookieStore).r != r {
t.Fatal("expected correct init")
}
}

func TestSessionStoreEndToEnd(t *testing.T) {
func TestAuthStoreEndToEnd(t *testing.T) {
w := httptest.NewRecorder()
r := &http.Request{Header: http.Header{}}
b := NewBackendMemory()
m := &TextMailer{}
s := NewSessionStore(b, m, w, r, cookieKey, "prefix", false)
s := NewAuthStore(b, m, w, r, cookieKey, "prefix", false)

// register new user
// adds to users, logins and sessions
Expand Down Expand Up @@ -735,19 +735,19 @@ func collectionEqual(expected, actual []string) bool {
}

/****************************************************************************/
type MockSessionStore struct {
type MockAuthStore struct {
}

func NewMockSessionStore() *MockSessionStore {
return &MockSessionStore{}
func NewMockAuthStore() *MockAuthStore {
return &MockAuthStore{}
}

func (s *MockSessionStore) Get() (*UserLoginSession, error) {
func (s *MockAuthStore) Get() (*UserLoginSession, error) {
return nil, nil
}
func (s *MockSessionStore) GetRememberMe() (*UserLoginRememberMe, error) {
func (s *MockAuthStore) GetRememberMe() (*UserLoginRememberMe, error) {
return nil, nil
}
func (s *MockSessionStore) Login(email, password, returnUrl string) (*UserLoginSession, error) {
func (s *MockAuthStore) Login(email, password, returnUrl string) (*UserLoginSession, error) {
return nil, nil
}
Loading

0 comments on commit aee02a8

Please sign in to comment.