2016-11-21 18:48:25 +00:00
|
|
|
// 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 (
|
2018-02-14 13:19:43 +00:00
|
|
|
"time"
|
|
|
|
|
2016-11-21 18:48:25 +00:00
|
|
|
"github.com/abcum/fibre"
|
2017-11-16 20:52:17 +00:00
|
|
|
"github.com/abcum/surreal/cnf"
|
2016-11-21 18:48:25 +00:00
|
|
|
"github.com/abcum/surreal/db"
|
2017-02-12 11:56:46 +00:00
|
|
|
"github.com/abcum/surreal/kvs"
|
2016-11-21 18:48:25 +00:00
|
|
|
"github.com/abcum/surreal/mem"
|
|
|
|
"github.com/abcum/surreal/sql"
|
2017-11-16 20:52:17 +00:00
|
|
|
"github.com/abcum/surreal/util/data"
|
2018-02-14 13:19:43 +00:00
|
|
|
"github.com/dgrijalva/jwt-go"
|
2016-11-21 18:48:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func signup(c *fibre.Context) (err error) {
|
|
|
|
|
|
|
|
var vars map[string]interface{}
|
|
|
|
|
|
|
|
c.Bind(&vars)
|
|
|
|
|
2018-02-17 09:14:26 +00:00
|
|
|
str, err := signupInternal(c, vars)
|
2018-02-14 13:19:43 +00:00
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return c.Send(200, str)
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func signupRpc(c *fibre.Context, vars map[string]interface{}) (res interface{}, err error) {
|
|
|
|
|
|
|
|
var str string
|
|
|
|
|
|
|
|
str, err = signupInternal(c, vars)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = checkBearer(c, str, ignore)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return str, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func signupInternal(c *fibre.Context, vars map[string]interface{}) (str string, err error) {
|
|
|
|
|
2017-11-16 20:52:17 +00:00
|
|
|
n, nok := vars[varKeyNs].(string)
|
|
|
|
d, dok := vars[varKeyDb].(string)
|
|
|
|
s, sok := vars[varKeySc].(string)
|
|
|
|
|
2016-11-21 18:48:25 +00:00
|
|
|
// If we have a namespace, database, and
|
|
|
|
// scope defined, then we are logging in
|
|
|
|
// to the scope level.
|
|
|
|
|
|
|
|
if nok && len(n) > 0 && dok && len(d) > 0 && sok && len(s) > 0 {
|
|
|
|
|
2017-11-16 20:52:17 +00:00
|
|
|
var ok bool
|
2017-02-12 11:56:46 +00:00
|
|
|
var txn kvs.TX
|
2018-02-14 13:19:43 +00:00
|
|
|
var doc *sql.Thing
|
2016-11-21 18:48:25 +00:00
|
|
|
var res []*db.Response
|
2017-11-16 20:52:17 +00:00
|
|
|
var exp *sql.SubExpression
|
2018-03-18 21:30:02 +00:00
|
|
|
var evt *sql.MultExpression
|
2017-02-12 11:56:46 +00:00
|
|
|
var scp *sql.DefineScopeStatement
|
|
|
|
|
|
|
|
// Start a new read transaction.
|
|
|
|
|
|
|
|
if txn, err = db.Begin(false); err != nil {
|
2018-02-14 13:19:43 +00:00
|
|
|
return str, fibre.NewHTTPError(500)
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the transaction closes.
|
|
|
|
|
|
|
|
defer txn.Cancel()
|
2016-11-21 18:48:25 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
// Get the current context.
|
|
|
|
|
|
|
|
ctx := c.Context()
|
|
|
|
|
2018-07-12 01:50:03 +00:00
|
|
|
// Create a temporary context.
|
|
|
|
|
|
|
|
t := fibre.NewContext(
|
|
|
|
c.Request(),
|
|
|
|
c.Response(),
|
|
|
|
c.Fibre(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure we copy the session od.
|
|
|
|
|
|
|
|
t.Set(varKeyUniq, c.Get(varKeyUniq))
|
|
|
|
|
2018-03-18 21:30:02 +00:00
|
|
|
// Give full permissions to scope.
|
|
|
|
|
2018-07-12 01:50:03 +00:00
|
|
|
t.Set(varKeyAuth, &cnf.Auth{Kind: cnf.AuthDB})
|
2018-03-18 21:30:02 +00:00
|
|
|
|
2017-12-07 18:48:42 +00:00
|
|
|
// Specify fields to show in logs.
|
|
|
|
|
|
|
|
f := map[string]interface{}{"ns": n, "db": d, "sc": s}
|
|
|
|
|
2016-11-21 18:48:25 +00:00
|
|
|
// Get the specified signin scope.
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if scp, err = mem.NewWithTX(txn).GetSC(ctx, n, d, s); err != nil {
|
2017-12-07 18:48:42 +00:00
|
|
|
m := "Authentication scope does not exist"
|
2018-02-14 13:19:43 +00:00
|
|
|
return str, fibre.NewHTTPError(403).WithFields(f).WithMessage(m)
|
2016-11-21 18:48:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:52:17 +00:00
|
|
|
// Check that the scope allows signup.
|
|
|
|
|
|
|
|
if exp, ok = scp.Signup.(*sql.SubExpression); !ok {
|
2017-12-07 18:48:42 +00:00
|
|
|
m := "Authentication scope does not allow signup"
|
2018-02-14 13:19:43 +00:00
|
|
|
return str, fibre.NewHTTPError(403).WithFields(f).WithMessage(m)
|
2017-11-16 20:52:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 18:48:25 +00:00
|
|
|
// Process the scope signup statement.
|
|
|
|
|
2017-11-16 20:52:17 +00:00
|
|
|
query := &sql.Query{Statements: []sql.Statement{exp.Expr}}
|
|
|
|
|
|
|
|
// If the query fails then return a 501 error.
|
|
|
|
|
2018-07-12 01:50:03 +00:00
|
|
|
if res, err = db.Process(t, query, vars); err != nil {
|
2017-12-07 18:48:42 +00:00
|
|
|
m := "Authentication scope signup was unsuccessful: Query failed"
|
2018-02-14 13:19:43 +00:00
|
|
|
return str, fibre.NewHTTPError(501).WithFields(f).WithMessage(m)
|
2016-11-21 18:48:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:52:17 +00:00
|
|
|
// If the response is not 1 record then return a 403 error.
|
|
|
|
|
2017-12-07 18:48:42 +00:00
|
|
|
if len(res) != 1 {
|
|
|
|
m := "Authentication scope signup was unsuccessful: Query failed"
|
2018-02-14 13:19:43 +00:00
|
|
|
return str, fibre.NewHTTPError(403).WithFields(f).WithMessage(m)
|
2017-12-07 18:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the response has an error set then return a 403 error.
|
|
|
|
|
|
|
|
if res[0].Status != "OK" {
|
|
|
|
m := "Authentication scope signin was unsuccessful: " + res[0].Detail
|
2018-02-14 13:19:43 +00:00
|
|
|
return str, fibre.NewHTTPError(403).WithFields(f).WithMessage(m)
|
2017-12-07 18:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the response has no record set then return a 403 error.
|
|
|
|
|
|
|
|
if len(res[0].Result) != 1 {
|
|
|
|
m := "Authentication scope signup was unsuccessful: No record created"
|
2018-02-14 13:19:43 +00:00
|
|
|
return str, fibre.NewHTTPError(403).WithFields(f).WithMessage(m)
|
2017-11-16 20:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the query does not return an id field then return a 403 error.
|
|
|
|
|
2018-02-14 13:19:43 +00:00
|
|
|
if doc, ok = data.Consume(res[0].Result[0]).Get("id").Data().(*sql.Thing); !ok {
|
2017-12-07 18:48:42 +00:00
|
|
|
m := "Authentication scope signup was unsuccessful: No id field found"
|
2018-02-14 13:19:43 +00:00
|
|
|
return str, fibre.NewHTTPError(403).WithFields(f).WithMessage(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new token signer with the default claims.
|
|
|
|
|
|
|
|
signr := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims{
|
|
|
|
"NS": n,
|
|
|
|
"DB": d,
|
|
|
|
"SC": s,
|
|
|
|
"TK": "default",
|
2018-03-02 11:02:47 +00:00
|
|
|
"IP": c.IP().String(),
|
2018-02-14 13:19:43 +00:00
|
|
|
"iss": "Surreal",
|
|
|
|
"iat": time.Now().Unix(),
|
|
|
|
"nbf": time.Now().Unix(),
|
|
|
|
"exp": time.Now().Add(scp.Time).Unix(),
|
|
|
|
"TB": doc.TB,
|
|
|
|
"ID": doc.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Try to create the final signed token as a string.
|
|
|
|
|
|
|
|
if str, err = signr.SignedString(scp.Code); err != nil {
|
|
|
|
m := "Problem with signing method: " + err.Error()
|
|
|
|
return str, fibre.NewHTTPError(403).WithFields(f).WithMessage(m)
|
2016-11-21 18:48:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 21:30:02 +00:00
|
|
|
// Check that the scope allows signup.
|
|
|
|
|
|
|
|
if evt, ok = scp.OnSignup.(*sql.MultExpression); ok {
|
|
|
|
|
|
|
|
stmts := make([]sql.Statement, len(evt.Expr))
|
|
|
|
|
|
|
|
for k := range evt.Expr {
|
|
|
|
stmts[k] = evt.Expr[k]
|
|
|
|
}
|
|
|
|
|
|
|
|
query := &sql.Query{Statements: stmts}
|
|
|
|
|
|
|
|
qvars := map[string]interface{}{
|
|
|
|
"id": doc,
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the query fails then return a 501 error.
|
|
|
|
|
2018-07-12 01:50:03 +00:00
|
|
|
if res, err = db.Process(t, query, qvars); err != nil {
|
2018-03-18 21:30:02 +00:00
|
|
|
m := "Authentication scope signup was unsuccessful: `ON SIGNUP` failed:" + err.Error()
|
|
|
|
return str, fibre.NewHTTPError(501).WithFields(f).WithMessage(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-14 13:19:43 +00:00
|
|
|
return str, err
|
2016-11-21 18:48:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-14 13:19:43 +00:00
|
|
|
return str, fibre.NewHTTPError(403)
|
2016-11-21 18:48:25 +00:00
|
|
|
|
|
|
|
}
|