Skip to content
This repository has been archived by the owner on Jan 26, 2023. It is now read-only.

Add proof-of-concept for encryption #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion controllers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
def service_create():
name = request.form.get('name', '').strip()
icon = request.form.get('icon', '').strip()
encrypted = request.form.get('encrypted', '').strip().lower() in ("1", "true", "yes")
if not name:
return jsonify(Error.ARGUMENT_MISSING('name'))
srv = Service(name, icon)
srv = Service(name, icon, encrypted=encrypted)
db.session.add(srv)
db.session.commit()
return jsonify({"service": srv.as_dict(True)})
Expand Down
1 change: 1 addition & 0 deletions database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ DROP TABLE IF EXISTS `service`;
CREATE TABLE IF NOT EXISTS `pushjet_api`.`service` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`secret` VARCHAR(32) NOT NULL,
`encryption_key` VARCHAR(512) NOT NULL,
`public` VARCHAR(40) NOT NULL,
`name` VARCHAR(255) NOT NULL,
`icon` TEXT NULL,
Expand Down
11 changes: 10 additions & 1 deletion models/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
class Service(db.Model):
id = db.Column(INTEGER(unsigned=True), primary_key=True)
secret = db.Column(db.VARCHAR(32), nullable=False)
encryption_key = db.Column(db.VARCHAR(512), nullable=False)
public = db.Column(db.VARCHAR(40), nullable=False)
name = db.Column(db.VARCHAR(255), nullable=False)
icon = db.Column(db.TEXT, nullable=False, default='')
timestamp_created = db.Column(db.TIMESTAMP, default=datetime.utcnow)

def __init__(self, name, icon=''):
def __init__(self, name, icon='', encrypted=False):
self.secret = hashlib.sha1(urandom(100)).hexdigest()[:32]
if encrypted:
self.encryption_key = hashlib.sha1(urandom(100)).hexdigest()[:32]
else:
self.encryption_key = ""
self.name = name
self.icon = icon
pub = list(hashlib.new('ripemd160', self.secret).hexdigest())[:40]
Expand Down Expand Up @@ -51,7 +56,11 @@ def as_dict(self, secret=False):
"name": self.name,
"created": int(self.timestamp_created.strftime("%s")),
"icon": self.icon,
"encrypted": True if self.encryption_key else False,
}

if secret:
data["secret"] = self.secret
data["encryption_key"] = self.encryption_key

return data