|
| 1 | +"""Class for implementing a basic access authenticator for SparkMagic""" |
| 2 | + |
| 3 | +from sparkmagic.livyclientlib.exceptions import BadUserDataException |
| 4 | +from hdijupyterutils.ipywidgetfactory import IpyWidgetFactory |
| 5 | +from requests.auth import HTTPBasicAuth |
| 6 | +from .customauth import Authenticator |
| 7 | + |
| 8 | +class Basic(HTTPBasicAuth, Authenticator): |
| 9 | + """Basic Access authenticator for SparkMagic""" |
| 10 | + def __init__(self, parsed_attributes=None): |
| 11 | + """Initializes the Authenticator with the attributes in the attributes |
| 12 | + parsed from a %spark magic command if applicable, or with default values |
| 13 | + otherwise. |
| 14 | +
|
| 15 | + Args: |
| 16 | + self, |
| 17 | + parsed_attributes (IPython.core.magics.namespace): The namespace object that |
| 18 | + is created from parsing %spark magic command. |
| 19 | + """ |
| 20 | + if parsed_attributes is not None: |
| 21 | + if parsed_attributes.user is '' or parsed_attributes.password is '': |
| 22 | + new_exc = BadUserDataException("Need to supply username and password arguments for "\ |
| 23 | + "Basic Access Authentication. (e.g. -a username -p password).") |
| 24 | + raise new_exc |
| 25 | + self.username = parsed_attributes.user |
| 26 | + self.password = parsed_attributes.password |
| 27 | + else: |
| 28 | + self.username = 'username' |
| 29 | + self.password = 'password' |
| 30 | + HTTPBasicAuth.__init__(self, self.username, self.password) |
| 31 | + Authenticator.__init__(self, parsed_attributes) |
| 32 | + |
| 33 | + def get_widgets(self, widget_width): |
| 34 | + """Creates and returns a list with an address, username, and password widget |
| 35 | +
|
| 36 | + Args: |
| 37 | + widget_width (str): The width of all widgets to be created. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + Sequence[hdijupyterutils.ipywidgetfactory.IpyWidgetFactory]: list of widgets |
| 41 | + """ |
| 42 | + ipywidget_factory = IpyWidgetFactory() |
| 43 | + |
| 44 | + self.user_widget = ipywidget_factory.get_text( |
| 45 | + description='Username:', |
| 46 | + value=self.username, |
| 47 | + width=widget_width |
| 48 | + ) |
| 49 | + |
| 50 | + self.password_widget = ipywidget_factory.get_text( |
| 51 | + description='Password:', |
| 52 | + value=self.password, |
| 53 | + width=widget_width |
| 54 | + ) |
| 55 | + |
| 56 | + widgets = [self.user_widget, self.password_widget] |
| 57 | + return Authenticator.get_widgets(self, widget_width) + widgets |
| 58 | + |
| 59 | + def update_with_widget_values(self): |
| 60 | + """Updates url, username, and password to be the value of their respective widgets.""" |
| 61 | + Authenticator.update_with_widget_values(self) |
| 62 | + self.username = self.user_widget.value |
| 63 | + self.password = self.password_widget.value |
| 64 | + |
| 65 | + def __eq__(self, other): |
| 66 | + if not isinstance(other, Basic): |
| 67 | + return False |
| 68 | + return self.url == other.url and self.username == other.username and \ |
| 69 | + self.password == other.password |
| 70 | + |
| 71 | + def __call__(self, request): |
| 72 | + return HTTPBasicAuth.__call__(self, request) |
| 73 | + |
| 74 | + def __hash__(self): |
| 75 | + return hash((self.username, self.password, self.url, self.__class__.__name__)) |
0 commit comments