auth.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package middleware
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strconv"
  9. "github.com/go-chi/chi"
  10. "github.com/gorilla/sessions"
  11. )
  12. // Auth implements the authorization functions
  13. type Auth struct {
  14. store sessions.Store
  15. cookieName string
  16. }
  17. // NewAuth returns a new Auth instance
  18. func NewAuth(
  19. store sessions.Store,
  20. cookieName string,
  21. ) *Auth {
  22. return &Auth{store, cookieName}
  23. }
  24. // BasicAuthenticate just checks that a user is logged in
  25. func (auth *Auth) BasicAuthenticate(next http.Handler) http.Handler {
  26. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  27. if auth.isLoggedIn(w, r) {
  28. next.ServeHTTP(w, r)
  29. } else {
  30. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  31. return
  32. }
  33. return
  34. })
  35. }
  36. // IDLocation represents the location of the ID to use for authentication
  37. type IDLocation uint
  38. const (
  39. // URLParam location looks for {id} in the URL
  40. URLParam IDLocation = iota
  41. // BodyParam location looks for user_id in the body
  42. BodyParam
  43. )
  44. type bodyID struct {
  45. UserID uint64 `json:"user_id"`
  46. }
  47. // DoesUserIDMatch checks the id URL parameter and verifies that it matches
  48. // the one stored in the session
  49. func (auth *Auth) DoesUserIDMatch(next http.Handler, loc IDLocation) http.Handler {
  50. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  51. var id uint64
  52. var err error
  53. if loc == URLParam {
  54. id, err = strconv.ParseUint(chi.URLParam(r, "id"), 0, 64)
  55. } else if loc == BodyParam {
  56. form := &bodyID{}
  57. body, _ := ioutil.ReadAll(r.Body)
  58. err = json.Unmarshal(body, form)
  59. id = form.UserID
  60. // need to create a new stream for the body
  61. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  62. }
  63. if err == nil && auth.doesSessionMatchID(r, uint(id)) {
  64. next.ServeHTTP(w, r)
  65. } else {
  66. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  67. return
  68. }
  69. return
  70. })
  71. }
  72. // Helpers
  73. func (auth *Auth) doesSessionMatchID(r *http.Request, id uint) bool {
  74. session, _ := auth.store.Get(r, auth.cookieName)
  75. if sessID, ok := session.Values["user_id"].(uint); !ok || sessID != id {
  76. return false
  77. }
  78. return true
  79. }
  80. func (auth *Auth) isLoggedIn(w http.ResponseWriter, r *http.Request) bool {
  81. session, err := auth.store.Get(r, auth.cookieName)
  82. if err != nil {
  83. session.Values["authenticated"] = false
  84. if err := session.Save(r, w); err != nil {
  85. fmt.Println("error while saving session in isLoggedIn", err)
  86. }
  87. return false
  88. }
  89. if auth, ok := session.Values["authenticated"].(bool); !auth || !ok {
  90. return false
  91. }
  92. return true
  93. }