Skip to content
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

Fixing temporary file creation for Windows #665

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/saml2/sigver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import os
import six
import sys

from time import mktime

Expand All @@ -17,7 +18,8 @@
import saml2.cryptography.asymmetric
import saml2.cryptography.pki

from tempfile import NamedTemporaryFile
from tempfile import NamedTemporaryFile, gettempdir
from uuid import uuid4
from subprocess import Popen
from subprocess import PIPE

Expand Down Expand Up @@ -341,12 +343,21 @@ def make_temp(content, suffix="", decode=True, delete_tmpfiles=True):
content.encode("utf-8") if not isinstance(content, six.binary_type) else content
)
content_raw = base64.b64decode(content_encoded) if decode else content_encoded
ntf = NamedTemporaryFile(suffix=suffix, delete=delete_tmpfiles)
ntf = _make_temp(suffix=suffix, delete_tmpfiles=delete_tmpfiles)
ntf.write(content_raw)
ntf.seek(0)
return ntf


def _make_temp(suffix="", delete_tmpfiles=True):
# `NamedTemporaryFile` is not very reliable on Windows, so we'll make a
# tempfile a different way.
if sys.platform == 'win32':
return open(os.path.join(gettempdir(), '%s.%s' % (uuid4(), suffix)), 'w+b')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this ever be cleaned up?

else:
return NamedTemporaryFile(suffix=suffix, delete=delete_tmpfiles)


def split_len(seq, length):
return [seq[i:i + length] for i in range(0, len(seq), length)]

Expand Down Expand Up @@ -888,7 +899,7 @@ def _run_xmlsec(self, com_list, extra_args):
key-value parameters
:result: Whatever xmlsec wrote to an --output temporary file
"""
with NamedTemporaryFile(suffix='.xml') as ntf:
with _make_temp(suffix='.xml') as ntf:
com_list.extend(['--output', ntf.name])
com_list += extra_args

Expand Down