syncthing/cmd/syncthing/gui_auth.go

98 lines
2.1 KiB
Go
Raw Normal View History

2014-11-16 21:13:20 +01:00
// Copyright (C) 2014 The Syncthing Authors.
2014-09-29 21:43:32 +02:00
//
2015-03-07 21:36:35 +01:00
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
2014-09-01 22:51:44 +02:00
package main
import (
"bytes"
"encoding/base64"
"math/rand"
"net/http"
"strings"
"time"
2014-09-22 21:42:11 +02:00
"github.com/syncthing/syncthing/internal/config"
2015-04-23 00:54:31 +02:00
"github.com/syncthing/syncthing/internal/sync"
"golang.org/x/crypto/bcrypt"
2014-09-01 22:51:44 +02:00
)
var (
2015-04-28 22:32:10 +02:00
sessions = make(map[string]bool)
sessionsMut = sync.NewMutex()
2014-09-01 22:51:44 +02:00
)
func basicAuthAndSessionMiddleware(cookieName string, cfg config.GUIConfiguration, next http.Handler) http.Handler {
2014-09-01 22:51:44 +02:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if cfg.APIKey != "" && r.Header.Get("X-API-Key") == cfg.APIKey {
next.ServeHTTP(w, r)
return
}
cookie, err := r.Cookie(cookieName)
2014-09-01 22:51:44 +02:00
if err == nil && cookie != nil {
sessionsMut.Lock()
_, ok := sessions[cookie.Value]
sessionsMut.Unlock()
if ok {
next.ServeHTTP(w, r)
return
}
}
if debugHTTP {
l.Debugln("Sessionless HTTP request with authentication; this is expensive.")
}
2014-09-01 22:51:44 +02:00
error := func() {
time.Sleep(time.Duration(rand.Intn(100)+100) * time.Millisecond)
w.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"")
http.Error(w, "Not Authorized", http.StatusUnauthorized)
}
hdr := r.Header.Get("Authorization")
if !strings.HasPrefix(hdr, "Basic ") {
error()
return
}
hdr = hdr[6:]
bs, err := base64.StdEncoding.DecodeString(hdr)
if err != nil {
error()
return
}
fields := bytes.SplitN(bs, []byte(":"), 2)
if len(fields) != 2 {
error()
return
}
if string(fields[0]) != cfg.User {
error()
return
}
if err := bcrypt.CompareHashAndPassword([]byte(cfg.Password), fields[1]); err != nil {
error()
return
}
sessionid := randomString(32)
sessionsMut.Lock()
sessions[sessionid] = true
sessionsMut.Unlock()
http.SetCookie(w, &http.Cookie{
Name: cookieName,
2014-09-01 22:51:44 +02:00
Value: sessionid,
MaxAge: 0,
})
next.ServeHTTP(w, r)
})
}