Skip to content

Commit

Permalink
http: server serve static files
Browse files Browse the repository at this point in the history
  • Loading branch information
ldb committed Jun 2, 2018
1 parent bafe06b commit 5bb088d
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 108 deletions.
2 changes: 1 addition & 1 deletion auth/sessionId.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package auth

import "github.com/satori/go.uuid"

func (Authenticator) SessionId() string {
func (Authenticator) SessionID() string {
v4 := uuid.NewV4()
return v4.String()
}
6 changes: 3 additions & 3 deletions auth/sessionId_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"testing"
)

func TestAuthenticator_CreateAccessToken(t *testing.T) {
func TestAuthenticator_SessionID(t *testing.T) {
t.Parallel()
authenticator := Authenticator{}
sessionId := authenticator.SessionId()
sessionID := authenticator.SessionID()

id, err := uuid.FromString(sessionId)
id, err := uuid.FromString(sessionID)
assert.Nil(t, err, "should not cause error")
assert.Equal(t, byte(4), id.Version(), "should create UUID v4")
}
2 changes: 1 addition & 1 deletion course.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package eduboard
import "gopkg.in/mgo.v2/bson"

type Course struct {
Id bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`
ID bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
Members []string `json:"members,omitempty" bson:"members,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ func NewAuthMiddleware(provider eduboard.UserAuthenticationProvider, nextHandler
return
}

accessToken := cookie.Value
err, ok := provider.CheckAuthentication(accessToken)
sessionID := cookie.Value
err, ok := provider.CheckAuthentication(sessionID)
if err != nil || !ok {
w.WriteHeader(http.StatusForbidden)
return
}

nextHandler.ServeHTTP(w, r)
}
}
}
10 changes: 5 additions & 5 deletions http/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

