-
Notifications
You must be signed in to change notification settings - Fork 52
Expose mechanisms in the high-level API #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import six | ||
|
||
from gssapi.raw import oids as roids | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💪 |
||
from gssapi._utils import import_gssapi_extension | ||
from gssapi.raw import misc as rmisc | ||
from gssapi import _utils | ||
|
||
rfc5587 = import_gssapi_extension('rfc5587') | ||
rfc5801 = import_gssapi_extension('rfc5801') | ||
|
||
|
||
class Mechanism(roids.OID): | ||
""" | ||
A GSSAPI Mechanism | ||
|
||
This class represents a mechanism and centralizes functions dealing with | ||
mechanisms and can be used with any calls. | ||
|
||
It inherits from the low-level GSSAPI :class:`~gssapi.raw.oids.OID` class, | ||
and thus can be used with both low-level and high-level API calls. | ||
""" | ||
def __new__(cls, cpy=None, elements=None): | ||
return super(Mechanism, cls).__new__(cls, cpy, elements) | ||
|
||
@property | ||
def name_types(self): | ||
""" | ||
Get the set of name types supported by this mechanism. | ||
""" | ||
return rmisc.inquire_names_for_mech(self) | ||
|
||
@property | ||
def _saslname(self): | ||
if rfc5801 is None: | ||
raise NotImplementedError("Your GSSAPI implementation does not " | ||
"have support for RFC 5801") | ||
return rfc5801.inquire_saslname_for_mech(self) | ||
|
||
@property | ||
def _attrs(self): | ||
if rfc5587 is None: | ||
raise NotImplementedError("Your GSSAPI implementation does not " | ||
"have support for RFC 5587") | ||
|
||
return rfc5587.inquire_attrs_for_mech(self) | ||
|
||
def __str__(self): | ||
if issubclass(str, six.text_type): | ||
# Python 3 -- we should return unicode | ||
return self._bytes_desc().decode(_utils._get_encoding()) | ||
else: | ||
return self._bytes_desc() | ||
|
||
def __unicode__(self): | ||
return self._bytes_desc().decode(_utils._get_encoding()) | ||
|
||
def _bytes_desc(self): | ||
base = self.dotted_form | ||
if rfc5801 is not None: | ||
base = self._saslname.mech_name | ||
|
||
if isinstance(base, six.text_type): | ||
base = base.encode(_utils._get_encoding()) | ||
|
||
return base | ||
|
||
def __repr__(self): | ||
""" | ||
Get a name representing the mechanism; always safe to call | ||
""" | ||
base = "<Mechanism (%s)>" % self.dotted_form | ||
if rfc5801 is not None: | ||
base = "<Mechanism %s (%s)>" % ( | ||
self._saslname.mech_name.decode('UTF-8'), | ||
self.dotted_form | ||
) | ||
|
||
return base | ||
|
||
@property | ||
def sasl_name(self): | ||
""" | ||
Get the SASL name for the mechanism | ||
|
||
:requires-ext:`rfc5801` | ||
""" | ||
return self._saslname.sasl_mech_name.decode('UTF-8') | ||
|
||
@property | ||
def description(self): | ||
""" | ||
Get the description of the mechanism | ||
|
||
:requires-ext:`rfc5801` | ||
""" | ||
return self._saslname.mech_description.decode('UTF-8') | ||
|
||
@property | ||
def known_attrs(self): | ||
""" | ||
Get the known attributes of the mechanism; returns a set of OIDs | ||
([OID]) | ||
|
||
:requires-ext:`rfc5587` | ||
""" | ||
return self._attrs.known_mech_attrs | ||
|
||
@property | ||
def attrs(self): | ||
""" | ||
Get the attributes of the mechanism; returns a set of OIDs ([OID]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving this open as I'm not sure if this is the desired format. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per IRC discussion with @DirectXMan12 this appears to be in the correct format. |
||
|
||
:requires-ext:`rfc5587` | ||
""" | ||
return self._attrs.mech_attrs | ||
|
||
@classmethod | ||
def all_mechs(cls): | ||
""" | ||
Get a generator of all mechanisms supported by GSSAPI | ||
""" | ||
return (cls(mech) for mech in rmisc.indicate_mechs()) | ||
|
||
@classmethod | ||
def from_name(cls, name=None): | ||
""" | ||
Get a generator of mechanisms that may be able to process the name | ||
|
||
Args: | ||
name (Name): a name to inquire about | ||
|
||
Returns: | ||
[Mechanism]: a set of mechanisms which support this name | ||
|
||
Raises: | ||
GSSError | ||
""" | ||
return (cls(mech) for mech in rmisc.inquire_mechs_for_name(name)) | ||
|
||
@classmethod | ||
def from_sasl_name(cls, name=None): | ||
""" | ||
Create a Mechanism from its SASL name | ||
|
||
Args: | ||
name (str): SASL name of the desired mechanism | ||
|
||
Returns: | ||
Mechanism: the desired mechanism | ||
|
||
Raises: | ||
GSSError | ||
|
||
:requires-ext:`rfc5801` | ||
""" | ||
if rfc5801 is None: | ||
raise NotImplementedError("Your GSSAPI implementation does not " | ||
"have support for RFC 5801") | ||
if isinstance(name, six.text_type): | ||
name = name.encode(_utils._get_encoding()) | ||
|
||
m = rfc5801.inquire_mech_for_saslname(name) | ||
|
||
return cls(m) | ||
|
||
@classmethod | ||
def from_attrs(cls, m_desired=None, m_except=None, m_critical=None): | ||
""" | ||
Get a generator of mechanisms supporting the specified attributes. See | ||
RFC 5587's :func:`indicate_mechs_by_attrs` for more information. | ||
|
||
Args: | ||
m_desired ([OID]): Desired attributes | ||
m_except ([OID]): Except attributes | ||
m_critical ([OID]): Critical attributes | ||
|
||
Returns: | ||
[Mechanism]: A set of mechanisms having the desired features. | ||
|
||
Raises: | ||
GSSError | ||
|
||
:requires-ext:`rfc5587` | ||
""" | ||
if isinstance(m_desired, roids.OID): | ||
m_desired = set([m_desired]) | ||
if isinstance(m_except, roids.OID): | ||
m_except = set([m_except]) | ||
if isinstance(m_critical, roids.OID): | ||
m_critical = set([m_critical]) | ||
|
||
if rfc5587 is None: | ||
raise NotImplementedError("Your GSSAPI implementation does not " | ||
"have support for RFC 5587") | ||
|
||
mechs = rfc5587.indicate_mechs_by_attrs(m_desired, | ||
m_except, | ||
m_critical) | ||
return (cls(mech) for mech in mechs) |