configurationengine/source/cone/storage/authenticate.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    36     current version of login page inside <noscript> before other stuff on page.
    36     current version of login page inside <noscript> before other stuff on page.
    37     Asks form inputs of types text and password from the user.
    37     Asks form inputs of types text and password from the user.
    38     The data is saved in varables inside class
    38     The data is saved in varables inside class
    39     """
    39     """
    40     def __init__(self, *argv, **kwargs):
    40     def __init__(self, *argv, **kwargs):
       
    41         self.username_func = kwargs.pop('username_func',None)
       
    42         self.password_func = kwargs.pop('password_func',None)
    41         HTMLParser.__init__(self, *argv, **kwargs)
    43         HTMLParser.__init__(self, *argv, **kwargs)
    42         self.html_end = False
    44         self.html_end = False
    43         self.httpdata = {}
    45         self.httpdata = {}
    44         self.input_requested = False
    46         self.input_requested = False
    45         self.input_entered = False
    47         self.input_entered = False
    46         self.method = ''
    48         self.method = ''
    47         self.action = ''
    49         self.action = ''
    48         self.username = kwargs.get('username')
       
    49         self.password = kwargs.get('password')
       
    50         
    50         
    51     def handle_starttag(self, tag, attrs):
    51     def handle_starttag(self, tag, attrs):
    52         attrs = dict(attrs)
    52         attrs = dict(attrs)
    53         if self.html_end:
    53         if self.html_end:
    54             return
    54             return
    62             if inputtype == 'hidden':
    62             if inputtype == 'hidden':
    63                 # Should the username be also overridable
    63                 # Should the username be also overridable
    64                 self.httpdata[attrs.get('name')] = attrs.get('value')
    64                 self.httpdata[attrs.get('name')] = attrs.get('value')
    65             if inputtype == 'password':
    65             if inputtype == 'password':
    66                 self.input_requested = True
    66                 self.input_requested = True
    67                 data = self.password
    67                 data = self.password_func()
    68                 if data:
    68                 if data:
    69                     self.input_entered = True
    69                     self.input_entered = True
    70                 self.httpdata[attrs.get('name')] = data
    70                 self.httpdata[attrs.get('name')] = data
    71             if inputtype == 'text':
    71             if inputtype == 'text':
    72                 self.input_requested = True
    72                 self.input_requested = True
    73                 data = raw_input()
    73                 data = self.username_func()
    74                 if data:
    74                 if data:
    75                     self.input_entered = True
    75                     self.input_entered = True
    76                 self.httpdata[attrs.get('name')] = data
    76                 self.httpdata[attrs.get('name')] = data
    77             if inputtype == 'submit':
    77             if inputtype == 'submit':
    78                 self.httpdata['submit'] = attrs.get('value')
    78                 self.httpdata['submit'] = attrs.get('value')
    93         if data.strip():
    93         if data.strip():
    94             print data.strip(),
    94             print data.strip(),
    95 
    95 
    96 class CarbonAuthHandler(urllib2.AbstractHTTPHandler):
    96 class CarbonAuthHandler(urllib2.AbstractHTTPHandler):
    97     handler_order = 600
    97     handler_order = 600
       
    98     def __init__(self):
       
    99         urllib2.AbstractHTTPHandler.__init__(self)
       
   100         self.auth_count = 0
       
   101         self.auth_max = 5
       
   102         self.username = ""
       
   103         self.password = ""
       
   104         self.username_func = None
       
   105         self.password_func = None
       
   106         
    98     
   107     
    99     def add_password(self, username, password):
   108     def add_username_func(self, username_func):
   100         """
   109         """
   101         Add username and password
   110         Add password getting function
   102         """
   111         """
   103         self.username = username
   112         self.username_func = username_func
   104         self.password = password
   113 
       
   114     def add_password_func(self, password_func):
       
   115         """
       
   116         Add password getting function
       
   117         """
       
   118         self.password_func = password_func
       
   119 
       
   120     def get_username(self):
       
   121         """
       
   122         Add password getting function
       
   123         """
       
   124         if self.auth_count == 0  and self.username_func:            
       
   125             return self.username_func()
       
   126         else:
       
   127             self.username = raw_input("Username: ")
       
   128             return self.username 
       
   129             
       
   130     def get_password(self):
       
   131         """
       
   132         Add password getting function
       
   133         """
       
   134         if self.auth_count == 0 and self.password_func:            
       
   135             return self.password_func()
       
   136         else:
       
   137             self.password = getpass.getpass()
       
   138             return self.password
   105 
   139 
   106     def https_response(self, request, response):
   140     def https_response(self, request, response):
   107         """
   141         """
   108         Catches responses which are from sso login page and asks for the
   142         Catches responses which are from sso login page and asks for the
   109         information from the command line and posts it.
   143         information from the command line and posts it.
   110         After posting urllib2 takes care of following redirects back to
   144         After posting urllib2 takes care of following redirects back to
   111         original page.
   145         original page.
   112         """
   146         """
   113         if (re.match('login.*\.europe\.nokia\.com', request.get_host())):
   147         if (re.match('login.*\.europe\.nokia\.com', request.get_host())):
   114             sso_parser = SSOHTMLParser(username=self.username, password=self.password)
   148             if self.auth_count > self.auth_max:
       
   149                 print "Authentication failed!"
       
   150                 return response
       
   151             sso_parser = SSOHTMLParser(username_func=self.get_username, password_func=self.get_password)
   115             sso_parser.feed(response.read())
   152             sso_parser.feed(response.read())
       
   153             self.auth_count += 1
       
   154             
   116             # !sso_parser.input_requested when we have posted the form and
   155             # !sso_parser.input_requested when we have posted the form and
   117             # are reading the redirect back. We don't want to handle that
   156             # are reading the redirect back. We don't want to handle that
   118             if sso_parser.input_requested:
   157             if sso_parser.input_requested:
   119                 if not sso_parser.input_entered:
   158                 if not sso_parser.input_entered:
   120                     # By entering empty username and password you get
   159                     # By entering empty username and password you get
   138         or asks for the information from the command line and posts it.
   177         or asks for the information from the command line and posts it.
   139         After posting urllib2 takes care of following redirects back to
   178         After posting urllib2 takes care of following redirects back to
   140         original page.
   179         original page.
   141         """    
   180         """    
   142         if response.code == 200 and (re.match('.*/extauth/login/?.*', request.get_full_url())):
   181         if response.code == 200 and (re.match('.*/extauth/login/?.*', request.get_full_url())):
       
   182             if self.auth_count > self.auth_max:
       
   183                 raise urllib2.HTTPError("Authentication failed!")
       
   184             
   143             loginreq = urllib2.Request(request.get_full_url(),
   185             loginreq = urllib2.Request(request.get_full_url(),
   144                                      urllib.urlencode({ 'username' : self.username, 
   186                                      urllib.urlencode({ 'username' : self.get_username(), 
   145                                                         'password' : self.password,
   187                                                         'password' : self.get_password(),
   146                                                         'submit' : 'login'}
   188                                                         'submit' : 'login'}
   147                                                       ),
   189                                                       ),
   148                                      origin_req_host=request.get_origin_req_host(),
   190                                      origin_req_host=request.get_origin_req_host(),
   149                                      unverifiable=True,
   191                                      unverifiable=True,
   150                                      )
   192                                      )
       
   193             self.auth_count += 1
   151             return self.parent.open(loginreq)
   194             return self.parent.open(loginreq)
   152         else:            
   195         else:            
   153             return response
   196             return response