func (c *ServerConnection) handleRegister(req *fosp.Request) *fosp.Response {
	if req.URL.Host != c.server.Domain() {
		return fosp.NewResponse(fosp.FAILED, fosp.StatusBadRequest)
	}
	user := req.URL.User.Username() + "@" + req.URL.Host
	obj := fosp.NewObject()
	if err := json.NewDecoder(req.Body).Decode(obj); err != nil {
		servConnLog.Warning("Unable to decode CREATE body :: %s", err)
		return fosp.NewResponse(fosp.FAILED, fosp.StatusBadRequest)
	}
	data, ok := obj.Data.(map[string]interface{})
	if !ok {
		return fosp.NewResponse(fosp.FAILED, fosp.StatusBadRequest)
	}
	opaque, ok := data["password"]
	if !ok {
		return fosp.NewResponse(fosp.FAILED, fosp.StatusBadRequest)
	}
	password, ok := opaque.(string)
	if !ok {
		return fosp.NewResponse(fosp.FAILED, fosp.StatusBadRequest)
	}
	if !c.server.database.Register(user, password) {
		return fosp.NewResponse(fosp.FAILED, fosp.StatusBadRequest)
	}
	return fosp.NewResponse(fosp.SUCCEEDED, fosp.StatusCreated)
}
func (c *ServerConnection) handleCreate(user string, req *fosp.Request) *fosp.Response {
	defer timeTrack(time.Now(), "create request")
	obj := fosp.NewObject()
	if err := json.NewDecoder(req.Body).Decode(obj); err != nil {
		servConnLog.Warning("Unable to decode CREATE body :: %s", err)
		return fosp.NewResponse(fosp.FAILED, fosp.StatusBadRequest)
	}
	if err := c.server.database.Create(user, req.URL, obj); err != nil {
		return fosp.NewResponse(fosp.FAILED, fosp.StatusInternalServerError)
	}
	return fosp.NewResponse(fosp.SUCCEEDED, fosp.StatusCreated)
}
Exemplo n.º 3
0
// GetObjectWithParents returns an object and all it's parents from the database.
// The parents are stored recursively in the object.
func (d *PostgresqlDriver) GetObjectWithParents(url *url.URL) (fosp.Object, error) {
	urls := urlFamily(url)
	args := make([]interface{}, len(urls))
	params := make([]string, len(urls))
	for i, url := range urls {
		args[i] = url.String()
		params[i] = fmt.Sprintf("$%d", (i + 1))
	}
	psqlLog.Debug("Fetching objects for URLs %v from database", args)
	psqlLog.Debug("SELECT * FROM data WHERE uri IN (" + strings.Join(params, ",") + ") ORDER BY uri ASC")
	rows, err := d.db.Query("SELECT * FROM data WHERE uri IN ("+strings.Join(params, ",")+") ORDER BY uri ASC", args...)
	if err != nil {
		psqlLog.Error("Error when fetching object and parents from database: ", err)
		return fosp.Object{}, InternalServerError
	}
	defer rows.Close()
	var parent *fosp.Object
	var numObjects int
	for rows.Next() {
		var (
			id       uint64
			uri      string
			parentID uint64
			content  string
		)
		if err := rows.Scan(&id, &uri, &parentID, &content); err != nil {
			psqlLog.Error("Error when reading values from object row :: %s", err)
			return fosp.Object{}, InternalServerError
		}
		obj := fosp.NewObject()
		err := json.Unmarshal([]byte(content), obj)
		if err != nil {
			psqlLog.Critical("Error when unmarshaling json ::%T %s", err, err)
			return fosp.Object{}, InternalServerError
		}
		obj.URL, err = url.Parse(uri)
		if err != nil {
			psqlLog.Critical("Error while parsing URL %s :: %s", uri, err)
			return fosp.Object{}, InternalServerError
		}
		obj.Parent = parent
		parent = obj
		numObjects++
	}
	if numObjects != len(urls) {
		psqlLog.Debug("Found only %d objects", numObjects)
		return fosp.Object{}, NewFospError("Object not found", fosp.StatusNotFound)
	}
	return *parent, nil
}
Exemplo n.º 4
0
func (d *Database) Register(user, password string) bool {
	newRoot := fosp.NewObject()
	newRoot.Owner = user
	newRoot.Mtime = time.Now().UTC()
	newRoot.Btime = time.Now().UTC()
	newRoot.Acl = fosp.NewAccessControlList()
	newRoot.Acl.Owner.Data = fosp.NewPermissionSet(fosp.PermissionRead, fosp.PermissionWrite)
	newRoot.Acl.Owner.Acl = fosp.NewPermissionSet(fosp.PermissionRead, fosp.PermissionWrite)
	newRoot.Acl.Owner.Subscriptions = fosp.NewPermissionSet(fosp.PermissionRead, fosp.PermissionWrite)
	newRoot.Acl.Owner.Children = fosp.NewPermissionSet(fosp.PermissionRead, fosp.PermissionWrite, fosp.PermissionDelete)
	newRoot.Acl.Users[user] = newRoot.Acl.Owner
	if !d.driver.Register(user, password, newRoot) {
		return false
	}
	return true
}