|
| 1 | +GSSAPI="BASE" # This ensures that a full module is generated by Cython |
| 2 | + |
| 3 | +from gssapi.raw.cython_types cimport * |
| 4 | +from gssapi.raw.cython_converters cimport c_create_oid_set |
| 5 | +from gssapi.raw.cython_converters cimport c_get_mech_oid_set |
| 6 | +from gssapi.raw.cython_converters cimport c_py_ttl_to_c, c_c_ttl_to_py |
| 7 | +from gssapi.raw.creds cimport Creds |
| 8 | +from gssapi.raw.names cimport Name |
| 9 | +from gssapi.raw.oids cimport OID |
| 10 | + |
| 11 | +from gssapi.raw.misc import GSSError |
| 12 | +from gssapi.raw.named_tuples import AcquireCredResult, AddCredResult |
| 13 | + |
| 14 | + |
| 15 | +cdef extern from "gssapi/gssapi_ext.h": |
| 16 | + OM_uint32 gss_export_cred(OM_uint32 *min_stat, gss_cred_id_t cred_handle, |
| 17 | + gss_buffer_t token) nogil |
| 18 | + |
| 19 | + OM_uint32 gss_import_cred(OM_uint32 *min_stat, gss_buffer_t token, |
| 20 | + gss_cred_id_t *cred_handle) nogil |
| 21 | + |
| 22 | + |
| 23 | +def export_cred(Creds creds not None): |
| 24 | + """Export GSSAPI credentials object |
| 25 | +
|
| 26 | + This method exports a GSSSAPI credentials object into a token |
| 27 | + which may be transmitted between different processes. |
| 28 | +
|
| 29 | + Args: |
| 30 | + creds (Creds): the credentials object to be exported |
| 31 | +
|
| 32 | + Returns: |
| 33 | + bytes: the exported token representing the given credentials object |
| 34 | +
|
| 35 | + Raises: |
| 36 | + GSSError |
| 37 | + """ |
| 38 | + |
| 39 | + # GSS_C_EMPTY_BUFFER |
| 40 | + cdef gss_buffer_desc exported_creds = gss_buffer_desc(0, NULL) |
| 41 | + |
| 42 | + cdef OM_uint32 maj_stat, min_stat |
| 43 | + |
| 44 | + with nogil: |
| 45 | + maj_stat = gss_export_cred(&min_stat, creds.raw_creds, &exported_creds) |
| 46 | + |
| 47 | + if maj_stat == GSS_S_COMPLETE: |
| 48 | + res = exported_creds.value[:exported_creds.length] |
| 49 | + gss_release_buffer(&min_stat, &exported_creds) |
| 50 | + return res |
| 51 | + else: |
| 52 | + raise GSSError(maj_stat, min_stat) |
| 53 | + |
| 54 | + |
| 55 | +def import_cred(token not None): |
| 56 | + """Import GSSAPI credentials from a token |
| 57 | +
|
| 58 | + This method imports a credentials object from a token |
| 59 | + previously exported by :func:`export_cred`. |
| 60 | +
|
| 61 | + Args: |
| 62 | + token (bytes): the token to import |
| 63 | +
|
| 64 | + Returns: |
| 65 | + Creds: the imported credentials object |
| 66 | +
|
| 67 | + Raises: |
| 68 | + GSSError |
| 69 | + """ |
| 70 | + |
| 71 | + cdef gss_buffer_desc token_buffer = gss_buffer_desc(len(token), token) |
| 72 | + |
| 73 | + cdef gss_cred_id_t creds |
| 74 | + |
| 75 | + cdef OM_uint32 maj_stat, min_stat |
| 76 | + |
| 77 | + with nogil: |
| 78 | + maj_stat = gss_import_cred(&min_stat, &token_buffer, &creds) |
| 79 | + |
| 80 | + cdef Creds res |
| 81 | + if maj_stat == GSS_S_COMPLETE: |
| 82 | + res = Creds() |
| 83 | + res.raw_creds = creds |
| 84 | + return res |
| 85 | + else: |
| 86 | + raise GSSError(maj_stat, min_stat) |
0 commit comments