auth.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package routes
  2. import (
  3. "encoding/gob"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "github.com/andreanidouglas/auth/model"
  8. "github.com/gorilla/securecookie"
  9. "golang.org/x/crypto/bcrypt"
  10. "github.com/gorilla/sessions"
  11. )
  12. type user struct {
  13. Email string `json:"email"`
  14. Password string `jsob:"password"`
  15. }
  16. type authenticated struct {
  17. Email string
  18. Authenticated bool
  19. }
  20. const (
  21. AUTH_KEY string = "session_id"
  22. USER_ID string = "user_id"
  23. )
  24. var store *sessions.CookieStore
  25. func Init() {
  26. auth_key := securecookie.GenerateRandomKey(64)
  27. encryption_key := securecookie.GenerateRandomKey(32)
  28. store = sessions.NewCookieStore(
  29. auth_key,
  30. encryption_key,
  31. )
  32. store.Options = &sessions.Options{
  33. Path: "/auth",
  34. MaxAge: 60 * 15,
  35. HttpOnly: true,
  36. }
  37. gob.Register(authenticated{})
  38. }
  39. func getUser(s *sessions.Session) authenticated {
  40. val := s.Values["user"]
  41. var user = authenticated{}
  42. user, ok := val.(authenticated)
  43. if !ok {
  44. return authenticated{Authenticated: false}
  45. }
  46. return user
  47. }
  48. func AuthMiddleware(next http.HandlerFunc) func(http.ResponseWriter, *http.Request) {
  49. return func(w http.ResponseWriter, r *http.Request) {
  50. fmt.Printf("got store: %v", store)
  51. session, err := store.Get(r, "cookie-name")
  52. if err != nil {
  53. w.WriteHeader(500)
  54. fmt.Fprintf(w, "Internal Server Error: %v", err)
  55. return
  56. }
  57. authed := getUser(session)
  58. if auth := authed.Authenticated; !auth {
  59. w.WriteHeader(403)
  60. fmt.Fprintf(w, "Unauthorized")
  61. return
  62. }
  63. next.ServeHTTP(w, r)
  64. }
  65. }
  66. func Login(w http.ResponseWriter, r *http.Request) {
  67. accepts := r.Header.Get("Accepts")
  68. if accepts != "application/json" {
  69. w.WriteHeader(400)
  70. fmt.Fprintf(w, "Could not process request. Incorrect body type")
  71. return
  72. }
  73. sessions, err := store.Get(r, "cookie-name")
  74. if err != nil {
  75. w.WriteHeader(500)
  76. fmt.Fprintf(w, "Internal server error: %v", err)
  77. }
  78. db := model.NewDb("./auth.db")
  79. db.Setup()
  80. var data user
  81. err = json.NewDecoder(r.Body).Decode(&data)
  82. if err != nil {
  83. w.WriteHeader(400)
  84. fmt.Fprintf(w, "Could not process request. Incorrect body format")
  85. return
  86. }
  87. var user model.User
  88. if !model.CheckEmail(data.Email, &user, &db) {
  89. w.WriteHeader(403)
  90. fmt.Fprintf(w, "Unauthorized")
  91. return
  92. }
  93. err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(data.Password))
  94. if err != nil {
  95. w.WriteHeader(403)
  96. fmt.Fprintf(w, "Unauthorized")
  97. return
  98. }
  99. authed := authenticated {
  100. Email: user.Email,
  101. Authenticated: true,
  102. }
  103. sessions.Values["user"] = authed
  104. err = sessions.Save(r, w)
  105. if err != nil {
  106. w.WriteHeader(500)
  107. fmt.Fprintf(w, "Internal Server Error: %v", err)
  108. }
  109. w.WriteHeader(200)
  110. fmt.Fprintf(w, "Authorized")
  111. }