gui_auth.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package main
  16. import (
  17. "bytes"
  18. "encoding/base64"
  19. "math/rand"
  20. "net/http"
  21. "strings"
  22. "sync"
  23. "time"
  24. "code.google.com/p/go.crypto/bcrypt"
  25. "github.com/syncthing/syncthing/internal/config"
  26. )
  27. var (
  28. sessions = make(map[string]bool)
  29. sessionsMut sync.Mutex
  30. )
  31. func basicAuthAndSessionMiddleware(cfg config.GUIConfiguration, next http.Handler) http.Handler {
  32. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  33. if cfg.APIKey != "" && r.Header.Get("X-API-Key") == cfg.APIKey {
  34. next.ServeHTTP(w, r)
  35. return
  36. }
  37. cookie, err := r.Cookie("sessionid")
  38. if err == nil && cookie != nil {
  39. sessionsMut.Lock()
  40. _, ok := sessions[cookie.Value]
  41. sessionsMut.Unlock()
  42. if ok {
  43. next.ServeHTTP(w, r)
  44. return
  45. }
  46. }
  47. error := func() {
  48. time.Sleep(time.Duration(rand.Intn(100)+100) * time.Millisecond)
  49. w.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"")
  50. http.Error(w, "Not Authorized", http.StatusUnauthorized)
  51. }
  52. hdr := r.Header.Get("Authorization")
  53. if !strings.HasPrefix(hdr, "Basic ") {
  54. error()
  55. return
  56. }
  57. hdr = hdr[6:]
  58. bs, err := base64.StdEncoding.DecodeString(hdr)
  59. if err != nil {
  60. error()
  61. return
  62. }
  63. fields := bytes.SplitN(bs, []byte(":"), 2)
  64. if len(fields) != 2 {
  65. error()
  66. return
  67. }
  68. if string(fields[0]) != cfg.User {
  69. error()
  70. return
  71. }
  72. if err := bcrypt.CompareHashAndPassword([]byte(cfg.Password), fields[1]); err != nil {
  73. error()
  74. return
  75. }
  76. sessionid := randomString(32)
  77. sessionsMut.Lock()
  78. sessions[sessionid] = true
  79. sessionsMut.Unlock()
  80. http.SetCookie(w, &http.Cookie{
  81. Name: "sessionid",
  82. Value: sessionid,
  83. MaxAge: 0,
  84. })
  85. next.ServeHTTP(w, r)
  86. })
  87. }