func TestAppServer_NewAuthMiddleware(t *testing.T) {
var as = &mock.UserAuthenticationProvider{
CheckAuthenticationFn: func(sessionId string) (err error, ok bool) {
if sessionId == "" {
return errors.New("empty sessionId"), false
CheckAuthenticationFn: func(sessionID string) (err error, ok bool) {
if sessionID == "" {
return errors.New("empty sessionID"), false
}
if sessionId == "invalid" {
if sessionID == "invalid" {
return errors.New("not found"), false
}
return nil, true
Expand Down Expand Up @@ -61,4 +61,4 @@ func TestAppServer_NewAuthMiddleware(t *testing.T) {
assert.True(t, as.CheckAuthenticationFnInvoked, "authentication was not actually checked")
})
}
}
}
10 changes: 0 additions & 10 deletions http/routes.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package http

import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
)

func (a *AppServer) authenticatedRoutes() *httprouter.Router {
router := httprouter.New()
//router.GET("/index.html", a.serveFilesHandler())
//router.GET("/static/*", a.serveFilesHandler())

// User
router.GET("/api/v1/user/:id", a.getUserHandler())
Expand All @@ -29,9 +25,3 @@ func (a *AppServer) publicRoutes() *httprouter.Router {
router.POST("/api/logout", a.logoutUserHandler())
return router
}

func (a *AppServer) serveFilesHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprint(w, "hello world")
}
}
1 change: 1 addition & 0 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (a *AppServer) initialize() {
mux := http.NewServeMux()
mux.Handle("/api/v1/", NewAuthMiddleware(a.UserService, protected))
mux.Handle("/api/", public)
mux.Handle("/", http.FileServer(http.Dir("./static")))

a.httpServer = &http.Server{
Addr: ":8080",
Expand Down
12 changes: 6 additions & 6 deletions http/userHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (a *AppServer) registerUserHandler() httprouter.Handle {
Password string `json:"password"`
}
type response struct {
Id string `json:"id"`
ID string `json:"id"`
Email string `json:"email"`
}

Expand All @@ -37,9 +37,9 @@ func (a *AppServer) registerUserHandler() httprouter.Handle {
w.WriteHeader(http.StatusInternalServerError)
return
}
response := response{Id: user.Id.Hex(), Email: user.Email}
response := response{ID: user.ID.Hex(), Email: user.Email}

cookie := createCookie(user.SessionId)
cookie := createCookie(user.SessionID)
http.SetCookie(w, &cookie)
if err = json.NewEncoder(w).Encode(response); err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -72,7 +72,7 @@ func (a *AppServer) loginUserHandler() httprouter.Handle {
return
}

cookie := createCookie(user.SessionId)
cookie := createCookie(user.SessionID)
response := response{user.Name, user.Surname, user.Email}
http.SetCookie(w, &cookie)
if err = json.NewEncoder(w).Encode(response); err != nil {
Expand All @@ -83,12 +83,12 @@ func (a *AppServer) loginUserHandler() httprouter.Handle {

func (a *AppServer) logoutUserHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
sessionId, err := r.Cookie("sessionId")
sessionID, err := r.Cookie("sessionID")
if err != nil {
w.WriteHeader(http.StatusOK)
return
}
err = a.UserService.Logout(sessionId.Value)
err = a.UserService.Logout(sessionID.Value)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand Down
20 changes: 10 additions & 10 deletions mock/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ type UserRepository struct {
FindByEmailFn func(email string) (error, *eduboard.User)
FindByEmailFnInvoked bool

FindBySessionIdFn func(sessionId string) (error, *eduboard.User)
FindBySessionIdFnInvoked bool
FindBySessionIDFn func(sessionID string) (error, *eduboard.User)
FindBySessionIDFnInvoked bool

UpdateSessionIdFn func(user *eduboard.User) (error, *eduboard.User)
UpdateSessionIdFnInvoked bool
UpdateSessionIDFn func(user *eduboard.User) (error, *eduboard.User)
UpdateSessionIDFnInvoked bool
}

var _ eduboard.UserRepository = (*UserRepository)(nil)
Expand All @@ -71,12 +71,12 @@ func (uRM *UserRepository)FindByEmail(email string) (error, *eduboard.User) {
return uRM.FindByEmailFn(email)
}

func (uRM *UserRepository)FindBySessionId(sessionId string) (error, *eduboard.User) {
uRM.FindBySessionIdFnInvoked = true
return uRM.FindBySessionIdFn(sessionId)
func (uRM *UserRepository)FindBySessionID(sessionID string) (error, *eduboard.User) {
uRM.FindBySessionIDFnInvoked = true
return uRM.FindBySessionIDFn(sessionID)
}

func (uRM *UserRepository)UpdateSessionId(user *eduboard.User) (error, *eduboard.User) {
uRM.UpdateSessionIdFnInvoked = true
return uRM.UpdateSessionIdFn(user)
func (uRM *UserRepository)UpdateSessionID(user *eduboard.User) (error, *eduboard.User) {
uRM.UpdateSessionIDFnInvoked = true
return uRM.UpdateSessionIDFn(user)
}
24 changes: 12 additions & 12 deletions mock/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ type UserAuthenticationProvider struct {
LoginFn func(email string, password string) (error, *eduboard.User)
LoginFnInvoked bool

LogoutFn func(accessToken string) error
LogoutFn func(sessionID string) error
LogoutFnInvoked bool

CheckAuthenticationFn func(sessionId string) (err error, ok bool)
CheckAuthenticationFn func(sessionID string) (err error, ok bool)
CheckAuthenticationFnInvoked bool
}

Expand All @@ -69,19 +69,19 @@ func (uAM *UserAuthenticationProvider) Login(email string, password string) (err
return uAM.LoginFn(email, password)
}

func (uAM *UserAuthenticationProvider) Logout(sessionId string) error {
func (uAM *UserAuthenticationProvider) Logout(sessionID string) error {
uAM.LogoutFnInvoked = true
return uAM.LogoutFn(sessionId)
return uAM.LogoutFn(sessionID)
}
func (uAM *UserAuthenticationProvider) CheckAuthentication(sessionId string) (err error, ok bool) {
func (uAM *UserAuthenticationProvider) CheckAuthentication(sessionID string) (err error, ok bool) {
uAM.CheckAuthenticationFnInvoked = true
return uAM.CheckAuthenticationFn(sessionId)
return uAM.CheckAuthenticationFn(sessionID)
}

type Authenticator interface {
Hash(password string) (string, error)
CompareHash(hashedPassword string, plainPassword string) (bool, error)
SessionId() string
SessionID() string
}

type AuthenticatorMock struct {
Expand All @@ -91,8 +91,8 @@ type AuthenticatorMock struct {
CompareHashFn func(hashedPassword string, plainPassword string) (bool, error)
CompareHashFnInvoked bool

SessionIdFn func() string
SessionIdFnInvoked bool
SessionIDFn func() string
SessionIDFnInvoked bool
}

var _ Authenticator = (*AuthenticatorMock)(nil)
Expand All @@ -106,7 +106,7 @@ func (uAM *AuthenticatorMock) CompareHash(hashedPassword string, plainPassword s
uAM.CompareHashFnInvoked = true
return uAM.CompareHashFn(hashedPassword, plainPassword)
}
func (uAM *AuthenticatorMock) SessionId() string {
uAM.SessionIdFnInvoked = true
return uAM.SessionIdFn()
func (uAM *AuthenticatorMock) SessionID() string {
uAM.SessionIDFnInvoked = true
return uAM.SessionIDFn()
}
12 changes: 6 additions & 6 deletions mongodb/userRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func newUserRepository(database *mgo.Database) *UserRepository {
}

func (u *UserRepository) Store(user *eduboard.User) error {
if user.Id == "" {
user.Id = bson.NewObjectId()
if user.ID == "" {
user.ID = bson.NewObjectId()
}
return u.c.Insert(user)
}
Expand All @@ -38,8 +38,8 @@ func (u *UserRepository) Find(id string) (error, *eduboard.User) {
return nil, &result
}

func (u *UserRepository) FindBySessionId(accessToken string) (error, *eduboard.User) {
return u.findBy("sessionId", accessToken)
func (u *UserRepository) FindBySessionID(accessToken string) (error, *eduboard.User) {
return u.findBy("sessionID", accessToken)
}

func (u *UserRepository) FindByEmail(email string) (error, *eduboard.User) {
Expand All @@ -56,8 +56,8 @@ func (u *UserRepository) findBy(key string, value string) (error, *eduboard.User
return nil, &result
}

func (u *UserRepository) UpdateSessionId(user *eduboard.User) (error, *eduboard.User) {
return u.updateValue(user.Id, "sessionId", user.SessionId)
func (u *UserRepository) UpdateSessionID(user *eduboard.User) (error, *eduboard.User) {
return u.updateValue(user.ID, "sessionID", user.SessionID)
}

func (u *UserRepository) updateValue(id bson.ObjectId, key string, value string) (error, *eduboard.User) {
Expand Down
6 changes: 3 additions & 3 deletions service/courseService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func TestCourseService_GetAllCourses(t *testing.T) {

service.r = &mockRepo

course1 := &eduboard.Course{Id: "1", Name: "Course 1"}
course2 := &eduboard.Course{Id: "2", Name: "Course 2"}
course1 := &eduboard.Course{ID: "1", Name: "Course 1"}
course2 := &eduboard.Course{ID: "2", Name: "Course 2"}

var testCases = []struct {
name string
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestCourseService_GetCourse(t *testing.T) {

service.r = &mockRepo

course1 := &eduboard.Course{Id: "1", Name: "Course 1"}
course1 := &eduboard.Course{ID: "1", Name: "Course 1"}

var testCases = []struct {
name string
Expand Down
20 changes: 10 additions & 10 deletions service/userService.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type UserService struct {
type Authenticator interface {
Hash(password string) (string, error)
CompareHash(hashedPassword string, plainPassword string) (bool, error)
SessionId() string
SessionID() string
}

func NewUserService(repository eduboard.UserRepository) *UserService {
Expand All @@ -39,7 +39,7 @@ func (uS *UserService) CreateUser(user *eduboard.User, password string) (error,
return err, &eduboard.User{}
}
user.PasswordHash = hashedPassword
user.SessionId = uS.a.SessionId()
user.SessionID = uS.a.SessionID()

err = uS.r.Store(user)
if err != nil {
Expand All @@ -66,29 +66,29 @@ func (uS *UserService) Login(email string, password string) (error, *eduboard.Us
return errors.New("invalid password"), &eduboard.User{}
}

user.SessionId = uS.a.SessionId()
err, user = uS.r.UpdateSessionId(user)
user.SessionID = uS.a.SessionID()
err, user = uS.r.UpdateSessionID(user)
if err != nil {
return err, &eduboard.User{}
}

return nil, user
}

func (uS *UserService) Logout(sessionId string) error {
err, user := uS.r.FindBySessionId(sessionId)
func (uS *UserService) Logout(sessionID string) error {
err, user := uS.r.FindBySessionID(sessionID)
if err != nil {
return err
}

user.SessionId = ""
uS.r.UpdateSessionId(user)
user.SessionID = ""
uS.r.UpdateSessionID(user)

return nil
}

func (uS *UserService) CheckAuthentication(sessionId string) (err error, ok bool) {
err, _ = uS.r.FindBySessionId(sessionId)
func (uS *UserService) CheckAuthentication(sessionID string) (err error, ok bool) {
err, _ = uS.r.FindBySessionID(sessionID)
if err != nil {
return err, false
}
Expand Down
Loading

0 comments on commit 5bb088d

Please sign in to comment.