forked from ronoaldo/openemm-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openemm.py
169 lines (147 loc) · 5.66 KB
/
openemm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# Copyright 2014 Ronoaldo JLP (http://www.ronoaldo.net)
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import uuid
import datetime
import time
from suds.plugin import MessagePlugin
from suds.client import Client
from suds.wsse import Security, UsernameToken
from hashlib import md5, sha1
from base64 import b64encode
from suds.wsse import UsernameToken, Token
from time import gmtime
WSDL = 'http://localhost:9090/openemm-ws2/emmservices.wsdl'
LOCATION = 'http://localhost:9090/openemm-ws2/'
class MapItemPlugin(MessagePlugin):
"""
SUDS plugin to properly handle the ns0:Map and ns0:MapItem
encoding for the OpenEMM API.
"""
def marshalled(self, context):
"""
Walks over the request Body and properly encodes the
key and value attributes of MapItem.
"""
body = context.envelope.getChild('Body')
def v(node):
if node.name in ['key','value']:
node.addPrefix('xsd', 'http://www.w3.org/2001/XMLSchema')
node.addPrefix('xsi', 'http://www.w3.org/2001/XMLSchema-instance')
node.set('xsi:type', 'xsd:string')
body.walk(v)
def Connect(username, password):
"""
Connects to the OpenEMM server at L{LOCATION}, parsing the
L{WSDL} using SUDS.
@ivar username: the openemm webserivce username
@type username: str
@ivar password: the openemm webservice password
@type password: str
@return a valid L{suds.client.Client} object
"""
client = Client(WSDL, location=LOCATION, plugins=[MapItemPlugin()])
# WSSE header authentication handler
token = UsernameDigestToken(username, password)
security = Security()
security.tokens.append(token)
client.set_options(wsse=security)
return client
class UsernameDigestToken(UsernameToken):
"""
Represents a basic I{UsernameToken} WS-Security token with password digest
@ivar username: A username.
@type username: str
@ivar password: A password.
@type password: str
@ivar nonce: A set of bytes to prevent reply attacks.
@type nonce: str
@ivar created: The token created.
@type created: L{datetime}
@doc: http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0.pdf
"""
def __init__(self, username=None, password=None):
UsernameToken.__init__(self, username, password)
self.setcreated()
self.setnonce()
def setcreated(self, created=None):
if created is None:
# Local offset required to properly validate on server.
# See discussion here:
# http://forum.openemm.org/using-openemm-f3/topic2607.html
local_offset = time.timezone / 60
local_offset_delta = datetime.timedelta(minutes=local_offset)
created = datetime.datetime.utcnow() + local_offset_delta
created_str = created.strftime('%Y-%m-%dT%H:%M:%SZ')
self.created = created_str
else:
self.created = created
def setnonce(self, text=None):
"""
Set I{nonce} which is arbitraty set of bytes to prevent
reply attacks.
@param text: The nonce text value.
Generated when I{None}.
@type text: str
@override: Nonce save binary string to build digest password
"""
if text is None:
s = []
s.append(self.username)
s.append(self.password)
s.append(Token.sysdate())
m = md5()
m.update(':'.join(s))
self.raw_nonce = m.digest()
self.nonce = b64encode(self.raw_nonce)
else:
self.nonce = text
def xml(self):
self.setnonce()
self.setcreated()
usernametoken = UsernameToken.xml(self)
password = usernametoken.getChild('Password')
nonce = usernametoken.getChild('Nonce')
created = usernametoken.getChild('Created')
password.set('Type', 'http://docs.oasis-open.org/wss/2004/01/'
'oasis-200401-wss-username-token-profile-1.0'
'#PasswordDigest')
s = sha1()
s.update(self.raw_nonce)
s.update(created.getText())
s.update(password.getText())
password.setText(b64encode(s.digest()))
nonce.set('EncodingType', 'http://docs.oasis-open.org/wss/2004'
'/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary')
return usernametoken
def EncodeMap(client, parameters):
"""
Utility function to encode a python map into a Map/MapItem object
set required on some OpenEMM method calls.
@ivar client: the SUDS client connected with OpenEMM.
@type client: suds.client.Client
@ivar parameters: the python parameter map
@type parameters: map
@return an object properly encoded as a Map/MapItem structure.
"""
result = client.factory.create('ns0:Map')
for k, v in parameters.iteritems():
entry = client.factory.create('ns0:MapItem')
entry.key = k
entry.value = v
result.item.append(entry)
return result
if __name__ == '__main__':
client = Connect('test', 'test')
print client