In Eve, how can you store the user's password securely?

sam

If you put a debugger in the run file, you will see that the user's password is hashed, but when you look in the mongo collection, the user's password is stored in plain text. How do you save the user's password as a hash?

Here are my files:

run.py:

from eve import Eve
from eve.auth import BasicAuth

import bcrypt

class BCryptAuth(BasicAuth):
    def check_auth(self, username, password, allowed_roles, resource, method):
        # use Eve's own db driver; no additional connections/resources are used
        accounts = app.data.driver.db["accounts"]
        account = accounts.find_one({"username": username})
        return account and \
            bcrypt.hashpw(password, account['password']) == account['password']

def create_user(*arguments, **keywords):
    password = arguments[0][0]['password']
    username = arguments[0][0]['username']
    user = {
        "password": bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()),
        "username": username,
    }
    return post_internal("accounts", user)


app = Eve(auth=BCryptAuth)
app.on_insert_accounts += create_user

if __name__ == '__main__':
    app.run()

settings.py:

API_NAME = "gametest"

CACHE_CONTROL = "max-age=20"
CACHE_EXPIRES = 20
MONGO_DBNAME = "gametest"
MONGO_HOST = "localhost"
MONGO_PORT = 27017
PUBLIC_ITEM_METHODS = ["GET"]
RESOURCE_METHODS = ["GET"]

accounts_schema = {
    "username": {
        "type": "string",
        "required": True,
        "unique": True,
    },
    "password": {
        "type": "string",
        "required": True,
    },
}

accounts = {
    # the standard account entry point is defined as
    # '/accounts/<ObjectId>'. We define  an additional read-only entry
    # point accessible at '/accounts/<username>'.
    "additional_lookup": {
        "url": "regex('[\w]+')",
        "field": "username",
    },

    # We also disable endpoint caching as we don't want client apps to
    # cache account data.
    "cache_control": "",
    "cache_expires": 0,

    # Finally, let's add the schema definition for this endpoint.
    "schema": accounts_schema,
    "public_methods": ["POST"],
    "resource_methods": ["POST"],
}
games_schema = {
    "game_id": {
        "type": "objectid",
        "required": True
    },
    "title": {
        "type": "string",
        "required": True
    },
}

games = {
    "item_title": "game",
    "schema": games_schema,
}

orders = {
    "schema": {
        "game": {
            "type": "objectid",
            "required": True,
        },
    },
    "resource_methods": ["GET", "POST"],
}

DOMAIN = {
    "accounts", accounts,
    "orders": orders,
    "games": game,
}
Jen Garcia

There were a few major things in your run.py that were preventing you from authenticating:

  • In your create_user event hook you were generating a salt with bcrypt.gensalt(), but you weren't saving the salt anywhere. Salts are useful for preventing rainbow table attacks, but you need to save them so that when you try to hash the password again you get the same result.
  • You're using the on_insert_accounts event hook to modify the document before it's posted, but then returning a post_internal instead of letting the event hook run its course. This might work, but I feel like you should just use the event hook as it was intended.

Here is the modified run.py:

from eve import Eve
from eve.auth import BasicAuth

import bcrypt

class BCryptAuth(BasicAuth):
    def check_auth(self, username, password, allowed_roles, resource, method):
        # use Eve's own db driver; no additional connections/resources are used
        accounts = app.data.driver.db["accounts"]
        account = accounts.find_one({"username": username})
        return account and \
            bcrypt.hashpw(password.encode('utf-8'), account['salt'].encode('utf-8')) == account['password']

def create_user(documents):
    for document in documents:
        document['salt'] = bcrypt.gensalt().encode('utf-8')
        password = document['password'].encode('utf-8')
        document['password'] = bcrypt.hashpw(password, document['salt'])

app = Eve(auth=BCryptAuth)
app.on_insert_accounts += create_user

if __name__ == '__main__':
    app.run()

There were a few typos in your settings.py, so I'm including a working version here for good measure:

API_NAME = "gametest"

CACHE_CONTROL = "max-age=20"
CACHE_EXPIRES = 20
MONGO_DBNAME = "gametest"
MONGO_HOST = "localhost"
MONGO_PORT = 27017
PUBLIC_ITEM_METHODS = ["GET"]
RESOURCE_METHODS = ["GET"]

accounts_schema = {
    "username": {
        "type": "string",
        "required": True,
        "unique": True
    },
    "password": {
        "type": "string",
        "required": True
    }
}

