[Python-modules-commits] [python-social-auth] 302/322: added get_auth_token method for ease of use

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Dec 24 15:13:22 UTC 2016


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to tag v0.2.10
in repository python-social-auth.

commit 36051d6df1a6ba6e80b4623540cec3c9d30f0371
Author: vinhub <vin.bhalerao at gmail.com>
Date:   Wed May 13 10:05:06 2015 -0700

    added get_auth_token method for ease of use
---
 social/backends/azuread.py | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/social/backends/azuread.py b/social/backends/azuread.py
index 1311139..42ead2d 100644
--- a/social/backends/azuread.py
+++ b/social/backends/azuread.py
@@ -10,6 +10,7 @@ from social.exceptions import AuthException, AuthFailed, AuthCanceled, \
 from jwt import DecodeError, ExpiredSignature, decode as jwt_decode
 from social.backends.oauth import BaseOAuth2
 import requests
+import time
 import urllib
 
 class AzureADOAuth2(BaseOAuth2):
@@ -25,6 +26,8 @@ class AzureADOAuth2(BaseOAuth2):
         ('id_token', 'id_token'),
         ('refresh_token', 'refresh_token'),
         ('expires_in', 'expires'),
+        ('expires_on', 'expires_on'),
+        ('not_before', 'not_before'),
         ('given_name', 'first_name'),
         ('family_name', 'last_name'),
         ('token_type', 'token_type')
@@ -85,11 +88,17 @@ class AzureADOAuth2(BaseOAuth2):
             'resource': self.setting('RESOURCE')
         }
 
-    def get_auth_token(self, token):
-        response = requests.get('https://graph.windows.net/me', headers={'Authorization': 'Bearer ' + token})
+    def get_auth_token(self, user_id):
+        """Return the access token for the given user, after ensuring that it has not expired, 
+        or refreshing it if so."""
+        user = self.get_user(user_id=user_id)
 
-        if response.status_code == 401:
-            new_token_response = self.refresh_token(token)
-            token = new_token_response['access_token']
+        access_token = user.social_user.access_token
+        expires_on = user.social_user.extra_data['expires_on']
+
+        if expires_on <= int(time.time()):
+            new_token_response = self.refresh_token(token=access_token)
+            access_token = new_token_response['access_token']
+
+        return access_token
 
-        return token

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-social-auth.git



More information about the Python-modules-commits mailing list