1
0
Fork 0
mirror of https://github.com/shouptech/tempgopher.git synced 2026-02-03 08:39:43 +00:00

Succesfully redirect if 401

This commit is contained in:
Emma 2018-10-13 09:32:13 -06:00
parent ac50b755d1
commit dd3e78eb28
3 changed files with 81 additions and 4 deletions

76
auth.go Normal file
View file

@ -0,0 +1,76 @@
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
// Modified to remove the WWW-Authenticate header for uses in TempGopher
package main
import (
"encoding/base64"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
type authPair struct {
value string
user string
}
type authPairs []authPair
func (a authPairs) searchCredential(authValue string) (string, bool) {
if authValue == "" {
return "", false
}
for _, pair := range a {
if pair.value == authValue {
return pair.user, true
}
}
return "", false
}
// BasicAuth returns a Basic HTTP Authorization middleware. It takes as arguments a map[string]string where
// the key is the user name and the value is the password. This does not set a www-authenticate header.
func BasicAuth(accounts gin.Accounts) gin.HandlerFunc {
pairs := processAccounts(accounts)
return func(c *gin.Context) {
// Search user in the slice of allowed credentials
user, found := pairs.searchCredential(c.GetHeader("Authorization"))
if !found {
// Credentials doesn't match, we return 401 and abort handlers chain.
c.AbortWithStatus(http.StatusUnauthorized)
return
}
// The user credentials was found, set user's id to key AuthUserKey in this context, the user's id can be read later using
// c.MustGet(gin.AuthUserKey).
c.Set(gin.AuthUserKey, user)
}
}
func processAccounts(accounts gin.Accounts) authPairs {
if len(accounts) == 0 {
log.Panic("Empty list of authorized credentials")
}
pairs := make(authPairs, 0, len(accounts))
for user, password := range accounts {
if user == "" {
log.Panic("User can not be empty")
}
value := authorizationHeader(user, password)
pairs = append(pairs, authPair{
value: value,
user: user,
})
}
return pairs
}
func authorizationHeader(user, password string) string {
base := user + ":" + password
return "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
}

View file

@ -12,10 +12,10 @@ function redirectIfNotAuthorized() {
beforeSend: authHeaders,
statusCode: {
401: function() {
window.location.replace(baseurl + "/app/login.html");
window.location.replace(jsconfig.baseurl + "/app/login.html");
},
403: function() {
window.location.replace(baseurl + "/app/login.html");
window.location.replace(jsconfig.baseurl + "/app/login.html");
}
}
});

5
web.go
View file

@ -130,7 +130,8 @@ func SetupRouter(config *Config, states *map[string]State) *gin.Engine {
if len(config.Users) == 0 {
api = r.Group("/api")
} else {
api = r.Group("/api", gin.BasicAuth(GetGinAccounts(config)))
api = r.Group("/api")
api.Use(BasicAuth(GetGinAccounts(config)))
}
api.GET("/status", StatusHandler(states))
@ -166,7 +167,7 @@ func reloadWebConfig(c *Config, p string) error {
// GetGinAccounts returns a gin.Accounts struct with values pulled from a Config struct
func GetGinAccounts(config *Config) gin.Accounts {
var a gin.Accounts
a := make(gin.Accounts)
for _, user := range config.Users {
a[user.Name] = user.Password
}