From 55cd1151abb7daec85df6673157e8eca15bc133a Mon Sep 17 00:00:00 2001 From: knight1997 Date: Sun, 17 Mar 2019 23:19:26 +0530 Subject: [PATCH] OTP Function fully completed --- mysite/.gitignore | 1 + mysite/account/.gitignore | 2 + mysite/account/admin.py | 3 +- .../account/migrations/0002_otp_database.py | 22 +++++ .../__pycache__/0001_initial.cpython-36.pyc | Bin 1423 -> 1383 bytes .../__pycache__/__init__.cpython-36.pyc | Bin 183 -> 143 bytes mysite/account/models.py | 6 ++ mysite/account/sendOTP.py | 26 +++++ mysite/account/static/account/otp_css.css | 51 ++++++++++ mysite/account/static/account/otp_js.js | 20 ++++ mysite/account/templates/account/base.html | 2 +- .../templates/account/change_password.html | 20 ++++ mysite/account/templates/account/login.html | 1 + .../templates/account/otp_input_page.html | 62 ++++++++++++ .../templates/account/otp_request_page.html | 60 ++++++++++++ mysite/account/templates/account/profile.html | 10 ++ mysite/account/urls.py | 4 + mysite/account/views.py | 92 +++++++++++++++++- .../__pycache__/__init__.cpython-36.pyc | Bin 0 -> 131 bytes .../__pycache__/settings.cpython-36.pyc | Bin 0 -> 2305 bytes mysite/mysite/__pycache__/urls.cpython-36.pyc | Bin 0 -> 968 bytes mysite/mysite/__pycache__/wsgi.cpython-36.pyc | Bin 0 -> 533 bytes 22 files changed, 379 insertions(+), 3 deletions(-) create mode 100644 mysite/.gitignore create mode 100644 mysite/account/.gitignore create mode 100644 mysite/account/migrations/0002_otp_database.py create mode 100644 mysite/account/sendOTP.py create mode 100644 mysite/account/static/account/otp_css.css create mode 100644 mysite/account/static/account/otp_js.js create mode 100644 mysite/account/templates/account/change_password.html create mode 100644 mysite/account/templates/account/otp_input_page.html create mode 100644 mysite/account/templates/account/otp_request_page.html create mode 100644 mysite/account/templates/account/profile.html create mode 100644 mysite/mysite/__pycache__/__init__.cpython-36.pyc create mode 100644 mysite/mysite/__pycache__/settings.cpython-36.pyc create mode 100644 mysite/mysite/__pycache__/urls.cpython-36.pyc create mode 100644 mysite/mysite/__pycache__/wsgi.cpython-36.pyc diff --git a/mysite/.gitignore b/mysite/.gitignore new file mode 100644 index 0000000..ba520cc --- /dev/null +++ b/mysite/.gitignore @@ -0,0 +1 @@ +db.sqlite3 \ No newline at end of file diff --git a/mysite/account/.gitignore b/mysite/account/.gitignore new file mode 100644 index 0000000..826e47b --- /dev/null +++ b/mysite/account/.gitignore @@ -0,0 +1,2 @@ +credentials.py +__pycache__ \ No newline at end of file diff --git a/mysite/account/admin.py b/mysite/account/admin.py index d5ac218..85769e0 100644 --- a/mysite/account/admin.py +++ b/mysite/account/admin.py @@ -2,9 +2,10 @@ #password: adminpassword from django.contrib import admin -from .models import Case, Profile, Passwords +from .models import Case, Profile, Passwords, Otp_database # Register your models here. admin.site.register(Case) admin.site.register(Passwords) admin.site.register(Profile) +admin.site.register(Otp_database) diff --git a/mysite/account/migrations/0002_otp_database.py b/mysite/account/migrations/0002_otp_database.py new file mode 100644 index 0000000..626e02b --- /dev/null +++ b/mysite/account/migrations/0002_otp_database.py @@ -0,0 +1,22 @@ +# Generated by Django 2.0.9 on 2019-03-17 17:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Otp_database', + fields=[ + ('username', models.CharField(max_length=20, primary_key=True, serialize=False)), + ('otp_id', models.CharField(max_length=10)), + ('otp_text', models.CharField(max_length=10)), + ('timestamp', models.DateTimeField()), + ], + ), + ] diff --git a/mysite/account/migrations/__pycache__/0001_initial.cpython-36.pyc b/mysite/account/migrations/__pycache__/0001_initial.cpython-36.pyc index 665afcc5340c8a84173eec40b77f7110477c78f5..da34e91ad71ad1fd4b04e2a37911927b2c502e24 100644 GIT binary patch delta 37 scmeC@e$K^i%*)HgSJ$_ZJ(`8f!PP1zJ+maEG->h_mKw&W$%3qP0L01)>;M1& delta 77 zcmaFP)z8gt%*)G_xxr*3do+tthO<>nXmM&$ag1+bWoc22OKNd;Nq#|0N>*ZCdcHzH hQGQlxa!GMaL2-FeN^)^(UfJXvmKw%elV7sb0RRJY9IpTX diff --git a/mysite/account/migrations/__pycache__/__init__.cpython-36.pyc b/mysite/account/migrations/__pycache__/__init__.cpython-36.pyc index 8e9ff5eed0f3509cf06b09d740da4b6da4548cc8..e6cea3ed4f11af206bdacff43722654156c782fc 100644 GIT binary patch delta 29 kcmdna*w4sr%*)HgSJyX@-Ga-;)hZ@Evm~Q5X=0%p0B)5CeEPx# diff --git a/mysite/account/models.py b/mysite/account/models.py index 9897fd5..18ca1a9 100644 --- a/mysite/account/models.py +++ b/mysite/account/models.py @@ -18,3 +18,9 @@ class Passwords(models.Model): email = models.CharField(max_length=100) encrypted_password = models.CharField(max_length=100) belongs_to = models.ForeignKey(Profile, on_delete=models.CASCADE) + +class Otp_database(models.Model): + username = models.CharField(max_length=20, primary_key=True) + otp_id = models.CharField(max_length=10) + otp_text = models.CharField(max_length=10) + timestamp = models.DateTimeField() diff --git a/mysite/account/sendOTP.py b/mysite/account/sendOTP.py new file mode 100644 index 0000000..dc9410c --- /dev/null +++ b/mysite/account/sendOTP.py @@ -0,0 +1,26 @@ +from twilio.rest import Client +from .credentials import account_sid, auth_token, my_cell, my_twilio + +# Find these values at https://twilio.com/user/account + + +my_msg = ''.join(['Hi Mayur!!\n' for i in range(100)]) +my_msg+="-from MATHUR ;) " + + +mayur_cell="+917678599539" +def send_otp(reg_number,num): + client = Client(account_sid, auth_token) + reg_number=my_twilio + otp= "Your One Time Password is: " + print(otp+num) + message = client.messages \ + .create( + body=otp+num, + from_=reg_number, + status_callback='http://postb.in/1234abcd', + to=my_cell + ) + return message.sid + +#print(message) diff --git a/mysite/account/static/account/otp_css.css b/mysite/account/static/account/otp_css.css new file mode 100644 index 0000000..9833890 --- /dev/null +++ b/mysite/account/static/account/otp_css.css @@ -0,0 +1,51 @@ +.new-login-area{ + padding:24px; +} +h3 { + font-size: 36px; + line-height: 30px; +} +label { + font-size: 24px; +} +.request-otp-header { + margin: 40px 0px; + font-weight: 900; + +} +.login-label label { + color: #c74032; + font-weight: 600; +} +.input-edit { + border: none !important; + border-bottom: 5px solid #ccc !important; + padding: 6px 0px; + opacity: 0.8; +} +.input-edit:focus { + border-bottom-color: #c74032 !important; + box-shadow: none; + outline: 0; +} + +.request-otp { + margin-top: 40px; + background: linear-gradient(#c74032, #91041b); + font-size: 14px; + color: #fff; +} +.request-otp:focus { + box-shadow: none; +} +.fa-chevron-left { + cursor: pointer; +} +.resend-otp{ + margin-top:6px; + cursor:pointer; +} +.btn-default{ + color: #fff; + background-color: #c74032; +} diff --git a/mysite/account/static/account/otp_js.js b/mysite/account/static/account/otp_js.js new file mode 100644 index 0000000..565c723 --- /dev/null +++ b/mysite/account/static/account/otp_js.js @@ -0,0 +1,20 @@ +//$('#verify-otp').hide(); +//$('#request-otp').on('click',function(){ +//// $.ajax({ +//// type: "GET", +//// url: '\one_time_password_request', +//// success: function(data){ +//// alert(data); +//// } +//// }); +//// $.get('/one_time_password_request', function (data) { +//// //console.log(data); +//// alert(data); +//// }); +// $('#sign-in').hide(); +// $('#verify-otp').show(); +//}); +//$('.fa-chevron-left').on('click',function(){ +// $('#sign-in').show(); +// $('#verify-otp').hide(); +//}); \ No newline at end of file diff --git a/mysite/account/templates/account/base.html b/mysite/account/templates/account/base.html index 43282b8..1dee738 100644 --- a/mysite/account/templates/account/base.html +++ b/mysite/account/templates/account/base.html @@ -28,4 +28,4 @@
Your suitc {% block body %} {% endblock %} - + \ No newline at end of file diff --git a/mysite/account/templates/account/change_password.html b/mysite/account/templates/account/change_password.html new file mode 100644 index 0000000..baf97bc --- /dev/null +++ b/mysite/account/templates/account/change_password.html @@ -0,0 +1,20 @@ +{% extends 'account/base.html' %} +{% block body %} + +
+ {% csrf_token %} +
+ Change your Password. +
+ + +
+
+ + +
+ +
+
+ +{% endblock %} \ No newline at end of file diff --git a/mysite/account/templates/account/login.html b/mysite/account/templates/account/login.html index e354b39..0950ce5 100644 --- a/mysite/account/templates/account/login.html +++ b/mysite/account/templates/account/login.html @@ -25,6 +25,7 @@
+ diff --git a/mysite/account/templates/account/otp_input_page.html b/mysite/account/templates/account/otp_input_page.html new file mode 100644 index 0000000..85f11cb --- /dev/null +++ b/mysite/account/templates/account/otp_input_page.html @@ -0,0 +1,62 @@ +{% extends 'account/base.html' %} +{% block body %} + + + + OTP Request + + + + + + + + + + + + + + + + + + +{% endblock %} \ No newline at end of file diff --git a/mysite/account/templates/account/otp_request_page.html b/mysite/account/templates/account/otp_request_page.html new file mode 100644 index 0000000..41bb41d --- /dev/null +++ b/mysite/account/templates/account/otp_request_page.html @@ -0,0 +1,60 @@ +{% extends 'account/base.html' %} +{% block body %} + + + + OTP Request + + + + + + + + + + + + + + + + + + + + +{% endblock %} \ No newline at end of file diff --git a/mysite/account/templates/account/profile.html b/mysite/account/templates/account/profile.html new file mode 100644 index 0000000..6ade0ca --- /dev/null +++ b/mysite/account/templates/account/profile.html @@ -0,0 +1,10 @@ + + + + + Title + + +

This is your profile.

+ + \ No newline at end of file diff --git a/mysite/account/urls.py b/mysite/account/urls.py index 90968ad..130c2e1 100644 --- a/mysite/account/urls.py +++ b/mysite/account/urls.py @@ -21,4 +21,8 @@ url(r'login/', views.login, name='login'), url(r'profile/(?P[0-9]+)/', views.profile, name='profile'), url(r'addPassword/(?P[0-9]+)/', views.addPassword, name='addPassword'), + url(r'otp_request_page/', views.render_otp_request_page, name='otp_request_page'), + url(r'one_time_password_request/',views.sendOTP,name="one_time_password_request"), + url(r'one_time_password_enter/(?P[A-Za-z0-9_-]{3,20})/',views.render_otp_input_page,name="one_time_password_enter"), + url(r'change_password/(?P[A-Za-z0-9_-]{3,20})/', views.change_password_view, name='change_password'), ] diff --git a/mysite/account/views.py b/mysite/account/views.py index 2f12b60..a366608 100644 --- a/mysite/account/views.py +++ b/mysite/account/views.py @@ -1,10 +1,15 @@ from django.shortcuts import render, redirect from .forms import RegisterForm, LoginForm, AddPasswordForm -from .models import Case, Profile, Passwords +from .models import Case, Profile, Passwords, Otp_database from .encryption import encrypt from .getPasswords import main from django.contrib.auth.models import User from django.contrib.auth import authenticate, login as lgin ,logout as lgout +from .sendOTP import send_otp +import random +import datetime; +from django.utils import timezone +from django.utils.timezone import utc def getFromId(id): @@ -184,3 +189,88 @@ def addPassword(request, id): return render(request, 'account/addPassword.html', context) else: return redirect(login) + +#<================== OTP Functionality =========================> +def render_otp_request_page(request): + return render(request, 'account/otp_request_page.html') + +def change_password_view(request,username): + if request.method == 'POST': + p1=request.POST['password1'] + p2=request.POST['password2'] + if p1 == p2: + obj=Case.objects.get(username=username) + print(obj.password) + obj.password=encrypt(p1) + print(obj.password) + obj.save() + return redirect('login') + context={'error': 404} + return render(request,'account/change_password.html',context) + else: + print(username + " &&&") + context={} + return render(request,'account/change_password.html',context) +def render_otp_input_page(request,username): + print(request) + print(username + "$$$$$$") + if request.method == 'POST': + otp_input=request.POST['otp_number'] + # retrive OTP and other row from DB using username + otp_database=Otp_database.objects.filter(username=username) + print(otp_database) + otp_num=otp_database.values('otp_text')[0]['otp_text'] + t_stamp=otp_database.values('timestamp')[0]['timestamp'] + print(otp_num) + print(t_stamp) + # if timeGap > 2min delete the OTP from DB and send alert OTP expired + diff_time=datetime.datetime.utcnow().replace(tzinfo=utc) - t_stamp + print(diff_time.total_seconds()) + if diff_time.total_seconds() > 120.0 : + #delete from DB + otp_database.delete() + context={"error": 0} + return render(request, 'account/otp_input_page.html', context) + #if OTP didnt match , send Failure and delete the OTP + if otp_input == otp_num: + otp_database.delete() + print(otp_input + "==" + otp_num) + context={"username": username} + return redirect('change_password', username) + else: + otp_database.delete() + context={"error": 1} + return render(request, 'account/otp_input_page.html', context) + else: + context={"username": username} + return render(request, 'account/otp_input_page.html', context) + +def sendOTP(request): + details="heelo" + print(details) + context={'otp_details':details} + print(request) + uname=request.GET['input_username'] + print(uname) + query=Case.objects.filter(username=uname).values('phone_number') + if len(query)!=0: + reg_mobile=query[0]['phone_number'] + else : + context = {"error": "true"} + return render(request, 'account/otp_request_page.html', context) + #generate random number + num=random.randint(100001,999999) + print(num) + print(reg_mobile) + details = send_otp(reg_mobile, str(num)) + #print(details) + #save username and OTP and OTP_id and Timestamp in DB + opt_database=Otp_database() + opt_database.username=uname + opt_database.otp_text=num + opt_database.otp_id='1' + opt_database.timestamp=datetime.datetime.utcnow().replace(tzinfo=utc) + opt_database.save() + return redirect('one_time_password_enter', uname) + #return render(request, 'account/otp_input_page.html', context) #how to send details data to front end? + diff --git a/mysite/mysite/__pycache__/__init__.cpython-36.pyc b/mysite/mysite/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4fd85227a9b43be66eb36e756bd8f2cf51659efe GIT binary patch literal 131 zcmXr!<>lh5>x*Fkg2x~N1{i@12OutH0TL+;!3>&=ek&P@K*9*(mw~HQOnPQXMrl$^ yKw@!md45rfb7FC7Om1ayW(k~%kI&4@EQycTE2zB1VUwGmQks)$2QsM`h#3HAZXYxN literal 0 HcmV?d00001 diff --git a/mysite/mysite/__pycache__/settings.cpython-36.pyc b/mysite/mysite/__pycache__/settings.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..106e973748a8351e551a17cb27a35fb3dc8bbc13 GIT binary patch literal 2305 zcmb7FTT|Oc6t-ny8*>RsNXV^?Xp<&1Mdp$zX{JdLwgR5`!XO!14Ko^9yO!7JVzjGJ z^w5{Q_ZQ?3^e^eP(}(_rKJ`el!DBL0>a|v9&*eL3&z`f|oSQ4n|MvUwU$dFapP8{= z5$J7rXap0=Xc?46oSoIOINRs?xv`nYQy;VO(DEp!6%el#t&BE}rcfRgP!Ua|5}L6J zS_#dfIW&)Mpapaj-2%=Gx~}htTu-pYV+t3`Xcdm z1Lx6~A35~+6GyVz0uspQ>yk2*YX6486&Pmg|6od&N(Fv3j^QlW0Q%c(k^zfZ!nC6&bjam zB_^u|a)>KXX83HV`Ai5XfCxj(b#Z-N+yL`;F{bBZfw%zg7>szX0Dsma;E>q(JMfQ7 zJ;ny~b-9c@lZvU6Q3$5zl(Acm5zEXQ5R;Zi5tmbZ*26+Kux-I6lnGwZt z*~ydTQ|2z8zU;4V{a~o!1H&}Ez-6K_7*J747ZuO#5-VIxcPpBn%Y4!ilhlT{C%FOZ zg%2l~u?cn=J0D;gE=>q2rWA60Rp5ZYu)yNMqy>Z1G2=6#;p$N45QJ=eX82g7*bIEa z&P8QNG%jS}%SouAb8*aGNqJ*dD_5PsVLP5HR+86$s8YWxR$w|L_-ssdYa$i{Qa2ul z1Z&6|F_T&xGo0MSC{g3ajWl^`0aOan_&r_t_H_Z8frEWwUPYL~?rK1_TwASZdAQ@DRl;I9CCV%J zqk^u(HLdFr=h28C7_0}UKztXm1w@!R8(LA18P8tN{w2zT9=Qc138*KXth+meNeBon(v}% zRc`FpB~@0Ug(GFR7VFn*6)7&4i0^yEji#%TD#gx~=$_P8_w;>9QI1*%RsB$^*Fe~U zTAQm&&E2-NE9;e3RR-8vL)Ntxlt-}*Vb|7QHB+j-zSfNLF;CUB8HiG?RRB@lgKeb? z2DW8i+sR#(^$j@{C0?|MS1ONa4 literal 0 HcmV?d00001 diff --git a/mysite/mysite/__pycache__/urls.cpython-36.pyc b/mysite/mysite/__pycache__/urls.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4e3ffa0f416929fd0ef481905067fc948d59adef GIT binary patch literal 968 zcma)5&5qMB5O)5yCG0Ny2tFXCkxHV%tpdcV+7&Ayg!s8c)pp(3ZBxZ|WIHU~1DD=; z4_+x(PP_sq#%Uv}m5|m_#$#vZn{R&n;^ZVe{r+Qm?jiIG9jpNKH}GjID9R9Zs9QSR z&0Owfo@38m>GL299K>AevtWI+?rmMr1Lo1At+REPZq}#0Pblt_jf)W9=)y4k<>Na% zkz!FSYeh^UMHqfwF?>_2vLePXCG-t03vIBHwPD&iXlx|@TCiK4;7h46mx|#+ETrNs z1g=U(G{c&)afo59jH&cEO{vUvLf1qrWu@erM4Mg?9qnp#)fNW>8R(IG(#*BTltPF z=)=zI;hg8CT&7URE?i_UTHGj(!{0E&D0UkUz?V zTG#jSqoy~T!3fP}+1Y)<3MLvUcsD3J{rAtPVy=9+Yn9)s*=`zKPN^|a#8 z@M1h&7G_n?ryrovw^Gpw^m@8`D&1x=l4{fRUvf$7lD&QczPb;>cg~z+@7VbZhj}gK literal 0 HcmV?d00001 diff --git a/mysite/mysite/__pycache__/wsgi.cpython-36.pyc b/mysite/mysite/__pycache__/wsgi.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7a91cabf38f1a213206c179dbc7f90bc9aff7ada GIT binary patch literal 533 zcmYjN!D<^Z5Y=j(*le4$hmy0dhIF^4w@^w$;~JOLDLAEI3~OaIUdw1T%SyK69D2#U z$NodUsGw_4{e_&O^%Af^ZzRpkdr$NJ=qNh=^?Uj!Amks}TcJOH^4q-gAjpJ>Ku{G- z0v|&~i*OPK1cGI2c_3Qi;GX(hcuyvWf{sb|@a}K)b37Qb4E0>*Y>pKxwnkcDWra(~ zY#c>H%iy*|1BTfJu%@0X#cvcaZWw0;ij{)53f7?5npbj{(!2n{W;0%vN@mr5`meC#ggkBpFL$*B%qUC-0LL^+*nKm zlnS>6Xq#+IE|ciL9|swA1NSBeo7QjXo)33&@%4N(xJt+U>+9iYFiyW+UHrK0-@R&n z9~-b%>fChOuAQddLYg{?#=UNg#F_UW*Q^!wI<5_REi2S6@{I!Ke5I^=u_yRIs>m1a x;PJG7HqE77tZt^?xiK58#Aj|`+GszAnmsPJuJfT8@5hcOFg3JjhrXo8^a(=Es0082 literal 0 HcmV?d00001