1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """The glue.auth module provides methods to communicate with data
17 stored behind the LIGO.ORG authentication system
18 """
19
20 import os
21 import sys
22 import stat
23 from six.moves import urllib
24 import six.moves.http_cookiejar
25
26 from .saml import HTTPNegotiateAuthHandler
27 from .. import git_version
28
29 __author__ = "Duncan Macleod <duncan.macleod@ligo.org>"
30 __credits__ = "Scott Koranda <scott.koranda@ligo.org>"
31 __date__ = git_version.date
32 __version__ = git_version.id
33
34 COOKIE_JAR = '/tmp/%s_cookies' % os.getenv('USER')
35 LIGO_LOGIN_URL = 'login.ligo.org'
36
37
39 """Request the given URL using LIGO.ORG SAML authentication.
40
41 This requires an active Kerberos ticket for the user, to get one:
42
43 $ kinit albert.einstein@LIGO.ORG
44
45 Parameters
46 ----------
47 url : `str`
48 URL path for request
49 debug : `bool`, optional
50 Query in verbose debuggin mode, default `False`
51
52 Returns
53 -------
54 urllib.addinfourl
55 file object containing output data, use .read() to extract
56 text content
57 """
58
59 debug = int(debug)
60
61
62 httpsHandler = HTTPSHandler(debuglevel = debug)
63
64
65 jar = six.moves.http_cookiejar.LWPCookieJar()
66
67
68
69 if os.path.exists(COOKIE_JAR):
70 os.chmod(COOKIE_JAR, stat.S_IRUSR | stat.S_IWUSR)
71
72
73 jar.load(COOKIE_JAR, ignore_discard = True)
74
75
76 cookie_handler = urllib.request.HTTPCookieProcessor(jar)
77
78 redirectHandler = urllib.request.HTTPRedirectHandler()
79
80
81
82 auth_handler = HTTPNegotiateAuthHandler(service_principal='HTTP@%s'
83 % (LIGO_LOGIN_URL))
84
85
86 opener = urllib.request.build_opener(auth_handler, cookie_handler, httpsHandler,
87 redirectHandler)
88
89
90 request = urllib.request.Request(url)
91
92
93 response = opener.open(request)
94
95
96
97 jar.save(COOKIE_JAR, ignore_discard=True)
98
99 return response
100