Merged
4 changes: 2 additions & 2 deletions README.md
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
4 changes: 2 additions & 2 deletions README.md
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
4 changes: 2 additions & 2 deletions README.md
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
4 changes: 2 additions & 2 deletions README.md
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
4 changes: 2 additions & 2 deletions README.md
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
4 changes: 2 additions & 2 deletions README.md
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
4 changes: 2 additions & 2 deletions README.md
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
4 changes: 2 additions & 2 deletions README.md
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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