-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP web ui initial commit * update url structure * web ui progress * configure CI to run golint * actually install golint before running it * add a GetTotalPoints() method to database * add templating, better webui config, and a functional site * oops * implement authentication * update README to include documentatin for the web UI * that might not have been necessary after all * update circleci config
- Loading branch information
Showing
17 changed files
with
951 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package webui | ||
|
||
import ( | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/pquerna/otp/totp" | ||
"github.com/satori/go.uuid" | ||
) | ||
|
||
type authMiddleware struct { | ||
handler http.Handler | ||
} | ||
|
||
type Client struct { | ||
UUID string | ||
Added time.Time | ||
} | ||
|
||
var ( | ||
authedClients []*Client | ||
clientsMutex sync.RWMutex | ||
) | ||
|
||
func mustAuth(h http.HandlerFunc) *authMiddleware { | ||
return &authMiddleware{http.HandlerFunc(h)} | ||
} | ||
|
||
func (h *authMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
cookie, err := r.Cookie("session") | ||
|
||
if err != nil { | ||
if err != http.ErrNoCookie { | ||
config.Logger.Printf("could not auth user: %s\n", err.Error()) | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
|
||
return | ||
} | ||
|
||
err = nil | ||
} | ||
|
||
authed := false | ||
if cookie != nil { | ||
clientsMutex.RLock() | ||
for _, client := range authedClients { | ||
if cookie.Value == client.UUID { | ||
authed = true | ||
break | ||
} | ||
} | ||
clientsMutex.RUnlock() | ||
} | ||
|
||
if !authed && hasValidTOTPToken(r) { | ||
cookie = &http.Cookie{ | ||
Name: "session", | ||
Value: uuid.NewV4().String(), | ||
} | ||
|
||
clientsMutex.Lock() | ||
authedClients = append(authedClients, &Client{ | ||
UUID: cookie.Value, | ||
Added: time.Now(), | ||
}) | ||
clientsMutex.Unlock() | ||
|
||
authed = true | ||
http.SetCookie(w, cookie) | ||
} | ||
|
||
if !authed { | ||
renderTemplate(w, "unauthed.html", &templateData{ | ||
Config: config, | ||
Data: nil, | ||
}) | ||
return | ||
} | ||
|
||
h.handler.ServeHTTP(w, r) | ||
} | ||
|
||
func hasValidTOTPToken(r *http.Request) bool { | ||
token := r.URL.Query().Get("token") | ||
if token == "" { | ||
return false | ||
} | ||
|
||
return totp.Validate(token, config.TOTPKey) | ||
} | ||
|
||
func expireClients() { | ||
for { | ||
<-time.After(2 * time.Minute) | ||
go func() { | ||
now := time.Now() | ||
|
||
for i, client := range authedClients { | ||
if now.Sub(client.Added).Hours() >= 48 { | ||
authedClients = append(authedClients[:i], authedClients[i+1:]...) | ||
} | ||
} | ||
}() | ||
} | ||
} |
Oops, something went wrong.