Last active
August 29, 2015 14:15
-
-
Save narenaryan/5c2eb80c40665a85a18e to your computer and use it in GitHub Desktop.
Freshdesk SSO remote login django view sample script
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
# Create your views here. | |
from django.shortcuts import render,redirect | |
from django.contrib.auth import authenticate | |
from datetime import datetime | |
import hmac | |
def ticket_login(request): | |
if request.method == 'GET': | |
return render(request, 'ticketing/login.html') | |
elif request.method == 'POST': | |
res = request.POST | |
#Fetch username password from remote login page and Check authenticity | |
user = authenticate(username=res['user'], password=res['pwd']) | |
if user: | |
#Prepare a hmac hasher with your secret shared key | |
digest_maker = hmac.new('68429c66b55eea0e70123xxxxxxxxx') | |
# Redirection URL. Fill elements later | |
url = "http://nest.freshdesk.com/login/sso?name={0}&email={1}×tamp={2}&hash={3}" | |
#Update hasher with username,email and UTC timestamp | |
digest_maker.update(user.username) | |
digest_maker.update(user.email) | |
#Calculating epoch in seconds Ex:1424237345.831716 seconds | |
td = datetime.utcnow()-datetime(1970,1,1) | |
ts = str(int(td.total_seconds())) | |
digest_maker.update(ts) | |
#Caluclate hexdigest of hash | |
digest = digest_maker.hexdigest() | |
#Rediect to login page of your freshdesk account | |
return redirect(url.format(user.username,user.email,ts,digest)) | |
else: | |
return redirect(ticket_login) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment