Skip to content
Snippets Groups Projects
Commit 1aeec1c4 authored by Ben Adida's avatar Ben Adida
Browse files

made login with clever work

parent 2b57d3c9
No related branches found
No related tags found
No related merge requests found
......@@ -7,11 +7,11 @@ from django.http import *
from django.core.mail import send_mail
from django.conf import settings
import httplib2,json
import httplib2,json,base64
import sys, os, cgi, urllib, urllib2, re
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.client import OAuth2WebServerFlow, OAuth2Credentials
# some parameters to indicate that status updating is not possible
STATUS_UPDATES = False
......@@ -25,7 +25,8 @@ def get_flow(redirect_url=None):
client_secret=settings.CLEVER_CLIENT_SECRET,
scope='read:students read:teachers read:user_id read:sis',
auth_uri="https://clever.com/oauth/authorize",
token_uri="https://clever.com/oauth/tokens",
#token_uri="https://clever.com/oauth/tokens",
token_uri="http://requestb.in/1b18gwf1",
redirect_uri=redirect_url)
def get_auth_url(request, redirect_url):
......@@ -35,25 +36,48 @@ def get_auth_url(request, redirect_url):
return flow.step1_get_authorize_url()
def get_user_info_after_auth(request):
flow = get_flow(request.session['clever-redirect-url'])
redirect_uri = request.session['clever-redirect-url']
del request.session['clever-redirect-url']
flow = get_flow(redirect_uri)
code = request.GET['code']
credentials = flow.step2_exchange(code)
# at this stage, just an access token
# do the POST manually, because OAuth2WebFlow can't do auth header for token exchange
http = httplib2.Http(".cache")
auth_header = "Basic %s" % base64.b64encode(settings.CLEVER_CLIENT_ID + ":" + settings.CLEVER_CLIENT_SECRET)
resp_headers, content = http.request("https://clever.com/oauth/tokens", "POST", urllib.urlencode({
"code" : code,
"grant_type": "authorization_code",
"redirect_uri": redirect_uri
}), headers = {
'Authorization': auth_header,
'Content-Type': "application/x-www-form-urlencoded"
})
token_response = json.loads(content)
access_token = token_response['access_token']
# package the credentials
credentials = OAuth2Credentials(access_token, settings.CLEVER_CLIENT_ID, settings.CLEVER_CLIENT_SECRET, None, None, None, None)
# get the nice name
http = httplib2.Http(".cache")
http = credentials.authorize(http)
(resp_headers, content) = http.request("https://api.clever.com/me", "GET")
# {"type":"student","data":{"id":"563395179f7408755c0006b7","district":"5633941748c07c0100000aac","type":"student","created":"2015-10-30T16:04:39.262Z","credentials":{"district_password":"eel7Thohd","district_username":"dianes10"},"dob":"1998-11-01T00:00:00.000Z","ell_status":"Y","email":"diane.s@example.org","gender":"F","grade":"9","hispanic_ethnicity":"Y","last_modified":"2015-10-30T16:04:39.274Z","location":{"zip":"11433"},"name":{"first":"Diane","last":"Schmeler","middle":"J"},"race":"Asian","school":"5633950c62fc41c041000005","sis_id":"738733110","state_id":"114327752","student_number":"738733110"},"links":[{"rel":"self","uri":"/me"},{"rel":"canonical","uri":"/v1.1/students/563395179f7408755c0006b7"},{"rel":"district","uri":"/v1.1/districts/5633941748c07c0100000aac"}]}
response = json.loads(content)
user_id = response['data']['id']
user_name = "%s %s" % (response['data']['name']['first'], response['data']['name']['last'])
user_type = response['type']
user_district = response['data']['district']
print content
# watch out, response also contains email addresses, but not sure whether thsoe are verified or not
# so for email address we will only look at the id_token
return {'type' : 'clever', 'user_id': response["data"]["id"], 'name': "" , 'info': {"district": response["data"]["district"], "type": response["data"]["type"]}, 'token':{}}
return {'type' : 'clever', 'user_id': user_id, 'name': user_name , 'info': {"district": user_district, "type": user_type}, 'token': {'access_token': access_token}}
def do_logout(user):
"""
......
helios_auth/media/login-icons/clever.png

33.7 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment