surrealpatch/web/auth.go

255 lines
6.1 KiB
Go
Raw Normal View History

// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package web
import (
2016-11-04 15:20:31 +00:00
"fmt"
"bytes"
"strings"
"encoding/base64"
"github.com/abcum/fibre"
"github.com/abcum/surreal/cnf"
"github.com/abcum/surreal/db"
"github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/mem"
"github.com/abcum/surreal/sql"
2016-11-04 15:20:31 +00:00
"github.com/dgrijalva/jwt-go"
)
func auth() fibre.MiddlewareFunc {
return func(h fibre.HandlerFunc) fibre.HandlerFunc {
return func(c *fibre.Context) (err error) {
auth := &cnf.Auth{}
c.Set("auth", auth)
2016-11-04 15:20:31 +00:00
// Start off with an authentication level
// which prevents running any sql queries,
// and denies access to all data.
auth.Kind = sql.AuthNO
// Retrieve the current domain host and
// if we are using a subdomain then set
// the NS and DB to the subdomain bits.
bits := strings.Split(c.Request().URL().Host, ".")
subs := strings.Split(bits[0], "-")
if len(subs) == 2 {
auth.Kind = sql.AuthSC
auth.Possible.NS = subs[0]
auth.Selected.NS = subs[0]
auth.Possible.DB = subs[1]
auth.Selected.DB = subs[1]
}
2016-11-04 15:20:31 +00:00
// Retrieve the HTTP Authorization header
// from the request, so that we can detect
// whether it is Basic auth or Bearer auth.
head := c.Request().Header().Get("Authorization")
2016-11-04 15:20:31 +00:00
// Check whether the Authorization header
// is a Basic Auth header, and if it is then
// process this as root authentication.
if head != "" && head[:5] == "Basic" {
base, err := base64.StdEncoding.DecodeString(head[6:])
if err == nil {
user := []byte(cnf.Settings.Auth.User)
pass := []byte(cnf.Settings.Auth.Pass)
cred := bytes.SplitN(base, []byte(":"), 2)
if len(cred) == 2 && bytes.Equal(cred[0], user) && bytes.Equal(cred[1], pass) {
2016-11-04 15:20:31 +00:00
// ------------------------------
// Root authentication
// ------------------------------
auth.Kind = sql.AuthKV
auth.Possible.NS = "*"
auth.Selected.NS = ""
auth.Possible.DB = "*"
auth.Selected.DB = ""
2016-11-04 15:20:31 +00:00
return h(c)
2016-11-04 15:20:31 +00:00
}
}
}
2016-11-04 15:20:31 +00:00
// Check whether the Authorization header
// is a Bearer Auth header, and if it is then
// process this as default authentication.
if head != "" && head[:6] == "Bearer" {
var txn kvs.TX
var vars jwt.MapClaims
var nok, dok, sok, tok, uok bool
var nsv, dbv, scv, tkv, usv string
// Start a new read transaction.
if txn, err = db.Begin(false); err != nil {
return fibre.NewHTTPError(500)
}
// Ensure the transaction closes.
defer txn.Cancel()
// Parse the specified JWT Token.
2016-11-04 15:20:31 +00:00
token, err := jwt.Parse(head[7:], func(token *jwt.Token) (interface{}, error) {
vars = token.Claims.(jwt.MapClaims)
if err := vars.Valid(); err != nil {
return nil, err
2016-11-04 15:20:31 +00:00
}
if val, ok := vars["auth"].(map[string]interface{}); ok {
auth.Data = val
}
nsv, nok = vars["NS"].(string) // Namespace
dbv, dok = vars["DB"].(string) // Database
scv, sok = vars["SC"].(string) // Scope
tkv, tok = vars["TK"].(string) // Token
usv, uok = vars["US"].(string) // Login
if tkv == "default" {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method")
}
}
2016-11-04 15:20:31 +00:00
if nok && dok && sok && tok {
scp, err := mem.New(txn).GetSC(nsv, dbv, scv)
if err != nil {
fmt.Errorf("Credentials failed")
}
auth.Data["scope"] = scp.Name
if tkv != "default" {
key, err := mem.New(txn).GetST(nsv, dbv, scv, tkv)
if err != nil {
fmt.Errorf("Credentials failed")
}
if token.Header["alg"] != key.Type {
return nil, fmt.Errorf("Unexpected signing method")
}
auth.Kind = sql.AuthSC
return key.Code, nil
} else {
auth.Kind = sql.AuthSC
return scp.Code, nil
}
} else if nok && dok && tok {
if tkv != "default" {
key, err := mem.New(txn).GetDT(nsv, dbv, tkv)
if err != nil {
fmt.Errorf("Credentials failed")
}
if token.Header["alg"] != key.Type {
return nil, fmt.Errorf("Unexpected signing method")
}
auth.Kind = sql.AuthDB
return key.Code, nil
} else if uok {
usr, err := mem.New(txn).GetDU(nsv, dbv, usv)
if err != nil {
fmt.Errorf("Credentials failed")
}
auth.Kind = sql.AuthDB
return usr.Code, nil
}
} else if nok && tok {
if tkv != "default" {
key, err := mem.New(txn).GetNT(nsv, tkv)
if err != nil {
fmt.Errorf("Credentials failed")
}
if token.Header["alg"] != key.Type {
return nil, fmt.Errorf("Unexpected signing method")
}
auth.Kind = sql.AuthNS
return key.Code, nil
} else if uok {
usr, err := mem.New(txn).GetNU(nsv, usv)
if err != nil {
fmt.Errorf("Credentials failed")
}
auth.Kind = sql.AuthNS
return usr.Code, nil
}
2016-11-04 15:20:31 +00:00
}
2016-11-04 15:20:31 +00:00
return nil, fmt.Errorf("No available token")
2016-11-04 15:20:31 +00:00
})
2016-11-04 15:20:31 +00:00
if err == nil && token.Valid {
2016-11-04 15:20:31 +00:00
if auth.Kind == sql.AuthNS {
auth.Possible.NS = nsv
auth.Selected.NS = nsv
auth.Possible.DB = "*"
auth.Selected.DB = ""
}
if auth.Kind == sql.AuthDB {
auth.Possible.NS = nsv
auth.Selected.NS = nsv
auth.Possible.DB = dbv
auth.Selected.DB = dbv
}
if auth.Kind == sql.AuthSC {
auth.Possible.NS = nsv
auth.Selected.NS = nsv
auth.Possible.DB = dbv
auth.Selected.DB = dbv
}
2016-11-04 15:20:31 +00:00
return h(c)
}
}
return h(c)
}
}
}