|
| 1 | +import six |
| 2 | + |
| 3 | +from gssapi.raw import oids as roids |
| 4 | +from gssapi._utils import import_gssapi_extension |
| 5 | +from gssapi.raw import misc as rmisc |
| 6 | +from gssapi import _utils |
| 7 | + |
| 8 | +rfc5587 = import_gssapi_extension('rfc5587') |
| 9 | +rfc5801 = import_gssapi_extension('rfc5801') |
| 10 | + |
| 11 | + |
| 12 | +class Mechanism(roids.OID): |
| 13 | + """ |
| 14 | + A GSSAPI Mechanism |
| 15 | +
|
| 16 | + This class represents a mechanism and centralizes functions dealing with |
| 17 | + mechanisms and can be used with any calls. |
| 18 | +
|
| 19 | + It inherits from the low-level GSSAPI :class:`~gssapi.raw.oids.OID` class, |
| 20 | + and thus can be used with both low-level and high-level API calls. |
| 21 | + """ |
| 22 | + def __new__(cls, cpy=None, elements=None): |
| 23 | + return super(Mechanism, cls).__new__(cls, cpy, elements) |
| 24 | + |
| 25 | + @property |
| 26 | + def name_types(self): |
| 27 | + """ |
| 28 | + Get the set of name types supported by this mechanism. |
| 29 | + """ |
| 30 | + return rmisc.inquire_names_for_mech(self) |
| 31 | + |
| 32 | + @property |
| 33 | + def _saslname(self): |
| 34 | + if rfc5801 is None: |
| 35 | + raise NotImplementedError("Your GSSAPI implementation does not " |
| 36 | + "have support for RFC 5801") |
| 37 | + return rfc5801.inquire_saslname_for_mech(self) |
| 38 | + |
| 39 | + @property |
| 40 | + def _attrs(self): |
| 41 | + if rfc5587 is None: |
| 42 | + raise NotImplementedError("Your GSSAPI implementation does not " |
| 43 | + "have support for RFC 5587") |
| 44 | + |
| 45 | + return rfc5587.inquire_attrs_for_mech(self) |
| 46 | + |
| 47 | + def __str__(self): |
| 48 | + if issubclass(str, six.text_type): |
| 49 | + # Python 3 -- we should return unicode |
| 50 | + return self._bytes_desc().decode(_utils._get_encoding()) |
| 51 | + else: |
| 52 | + return self._bytes_desc() |
| 53 | + |
| 54 | + def __unicode__(self): |
| 55 | + return self._bytes_desc().decode(_utils._get_encoding()) |
| 56 | + |
| 57 | + def _bytes_desc(self): |
| 58 | + base = self.dotted_form |
| 59 | + if rfc5801 is not None: |
| 60 | + base = self._saslname.mech_name |
| 61 | + |
| 62 | + if isinstance(base, six.text_type): |
| 63 | + base = base.encode(_utils._get_encoding()) |
| 64 | + |
| 65 | + return base |
| 66 | + |
| 67 | + def __repr__(self): |
| 68 | + """ |
| 69 | + Get a name representing the mechanism; always safe to call |
| 70 | + """ |
| 71 | + base = "<Mechanism (%s)>" % self.dotted_form |
| 72 | + if rfc5801 is not None: |
| 73 | + base = "<Mechanism %s (%s)>" % ( |
| 74 | + self._saslname.mech_name.decode('UTF-8'), |
| 75 | + self.dotted_form |
| 76 | + ) |
| 77 | + |
| 78 | + return base |
| 79 | + |
| 80 | + @property |
| 81 | + def sasl_name(self): |
| 82 | + """ |
| 83 | + Get the SASL name for the mechanism |
| 84 | +
|
| 85 | + :requires-ext:`rfc5801` |
| 86 | + """ |
| 87 | + return self._saslname.sasl_mech_name.decode('UTF-8') |
| 88 | + |
| 89 | + @property |
| 90 | + def description(self): |
| 91 | + """ |
| 92 | + Get the description of the mechanism |
| 93 | +
|
| 94 | + :requires-ext:`rfc5801` |
| 95 | + """ |
| 96 | + return self._saslname.mech_description.decode('UTF-8') |
| 97 | + |
| 98 | + @property |
| 99 | + def known_attrs(self): |
| 100 | + """ |
| 101 | + Get the known attributes of the mechanism; returns a set of OIDs |
| 102 | + ([OID]) |
| 103 | +
|
| 104 | + :requires-ext:`rfc5587` |
| 105 | + """ |
| 106 | + return self._attrs.known_mech_attrs |
| 107 | + |
| 108 | + @property |
| 109 | + def attrs(self): |
| 110 | + """ |
| 111 | + Get the attributes of the mechanism; returns a set of OIDs ([OID]) |
| 112 | +
|
| 113 | + :requires-ext:`rfc5587` |
| 114 | + """ |
| 115 | + return self._attrs.mech_attrs |
| 116 | + |
| 117 | + @classmethod |
| 118 | + def all_mechs(cls): |
| 119 | + """ |
| 120 | + Get a generator of all mechanisms supported by GSSAPI |
| 121 | + """ |
| 122 | + return (cls(mech) for mech in rmisc.indicate_mechs()) |
| 123 | + |
| 124 | + @classmethod |
| 125 | + def from_name(cls, name=None): |
| 126 | + """ |
| 127 | + Get a generator of mechanisms that may be able to process the name |
| 128 | +
|
| 129 | + Args: |
| 130 | + name (Name): a name to inquire about |
| 131 | +
|
| 132 | + Returns: |
| 133 | + [Mechanism]: a set of mechanisms which support this name |
| 134 | +
|
| 135 | + Raises: |
| 136 | + GSSError |
| 137 | + """ |
| 138 | + return (cls(mech) for mech in rmisc.inquire_mechs_for_name(name)) |
| 139 | + |
| 140 | + @classmethod |
| 141 | + def from_sasl_name(cls, name=None): |
| 142 | + """ |
| 143 | + Create a Mechanism from its SASL name |
| 144 | +
|
| 145 | + Args: |
| 146 | + name (str): SASL name of the desired mechanism |
| 147 | +
|
| 148 | + Returns: |
| 149 | + Mechanism: the desired mechanism |
| 150 | +
|
| 151 | + Raises: |
| 152 | + GSSError |
| 153 | +
|
| 154 | + :requires-ext:`rfc5801` |
| 155 | + """ |
| 156 | + if rfc5801 is None: |
| 157 | + raise NotImplementedError("Your GSSAPI implementation does not " |
| 158 | + "have support for RFC 5801") |
| 159 | + if isinstance(name, six.text_type): |
| 160 | + name = name.encode(_utils._get_encoding()) |
| 161 | + |
| 162 | + m = rfc5801.inquire_mech_for_saslname(name) |
| 163 | + |
| 164 | + return cls(m) |
| 165 | + |
| 166 | + @classmethod |
| 167 | + def from_attrs(cls, m_desired=None, m_except=None, m_critical=None): |
| 168 | + """ |
| 169 | + Get a generator of mechanisms supporting the specified attributes. See |
| 170 | + RFC 5587's :func:`indicate_mechs_by_attrs` for more information. |
| 171 | +
|
| 172 | + Args: |
| 173 | + m_desired ([OID]): Desired attributes |
| 174 | + m_except ([OID]): Except attributes |
| 175 | + m_critical ([OID]): Critical attributes |
| 176 | +
|
| 177 | + Returns: |
| 178 | + [Mechanism]: A set of mechanisms having the desired features. |
| 179 | +
|
| 180 | + Raises: |
| 181 | + GSSError |
| 182 | +
|
| 183 | + :requires-ext:`rfc5587` |
| 184 | + """ |
| 185 | + if isinstance(m_desired, roids.OID): |
| 186 | + m_desired = set([m_desired]) |
| 187 | + if isinstance(m_except, roids.OID): |
| 188 | + m_except = set([m_except]) |
| 189 | + if isinstance(m_critical, roids.OID): |
| 190 | + m_critical = set([m_critical]) |
| 191 | + |
| 192 | + if rfc5587 is None: |
| 193 | + raise NotImplementedError("Your GSSAPI implementation does not " |
| 194 | + "have support for RFC 5587") |
| 195 | + |
| 196 | + mechs = rfc5587.indicate_mechs_by_attrs(m_desired, |
| 197 | + m_except, |
| 198 | + m_critical) |
| 199 | + return (cls(mech) for mech in mechs) |
0 commit comments