Skip to content
Merged
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ version=v0

## Build status:

- `master` - [![Build Status](https://magnum.travis-ci.com/lavab/api.svg?token=kJbppXeTxzqpCVvt4t5X&branch=master)](https://magnum.travis-ci.com/lavab/api)
- `develop` - [![Build Status](https://magnum.travis-ci.com/lavab/api.svg?token=kJbppXeTxzqpCVvt4t5X&branch=develop)](https://magnum.travis-ci.com/lavab/api)
- `master` - [![Circle CI](https://circleci.com/gh/lavab/api/tree/master.svg?style=svg&circle-token=4a52d619a03d0249906195d6447ceb60a475c0c5)](https://circleci.com/gh/lavab/api/tree/master)
- `develop` - [![Circle CI](https://circleci.com/gh/lavab/api/tree/develop.svg?style=svg&circle-token=4a52d619a03d0249906195d6447ceb60a475c0c5)](https://circleci.com/gh/lavab/api/tree/develop)
40 changes: 40 additions & 0 deletions _research/ws_client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>lavab client</title>
</head>
<body>
<select id="method">
<option>GET</option>
<option>POST</option>
<option>PUT</option>
<option>DELETE</option>
</select>

<input type="text" id="path" /><br>
<textarea id="body"></textarea><br>
<button id="send">Query</button>

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>
<script type="text/javascript">
$(function() {
var sock = new SockJS("http://127.0.0.1:5000/ws");

$("#send").click(function() {
sock.send(JSON.stringify({
id: "random id",
method: $("#method").val(),
path: $("#path").val(),
body: $("#body").val(),
}));
});

sock.onmessage = function(e) {
console.log(JSON.parse(e.data));
};
});
</script>
</body>
</html>
59 changes: 46 additions & 13 deletions _vagrant/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,54 @@
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"

# rethinkdb
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "forwarded_port", guest: 28015, host: 28015
config.vm.network "forwarded_port", guest: 29015, host: 29015
#config.vm.define "redisthink" do |rethinkdb|
#rethinkdb.vm.box = "ubuntu/trusty64"

# redis
config.vm.network "forwarded_port", guest: 6379, host: 6379
# rethinkdb
#rethinkdb.vm.network "forwarded_port", guest: 8080, host: 8080
#rethinkdb.vm.network "forwarded_port", guest: 28015, host: 28015
#rethinkdb.vm.network "forwarded_port", guest: 29015, host: 29015

config.vm.provider "virtualbox" do |v|
v.memory = 2048
v.cpus = 4
end
# redis
#rethinkdb.vm.network "forwarded_port", guest: 6379, host: 6379

#rethinkdb.vm.provider "virtualbox" do |v|
#v.memory = 2048
#v.cpus = 4
#end

# load ansible playbook
#rethinkdb.vm.provision "shell", path: "deploy.sh"
#end

config.vm.define "docker" do |docker|
docker.vm.box = "ubuntu/trusty64"

docker.vm.network "forwarded_port", guest: 4222, host: 4222
docker.vm.network "forwarded_port", guest: 8333, host: 8333
docker.vm.network "forwarded_port", guest: 6379, host: 6379
docker.vm.network "forwarded_port", guest: 8080, host: 8080
docker.vm.network "forwarded_port", guest: 28015, host: 28015
docker.vm.network "forwarded_port", guest: 29015, host: 29015

# load ansible playbook
config.vm.provision "shell", path: "deploy.sh"
docker.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end

docker.vm.provision "docker" do |d|
d.pull_images "apcera/gnatsd"
d.run "apcera/gnatsd",
args: "--name gnatsd -p 4222:4222 -p 8333:8333"

d.pull_images "dockerfile/rethinkdb"
d.run "dockerfile/rethinkdb",
args: "--name rethinkdb -p 8080:8080 -p 28015:28015 -p 29015:29015"

d.pull_images "dockerfile/redis"
d.run "dockerfile/redis",
args: "--name redis -p 6379:6379"
end
end
end
3 changes: 3 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ dependencies:
- wget http://download.redis.io/releases/redis-2.8.18.tar.gz
- tar xvzf redis-2.8.18.tar.gz
- cd redis-2.8.18 && make
- go get github.com/apcera/gnatsd
post:
- rethinkdb --bind all:
background: true
- src/redis-server:
background: true
- gnatsd:
background: true

test:
override:
Expand Down
104 changes: 104 additions & 0 deletions db/table_emails.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package db

import (
"github.com/dancannon/gorethink"

"github.com/lavab/api/models"
)

// Emails implements the CRUD interface for tokens
type EmailsTable struct {
RethinkCRUD
}

// GetEmail returns a token with specified name
func (e *EmailsTable) GetEmail(id string) (*models.Email, error) {
var result models.Email

if err := e.FindFetchOne(id, &result); err != nil {
return nil, err
}

return &result, nil
}

// GetOwnedBy returns all emails owned by id
func (e *EmailsTable) GetOwnedBy(id string) ([]*models.Email, error) {
var result []*models.Email

err := e.WhereAndFetch(map[string]interface{}{
"owner": id,
}, &result)
if err != nil {
return nil, err
}

return result, nil
}

// DeleteOwnedBy deletes all emails owned by id
func (e *EmailsTable) DeleteOwnedBy(id string) error {
return e.Delete(map[string]interface{}{
"owner": id,
})
}

func (e *EmailsTable) CountOwnedBy(id string) (int, error) {
return e.FindByAndCount("owner", id)
}

func (e *EmailsTable) List(
owner string,
sort []string,
offset int,
limit int,
) ([]*models.Email, error) {
// Filter by owner's ID
term := e.GetTable().Filter(map[string]interface{}{
"owner": owner,
})

// If sort array has contents, parse them and add to the term
if sort != nil && len(sort) > 0 {
var conds []interface{}
for _, cond := range sort {
if cond[0] == '-' {
conds = append(conds, gorethink.Desc(cond[1:]))
} else if cond[0] == '+' || cond[0] == ' ' {
conds = append(conds, gorethink.Asc(cond[1:]))
} else {
conds = append(conds, gorethink.Asc(cond))
}
}

term = term.OrderBy(conds...)
}

// Slice the result in 3 cases
if offset != 0 && limit == 0 {
term = term.Skip(offset)
}

if offset == 0 && limit != 0 {
term = term.Limit(limit)
}

if offset != 0 && limit != 0 {
term = term.Slice(offset, offset+limit)
}

// Run the query
cursor, err := term.Run(e.GetSession())
if err != nil {
return nil, err
}

// Fetch the cursor
var resp []*models.Email
err = cursor.All(&resp)
if err != nil {
return nil, err
}

return resp, nil
}
2 changes: 2 additions & 0 deletions env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Flags struct {
RethinkDBKey string
RethinkDBDatabase string

NATSAddress string

YubiCloudID string
YubiCloudKey string
}
5 changes: 5 additions & 0 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env

import (
"github.com/Sirupsen/logrus"
"github.com/apcera/nats"
"github.com/dancannon/gorethink"

"github.com/lavab/api/cache"
Expand All @@ -28,6 +29,10 @@ var (
Contacts *db.ContactsTable
// Reservations is the global instance of ReservationsTable
Reservations *db.ReservationsTable
// Emails is the global instance of EmailsTable
Emails *db.EmailsTable
// Factors contains all currently registered factors
Factors map[string]factor.Factor
// NATS is the encoded connection to the NATS queue
NATS *nats.EncodedConn
)
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ var (
}
return database
}(), "Database name on the RethinkDB server")
// NATS address
natsAddress = flag.String("nats_address", func() string {
address := os.Getenv("NATS_PORT_4222_TCP_ADDR")
if address == "" {
address = "127.0.0.1"
}
return "nats://" + address + ":4222"
}(), "Address of the NATS server")
// YubiCloud params
yubiCloudID = flag.String("yubicloud_id", "", "YubiCloud API id")
yubiCloudKey = flag.String("yubicloud_key", "", "YubiCloud API key")
Expand Down Expand Up @@ -83,6 +91,8 @@ func main() {
RethinkDBKey: *rethinkdbKey,
RethinkDBDatabase: *rethinkdbDatabase,

NATSAddress: *natsAddress,

YubiCloudID: *yubiCloudID,
YubiCloudKey: *yubiCloudKey,
}
Expand Down
4 changes: 2 additions & 2 deletions models/base_encrypted.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ type Encrypted struct {
// Encoding tells the reader how to decode the data; can be "json", "protobuf", maybe more in the future
Encoding string `json:"encoding" gorethink:"encoding"`

// PgpFingerprints contains the fingerprints of the PGP public keys used to encrypt the data.
PgpFingerprints []string `json:"pgp_fingerprints" gorethink:"pgp_fingerprints"`
// PGPFingerprints contains the fingerprints of the PGP public keys used to encrypt the data.
PGPFingerprints []string `json:"pgp_fingerprints" gorethink:"pgp_fingerprints"`

// Data is the raw, PGP-encrypted data
Data string `json:"raw" gorethink:"raw"`
Expand Down
8 changes: 8 additions & 0 deletions models/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ package models
type Email struct {
Resource

// Kind of the email. Value is either sent or received.
Kind string `json:"kind" gorethink:"kind"`

// Who is supposed to receive the email / what email received it.
To []string `json:"to" gorethink:"to"`

// AttachmentsIDs is a slice of the FileIDs associated with this email
// For uploading attachments see `POST /upload`
AttachmentIDs []string `json:"attachments" gorethink:"attachments"`
Expand All @@ -23,4 +29,6 @@ type Email struct {

// ThreadID
ThreadID string `json:"thread_id" gorethink:"thread_id"`

Status string `json:"status" gorethink:"status"`
}
39 changes: 39 additions & 0 deletions routes/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,45 @@ func TestAccountsCreateInvitedExisting(t *testing.T) {
require.Equal(t, "Username already exists", response.Message)
}

func TestAccountsCreateInvitedWeakPassword(t *testing.T) {
const (
username = "jeremy"
password = "c0067d4af4e87f00dbac63b6156828237059172d1bbeac67427345d6a9fda484"
)

// Prepare a token
inviteToken := models.Token{
Resource: models.MakeResource("", "test invite token"),
Type: "invite",
}
inviteToken.ExpireSoon()

err := env.Tokens.Insert(inviteToken)
require.Nil(t, err)

// POST /accounts - invited
result, err := goreq.Request{
Method: "POST",
Uri: server.URL + "/accounts",
ContentType: "application/json",
Body: routes.AccountsCreateRequest{
Username: username,
Password: password,
Token: inviteToken.ID,
},
}.Do()
require.Nil(t, err)

// Unmarshal the response
var response routes.AccountsCreateResponse
err = result.Body.FromJsonTo(&response)
require.Nil(t, err)

// Check the result's contents
require.False(t, response.Success)
require.Equal(t, "Weak password", response.Message)
}

func TestAccountsCreateInvitedExpired(t *testing.T) {
const (
username = "jeremy2"
Expand Down
Loading