Skip to content

Instantly share code, notes, and snippets.

@lujiacn
Created November 20, 2015 08:22
Show Gist options
  • Save lujiacn/1f90b0680b21945660b0 to your computer and use it in GitHub Desktop.
Save lujiacn/1f90b0680b21945660b0 to your computer and use it in GitHub Desktop.
package controllers
import (
"encoding/base64"
"errors"
"github.com/robfig/revel"
"net/http"
"strings"
)
type Admin struct {
*revel.Controller
}
func checkAuthentifcation(c *revel.Controller) revel.Result {
correctUsername, _ := revel.Config.String("admin.username")
correctPassword, _ := revel.Config.String("admin.password")
if auth := c.Request.Header.Get("Authorization"); auth != "" {
// Split up the string to get just the data, then get the credentials
username, password, err := getCredentials(strings.Split(auth, " ")[1])
if err != nil {
return c.RenderError(err)
}
if username != correctUsername || password != correctPassword {
c.Response.Status = http.StatusUnauthorized
c.Response.Out.Header().Set("WWW-Authenticate", `Basic realm="revel"`)
return c.RenderError(errors.New("401: Not authorized"))
}
return nil
} else {
c.Response.Status = http.StatusUnauthorized
c.Response.Out.Header().Set("WWW-Authenticate", `Basic realm="revel"`)
return c.RenderError(errors.New("401: Not authorized"))
}
}
func init() {
revel.InterceptFunc(checkAuthentifcation, revel.BEFORE, &Admin{})
}
// GET: Admin Index
func (c Admin) Index() revel.Result {
return c.Render()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment