Created
May 20, 2016 12:21
-
-
Save yinzishao/258766fee449c779fbc4e33251c6a3f2 to your computer and use it in GitHub Desktop.
token-api auth
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
from django.contrib import admin | |
# Register your models here. |
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
from django.db import models | |
# Create your models here. |
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
{% block content %} | |
{% if form.errors %} | |
<p class="error">Sorry, that's not a valid username or password</p> | |
{% endif %} | |
<form action="/auth/" method="post">{% csrf_token %} | |
<label for="username">User name:</label> | |
<input type="text" name="username" value="" id="username"> | |
<label for="password">Password:</label> | |
<input type="password" name="password" value="" id="password"> | |
<input type="submit" value="login" /> | |
<input type="hidden" name="next" value="{{ next|escape }}" /> | |
</form> | |
{% endblock %} |
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
<!DOCTYPE html> | |
<html> | |
<head></head> | |
<body> | |
{{ user.date_joined }} | |
<form action="/token/new.json" method="POST"> | |
<div data-role="fieldcontain"> | |
<label for="email">Email:</label><input type="text" name="username" id="email" value="" /> | |
</div> | |
<div data-role="fieldcontain"> | |
<label for="password">Password:</label><input type="password" name="password" id="password" value="" /> | |
</div> | |
<button type="submit" name="submit" value="submit-value">Sign in</button> | |
</form> | |
<body> | |
<form action="/signup/" method="POST"> | |
<div data-role="fieldcontain"> | |
<label for="email">Email:</label><input type="text" name="username" id="email" value="" /> | |
</div> | |
<div data-role="fieldcontain"> | |
<label for="password">Password:</label><input type="password" name="password" id="password" value="" /> | |
</div> | |
<button type="submit" name="submit" value="submit-value">Sign up</button> | |
</form> | |
</body> | |
</html> |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
{{user}} | |
{% if user.is_authenticated %} | |
<p>Welcome, {{ user.username }}. Thanks for logging in.</p> | |
{% else %} | |
<p>Welcome, new user. Please log in.</p> | |
{% endif %} | |
I'm secure. | |
<a href = "/signout"><button src="signout">Sign out</button></a> | |
</body> | |
</html> |
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
from django.test import TestCase | |
# Create your tests here. |
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
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
from django.conf.urls import url | |
__author__ = 'yinzishao' | |
urlpatterns = [ | |
url(r'^login/','auth_token.views.loginview',name='loginview'), | |
url(r'^signup/','auth_token.views.signup',name='signup'), | |
url(r'^auth/','auth_token.views.auth',name='auth') | |
# url(r'^token/new.json$', token_new, name='api_token_new'), | |
# url(r'^token/(?P<token>.{24})/(?P<user>\d+).json$', token, name='api_token'), | |
] |
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 json | |
from django.contrib.auth import authenticate | |
from django.contrib.auth.views import login | |
from django.shortcuts import render, render_to_response,HttpResponse, redirect | |
from django.contrib.auth.models import User | |
# Create your views here. | |
from django.template.context_processors import csrf | |
from tokenapi.views import token_new | |
from tokenapi.decorators import token_required | |
# @token_required | |
def loginview(request): | |
# c = {"yin":"yin"} | |
# c.update(csrf(request)) | |
basic_auth = request.META.get('HTTP_AUTHORIZATION') | |
print basic_auth | |
return render(request,'login.html') | |
# return render_to_response('login.html', c,) | |
def auth(request): | |
result = token_new(request) | |
return result | |
def signup(request): | |
data={} | |
if request.method == "POST": | |
username = request.POST.get('username',None) | |
password = request.POST.get('password',None) | |
if not (username and password): | |
print request.body | |
request_data= json.loads(request.body) | |
print request_data["username"] | |
print request_data["password"] | |
print request_data | |
return HttpResponse(request.body,content_type="application/json") | |
# print request_data | |
print username,password | |
data['username']=username | |
data['password']=password | |
return HttpResponse(json.dumps(data),content_type="application/json") | |
if request.method == "GET": | |
print request.read() | |
return HttpResponse(json.dumps({"name":"yinzishao"}),content_type="application/json") | |
return HttpResponse("No post") | |
# | |
# | |
# def auth_and_login(request, onsuccess='/secure', onfail='/login/'): | |
# user = authenticate(username=request.POST['email'], password=request.POST['password']) | |
# if user is not None and user.is_active: | |
# login(request, user) | |
# request.session['name'] = "yin" | |
# return redirect(onsuccess) | |
# else: | |
# return redirect(onfail) | |
# | |
# def create_user(username, email, password): | |
# user = User(username=username, email=email) | |
# user.set_password(password) | |
# user.save() | |
# return user | |
# | |
# def user_exists(username): | |
# user_count = User.objects.filter(username=username).count() | |
# if user_count == 0: | |
# return False | |
# return True | |
# | |
# def sign_up_in(request): | |
# post = request.POST | |
# if not user_exists(post['email']): | |
# user = create_user(username=post['email'], email=post['email'], password=post['password']) | |
# return auth_and_login(request) | |
# else: | |
# return redirect("/login/") |
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
SQLite format 3 @ $ * -� � ����� " |