forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openedx#491 from jauzzz/ironwood
user profile mobile field change
- Loading branch information
Showing
7 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from django.db.models import CharField | ||
|
||
|
||
class HashCharField(CharField): | ||
description = "A hashed field" | ||
|
||
def __init__(self, verbose_name=None, name=None, hasher=None, *args, **kwargs): | ||
self.hasher = hasher | ||
super(HashCharField, self).__init__(*args, **kwargs) | ||
|
||
def deconstruct(self): | ||
name, path, args, kwargs = super(HashCharField, self).deconstruct() | ||
if self.hasher: | ||
kwargs['hasher'] = self.hasher | ||
return name, path, args, kwargs | ||
|
||
def from_db_value(self, value, expression, connection, context): | ||
if value is None: | ||
return value | ||
return self.hasher.decrypt(value) | ||
|
||
def get_prep_value(self, value): | ||
if value is None: | ||
return value | ||
return self.hasher.encrypt(value) | ||
|
||
def get_prep_lookup(self, lookup_type, value): | ||
# TODO: maybe more lookup type, need to check | ||
if lookup_type == 'exact': | ||
return self.get_prep_value(value) | ||
elif lookup_type == 'in': | ||
return [self.get_prep_value(v) for v in value] | ||
else: | ||
raise TypeError('Lookup type %r not supported.' % lookup_type) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import base64 | ||
import hashlib | ||
from Crypto import Random | ||
from Crypto.Cipher import AES | ||
from django.conf import settings | ||
|
||
|
||
class AESCipher(object): | ||
|
||
@classmethod | ||
def encrypt(cls, raw): | ||
if raw is None: | ||
return raw | ||
key = hashlib.sha256(settings.AES_KEY).digest() | ||
raw = cls._pad(raw) | ||
# iv = Random.new().read(AES.block_size) | ||
# use settings iv instead of random to produce same result | ||
iv = settings.AES_IV | ||
cipher = AES.new(key, AES.MODE_CBC, iv) | ||
return base64.b64encode(iv + cipher.encrypt(raw.encode())) | ||
|
||
@classmethod | ||
def decrypt(cls, enc): | ||
if enc is None: | ||
return enc | ||
key = hashlib.sha256(settings.AES_KEY).digest() | ||
enc = base64.b64decode(enc) | ||
iv = enc[:AES.block_size] | ||
cipher = AES.new(key, AES.MODE_CBC, iv) | ||
return cls._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8') | ||
|
||
@classmethod | ||
def _pad(cls, s): | ||
bs = AES.block_size | ||
return s + (bs - len(s) % bs) * chr(bs - len(s) % bs) | ||
|
||
@classmethod | ||
def _unpad(cls, s): | ||
return s[:-ord(s[len(s) - 1:])] |
25 changes: 25 additions & 0 deletions
25
common/djangoapps/student/migrations/0021_hash_userprofile_phone.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.20 on 2019-05-10 06:17 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations | ||
from student.models import UserProfile | ||
from student.hasher import AESCipher | ||
|
||
def forwards_func(apps, schema_editor): | ||
users = UserProfile.objects.all() | ||
hasher = AESCipher | ||
for profile in users: | ||
profile.phone = hasher.encrypt(profile.phone) | ||
profile.save(update_fields=['phone']) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('student', '0020_merge_20190510_0217'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(forwards_func), | ||
] |
22 changes: 22 additions & 0 deletions
22
common/djangoapps/student/migrations/0022_auto_20191213_0427.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.20 on 2019-12-13 09:27 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations | ||
import student.fields | ||
import student.hasher | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('student', '0021_hash_userprofile'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='userprofile', | ||
name='phone', | ||
field=student.fields.HashCharField(hasher=student.hasher.AESCipher, max_length=256, null=True), | ||
), | ||
] |
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
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
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