accounts = {
    # the standard account entry point is defined as
    # '/accounts/<ObjectId>'. We define  an additional read-only entry
    # point accessible at '/accounts/<username>'.
    "additional_lookup": {
        "url": "regex('[\w]+')",
        "field": "username",
    },

    # We also disable endpoint caching as we don't want client apps to
    # cache account data.
    "cache_control": "",
    "cache_expires": 0,

    # Finally, let's add the schema definition for this endpoint.
    "schema": accounts_schema,
    "public_methods": ["POST"],
    "resource_methods": ["POST"]
}
games_schema = {
    "game_id": {
        "type": "objectid",
        "required": True
    },
    "title": {
        "type": "string",
        "required": True
    }
}

games = {
    "item_title": "game",
    "schema": games_schema
}

orders = {
    "schema": {
        "game": {
            "type": "objectid",
            "required": True,
        }
    },
    "resource_methods": ["GET", "POST"]
}

DOMAIN = {
    "accounts": accounts,
    "orders": orders,
    "games": games
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to store password in angularjs securely

From Dev

How can I store password securely in MySQL and authenticate to external services

From Dev

How can I store password securely in MySQL and authenticate to external services

From Dev

Laravel 3 - how to securely store user's data in ubuntu server

From Dev

How to store a password as securely in Chrome Extension?

From Dev

How to store samba (cifs) password securely?

From Dev

How to securely store user passwords in a Cloudant DB?

From Dev

Can I securely store username and password in PHP session variables?

From Dev

How can you pass user input from a text box into a function securely, and then output to DOM?

From Dev

How to securely send/store password in a Spring RESTful login service

From Dev

Golang/App Engine - securely hashing a user's password

From Dev

How to handle JSON Store if user's password has been used?

From Dev

How to store user/password in android?

From Dev

How Can I Use the Android KeyStore to securely store arbitrary strings?

From Dev

How can I securely store my AWS keys for an app on AppHarbor?

From Dev

How can I securely store EC private key in Android?

From Dev

Store a key or password securely locally on android

From Dev

Any way to store a password securely in an application

From Dev

Securely store token/password in Chrome extension

From Dev

Any way to store a password securely in an application

From Dev

How to change password securely in Meteor?

From Dev

How 'exposed' do you store a password in a session?

From Dev

Passing a user and password in a clickable URI link securely

From Dev

How to Securely Store Various Credentials?

From Dev

How can I find what password store Chromium's password manager is using?

From Dev

How to store a password when you need the actual password text

From Dev

Joomla 3.3 - How to store user password?

From Dev

How do you configure Netplan on Ubuntu to store 802.1x credentials securely?

From Dev

How to securely log user statistics

Related Related

  1. 1

    How to store password in angularjs securely

  2. 2

    How can I store password securely in MySQL and authenticate to external services

  3. 3

    How can I store password securely in MySQL and authenticate to external services

  4. 4

    Laravel 3 - how to securely store user's data in ubuntu server

  5. 5

    How to store a password as securely in Chrome Extension?

  6. 6

    How to store samba (cifs) password securely?

  7. 7

    How to securely store user passwords in a Cloudant DB?

  8. 8

    Can I securely store username and password in PHP session variables?

  9. 9

    How can you pass user input from a text box into a function securely, and then output to DOM?

  10. 10

    How to securely send/store password in a Spring RESTful login service

  11. 11

    Golang/App Engine - securely hashing a user's password

  12. 12

    How to handle JSON Store if user's password has been used?

  13. 13

    How to store user/password in android?

  14. 14

    How Can I Use the Android KeyStore to securely store arbitrary strings?

  15. 15

    How can I securely store my AWS keys for an app on AppHarbor?

  16. 16

    How can I securely store EC private key in Android?

  17. 17

    Store a key or password securely locally on android

  18. 18

    Any way to store a password securely in an application

  19. 19

    Securely store token/password in Chrome extension

  20. 20

    Any way to store a password securely in an application

  21. 21

    How to change password securely in Meteor?

  22. 22

    How 'exposed' do you store a password in a session?

  23. 23

    Passing a user and password in a clickable URI link securely

  24. 24

    How to Securely Store Various Credentials?

  25. 25

    How can I find what password store Chromium's password manager is using?

  26. 26

    How to store a password when you need the actual password text

  27. 27

    Joomla 3.3 - How to store user password?

  28. 28

    How do you configure Netplan on Ubuntu to store 802.1x credentials securely?

  29. 29

    How to securely log user statistics

HotTag

Archive