Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions db/table_accounts.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package db

import (
"errors"

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

// AccountsTable implements the CRUD interface for accounts
type AccountsTable struct {
RethinkCRUD

Tokens *TokensTable
}

// GetAccount returns an account with specified ID
Expand All @@ -30,3 +34,14 @@ func (users *AccountsTable) FindAccountByName(name string) (*models.Account, err

return &result, nil
}

func (a *AccountsTable) GetTokenOwner(token *models.Token) (*models.Account, error) {
user, err := a.GetAccount(token.Owner)
if err != nil {
// Try to remove the orphaned token
a.Tokens.DeleteID(token.ID)
return nil, errors.New("Account disabled")
}

return user, nil
}
42 changes: 42 additions & 0 deletions db/table_contacts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package db

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

// Contacts implements the CRUD interface for tokens
type ContactsTable struct {
RethinkCRUD
}

// GetContact returns a token with specified name
func (c *ContactsTable) GetContact(id string) (*models.Contact, error) {
var result models.Contact

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

return &result, nil
}

// GetOwnedBy returns all contacts owned by id
func (c *ContactsTable) GetOwnedBy(id string) ([]*models.Contact, error) {
var result []*models.Contact

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

return result, nil
}

// DeleteOwnedBy deletes all contacts owned by id
func (c *ContactsTable) DeleteOwnedBy(id string) error {
return c.Delete(map[string]interface{}{
"owner": id,
})
}
4 changes: 2 additions & 2 deletions db/table_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func (t *TokensTable) GetToken(id string) (*models.Token, error) {
return &result, nil
}

// DeleteByOwner deletes all tokens owned by id
func (t *TokensTable) DeleteByOwner(id string) error {
// DeleteOwnedBy deletes all tokens owned by id
func (t *TokensTable) DeleteOwnedBy(id string) error {
return t.Delete(map[string]interface{}{
"owner": id,
})
Expand Down
2 changes: 2 additions & 0 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ var (
Tokens *db.TokensTable
// Keys is the global instance of KeysTable
Keys *db.KeysTable
// Contacts is the global instance of ContactsTable
Contacts *db.ContactsTable
)
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,20 @@ func main() {
env.Rethink = rethinkSession

// Initialize the tables
env.Accounts = &db.AccountsTable{
env.Tokens = &db.TokensTable{
RethinkCRUD: db.NewCRUDTable(
rethinkSession,
rethinkOpts.Database,
"accounts",
"tokens",
),
}
env.Tokens = &db.TokensTable{
env.Accounts = &db.AccountsTable{
RethinkCRUD: db.NewCRUDTable(
rethinkSession,
rethinkOpts.Database,
"tokens",
"accounts",
),
Tokens: env.Tokens,
}
env.Keys = &db.KeysTable{
RethinkCRUD: db.NewCRUDTable(
Expand All @@ -125,6 +126,13 @@ func main() {
"keys",
),
}
env.Contacts = &db.ContactsTable{
RethinkCRUD: db.NewCRUDTable(
rethinkSession,
rethinkOpts.Database,
"contacts",
),
}

// Create a new goji mux
mux := web.New()
Expand Down
2 changes: 1 addition & 1 deletion models/base_encrypted.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Encrypted struct {
PgpFingerprints []string `json:"pgp_fingerprints" gorethink:"pgp_fingerprints"`

// Data is the raw, PGP-encrypted data
Data []byte `json:"raw" gorethink:"raw"`
Data string `json:"raw" gorethink:"raw"`

// Schema is the name of the schema used to encode the data
// Examples: string, contact, email
Expand Down
28 changes: 8 additions & 20 deletions routes/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func AccountsCreate(w http.ResponseWriter, r *http.Request) {

env.Log.WithFields(logrus.Fields{
"error": err,
}).Error("Could not insert an user to the database")
}).Error("Could not insert an user into the database")
return
}

Expand Down Expand Up @@ -247,7 +247,7 @@ func AccountsGet(c web.C, w http.ResponseWriter, r *http.Request) {
}

// Fetch the current session from the database
session := c.Env["session"].(*models.Token)
session := c.Env["token"].(*models.Token)

// Fetch the user object from the database
user, err := env.Accounts.GetAccount(session.Owner)
Expand Down Expand Up @@ -336,7 +336,7 @@ func AccountsUpdate(c web.C, w http.ResponseWriter, r *http.Request) {
}

// Fetch the current session from the database
session := c.Env["session"].(*models.Token)
session := c.Env["token"].(*models.Token)

// Fetch the user object from the database
user, err := env.Accounts.GetAccount(session.Owner)
Expand Down Expand Up @@ -439,7 +439,7 @@ func AccountsDelete(c web.C, w http.ResponseWriter, r *http.Request) {
}

// Fetch the current session from the database
session := c.Env["session"].(*models.Token)
session := c.Env["token"].(*models.Token)

// Fetch the user object from the database
user, err := env.Accounts.GetAccount(session.Owner)
Expand Down Expand Up @@ -478,7 +478,7 @@ func AccountsDelete(c web.C, w http.ResponseWriter, r *http.Request) {
// TODO: Delete threads

// Delete tokens
err = env.Tokens.DeleteByOwner(user.ID)
err = env.Tokens.DeleteOwnedBy(user.ID)
if err != nil {
env.Log.WithFields(logrus.Fields{
"id": user.ID,
Expand Down Expand Up @@ -540,29 +540,17 @@ func AccountsWipeData(c web.C, w http.ResponseWriter, r *http.Request) {
}

// Fetch the current session from the database
session := c.Env["session"].(*models.Token)
session := c.Env["token"].(*models.Token)

// Fetch the user object from the database
user, err := env.Accounts.GetAccount(session.Owner)
user, err := env.Accounts.GetTokenOwner(session)
if err != nil {
// The session refers to a non-existing user
env.Log.WithFields(logrus.Fields{
"id": session.ID,
"error": err,
}).Warn("Valid session referred to a removed account")

// Try to remove the orphaned session
if err := env.Tokens.DeleteID(session.ID); err != nil {
env.Log.WithFields(logrus.Fields{
"id": session.ID,
"error": err,
}).Error("Unable to remove an orphaned session")
} else {
env.Log.WithFields(logrus.Fields{
"id": session.ID,
}).Info("Removed an orphaned session")
}

utils.JSONResponse(w, 410, &AccountsWipeDataResponse{
Success: false,
Message: "Account disabled",
Expand All @@ -579,7 +567,7 @@ func AccountsWipeData(c web.C, w http.ResponseWriter, r *http.Request) {
// TODO: Delete threads

// Delete tokens
err = env.Tokens.DeleteByOwner(user.ID)
err = env.Tokens.DeleteOwnedBy(user.ID)
if err != nil {
env.Log.WithFields(logrus.Fields{
"id": user.ID,
Expand Down
Loading