Browse Source

[liboauth] Add a mapping to shorten domain names in usernames

Dominik Gehl 11 years ago
parent
commit
403cd90

+ 6 - 0
desktop/conf.dist/hue.ini

@@ -478,6 +478,12 @@
   ## authenticate_url_facebook=https://graph.facebook.com/me?access_token=
   ## authenticate_url_linkedin=https://api.linkedin.com/v1/people/~:(email-address)?format=json&oauth2_access_token=
 
+  # Username Map. Json Hash format.
+  # Replaces username parts in order to simplify usernames obtained
+  # Example: {"@sub1.domain.com":"_S1", "@sub2.domain.com":"_S2"}
+  # converts 'email@sub1.domain.com' to 'email_S1'
+  ## username_map={}
+
   # Whitelisted domains (only applies to Google OAuth). CSV format.
   ## whitelisted_domains_google=
 

+ 6 - 0
desktop/conf/pseudo-distributed.ini.tmpl

@@ -483,6 +483,12 @@
   ## authenticate_url_facebook=https://graph.facebook.com/me?access_token=
   ## authenticate_url_linkedin=https://api.linkedin.com/v1/people/~:(email-address)?format=json&oauth2_access_token=
 
+  # Username Map. Json Hash format.
+  # Replaces username parts in order to simplify usernames obtained
+  # Example: {"@sub1.domain.com":"_S1", "@sub2.domain.com":"_S2"}
+  # converts 'email@sub1.domain.com' to 'email_S1'
+  ## username_map={}
+
   # Whitelisted domains (only applies to Google OAuth). CSV format.
   ## whitelisted_domains_google=
 

+ 9 - 3
desktop/libs/liboauth/src/liboauth/backend.py

@@ -157,7 +157,7 @@ class OAuthBackend(DesktopBackendBase):
             if resp['status'] != '200':
                 raise Exception(_("Invalid response from OAuth provider: %s") % resp)
             username=(json.loads(content))["email"]
-            access_token = dict(screen_name=''.join([x for x in username if x.isalnum()]), oauth_token_secret=access_tok)
+            access_token = dict(screen_name=map_username(username), oauth_token_secret=access_tok)
             whitelisted_domains = liboauth.conf.WHITELISTED_DOMAINS_GOOGLE.get()
             if whitelisted_domains:
                 if username.split('@')[1] not in whitelisted_domains:
@@ -170,7 +170,7 @@ class OAuthBackend(DesktopBackendBase):
             if resp['status'] != '200':
                 raise Exception(_("Invalid response from OAuth provider: %s") % resp)
             username = (json.loads(content))["email"]
-            access_token = dict(screen_name=''.join([x for x in username if x.isalnum()]), oauth_token_secret=access_tok)
+            access_token = dict(screen_name=map_username(username), oauth_token_secret=access_tok)
         #linkedin
         elif social == 'linkedin':
             access_tok = (json.loads(cont))['access_token']
@@ -179,7 +179,7 @@ class OAuthBackend(DesktopBackendBase):
             if resp['status'] != '200':
                 raise Exception(_("Invalid response from OAuth provider: %s") % resp)
             username = (json.loads(content))['emailAddress']
-            access_token = dict(screen_name=''.join([x for x in username if x.isalnum()]), oauth_token_secret=access_tok)
+            access_token = dict(screen_name=map_username(username), oauth_token_secret=access_tok)
   
 
     return access_token
@@ -259,6 +259,12 @@ class OAuthBackend(DesktopBackendBase):
        )
     return url
 
+def map_username(username):
+    username_map = liboauth.conf.USERNAME_MAP.get()
+    if username_map:
+        for key, value in username_map.iteritems():
+            username = username.replace(key, value)
+    return ''.join([x for x in username if x.isalnum()])
 
 def find_or_create_user(username, password=None):
   try:

+ 11 - 1
desktop/libs/liboauth/src/liboauth/conf.py

@@ -19,7 +19,7 @@ import os
 
 from django.utils.translation import ugettext_lazy as _t, ugettext as _
 
-from desktop.lib.conf import Config, coerce_bool, coerce_csv
+from desktop.lib.conf import Config, coerce_bool, coerce_csv, coerce_json_dict
 
 
 CONSUMER_KEY_TWITTER = Config(
@@ -155,3 +155,13 @@ WHITELISTED_DOMAINS_GOOGLE = Config(
     type=coerce_csv,
     default=''
 )
+
+USERNAME_MAP = Config(
+    key="username_map",
+    help=_t('JSON formatted hash of username simplifications. '
+        'Example: {"@sub1.domain.com":"_S1", "@sub2.domain.com":"_S2"} '
+        'converts "email@sub1.domain.com" to "email_S1"'),
+    type=coerce_json_dict,
+    default='{}'
+)
+