Created
August 6, 2017 16:26
-
-
Save stefbrad15/8e34e482a95b1bb8455647d137d3a72e to your computer and use it in GitHub Desktop.
Helpers module for the python flask app where we start working with users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import entire module | |
import user_helpers | |
# Import just the class | |
from dynamo_helper import DynamoInterface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto.dynamodb2 | |
from boto.dynamodb2.fields import HashKey, RangeKey, KeysOnlyIndex, GlobalAllIndex | |
from boto.dynamodb2.table import Table | |
from boto.dynamodb2.types import NUMBER | |
class DynamoInterface(): | |
def __init__(self): | |
# Provide a way to list_tables | |
self.conn = boto.dynamodb.connect_to_region('us-east-1') #N Virginia region | |
def list_tables(self): | |
""" | |
List tables to determine if a table should be created | |
This will need to be done if for instance we switch from a beta instance to prod | |
""" | |
return self.conn.list_tables() | |
def get_table(self, table_name): | |
return Table(table_name) | |
def create_table(self, table_name, hashkey, rangekey, throughput_dict): | |
schema_list = [ | |
HashKey(hashkey), | |
RangeKey(rangekey) | |
] | |
return Table.create(table_name, schema=schema_list, throughput=throughput_dict) | |
def get_all_items(self, table_name, attributes=None): | |
table = Table(table_name) | |
items = table.scan(attributes = attributes) | |
to_list = list() | |
for i in items: | |
to_list.append(dict(i)) | |
return to_list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
import binascii | |
import time | |
def create_user_table(dynamo_interface, logger=None): | |
""" | |
Takes a boto.dynamodb.conn object and creates | |
a users table on it. | |
We want to do this programatically so we can easily | |
create this table across different domain and realms | |
""" | |
throughput = { | |
'read' : 2, | |
'write' : 2 | |
} | |
table = dynamo_interface.create_table( | |
'users', | |
'username', | |
'password', | |
throughput) | |
time.sleep(10) # give aws time to make the table | |
users = seed_user_table(table) | |
if logger: | |
logger.debug(table) | |
return users | |
def seed_user_table(table): | |
user_array = list() | |
user1 = { | |
'username' : 'johndoe', | |
'first_name' : 'John', | |
'last_name' : 'Doe', | |
'account_type' : 'admin_user', | |
'password' : encrypt_password("JohnPass123") | |
} | |
user2 = { | |
'username' : 'janedoe', | |
'first_name' : 'Jane', | |
'last_name' : 'Doe', | |
'account_type' : 'standard_user', | |
'password' : encrypt_password("JanePass123") | |
} | |
# check put success | |
if table.put_item(user1): | |
user_array.append(user1) | |
if table.put_item(user2): | |
user_array.append(user2) | |
return user_array | |
def encrypt_password(password): | |
""" | |
Encrypt and salt the provided password using sha256 | |
""" | |
# derived_key | |
dk = hashlib.pbkdf2_hmac('sha256', password, "hello_flask", 100007) | |
return binascii.hexlify(dk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment