/
lgn_client.py
218 lines (141 loc) · 5.55 KB
/
lgn_client.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#**************************************************************************
# Copyright (c) 2012 Digi International Inc.,
# All rights not expressly granted are reserved.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
#
#**************************************************************************
from __future__ import print_function
import webapp2
from os import environ
import logging
from suds.client import Client
from suds.sax.element import Element
log = logging.getLogger(__name__)
wsdl_base = environ['MAP_WSDL_BASE']
log.info('wsdl_base: {}'.format(wsdl_base))
LGN_URL = '{}LoginService?wsdl'.format(wsdl_base)
log.info('login url {}'.format(LGN_URL))
def get_lgn_client():
'''
Returns a soap client object.
If an object has already been created, we recyle it,
otherwise, a new one is created and returned.
'''
app = webapp2.get_app()
request = webapp2.get_request()
username = request.registry['session'].get('username')
password = request.registry['session'].get('password')
lgn_key = 'lgn_client:{}'.format(username)
# Check if we already have the client
lgn_client = app.registry.get(lgn_key)
if not lgn_client:
lgn_client = Client(LGN_URL, cache=None)
lgn_client.add_prefix(
'srv', "http://service.login.core.mtrak.digi.com")
lgn_client.add_prefix(
'vo', "http://vo.login.core.mtrak.digi.com/xsd")
app.registry[lgn_key] = lgn_client
lgn_client.set_options(soapheaders=(
Element('username').setText(username),
Element('password').setText(password)))
return lgn_client
def _make_VO(namespace, name, **kwargs):
'''
Creates a Suds SOAP value object.
namespace = string
name = string
'''
lgn_client = get_lgn_client()
vo_str = namespace + ':' + name
vo = lgn_client.factory.create(vo_str)
for key in kwargs:
vo[key] = kwargs[key]
return vo
def authenticate(username, password):
"""
Authenticate the current profile that is logged in.
"""
lgn_client = get_lgn_client()
userVO = _make_VO('vo', 'UserVO')
userVO.userName = username
userVO.password = password
userVO.portalId = 1
resp = lgn_client.service.authenticate(userVO)
return resp, lgn_client.options.transport.cookiejar
def change_password(old_password, new_password):
"""
Change the password given the old password and the new one.
"""
lgn_client = get_lgn_client()
passwordVO = _make_VO('vo', 'PasswordVO')
passwordVO.newPassword = new_password
passwordVO.oldPassword = old_password
response = lgn_client.service.changePassword(passwordVO)
return response
def reset_password(new_password, answer1, answer2, answer3,
question1, question2, question3, userName):
"""
Reset the password given all of the security questions and answers.
"""
lgn_client = get_lgn_client()
session = webapp2.get_request().registry.get('session')
userId = session.get('user_id')
securityQuestionVO = _make_VO('vo', 'SecurityQuestionVO')
securityQuestionVO.newPassword = new_password
securityQuestionVO.reminderAnswer1 = answer1
securityQuestionVO.reminderQuestion1 = question1
securityQuestionVO.reminderAnswer2 = answer2
securityQuestionVO.reminderQuestion2 = question2
securityQuestionVO.reminderAnswer3 = answer3
securityQuestionVO.reminderQuestion3 = question3
securityQuestionVO.userId = userId
securityQuestionVO.userName = userName
response = lgn_client.service.resetPassword(securityQuestionVO)
return response
def create_security_question(answer1, answer2, answer3,
question1, question2, question3, userName):
"""
Create a new security question.
"""
lgn_client = get_lgn_client()
session = webapp2.get_request().registry.get('session')
userId = session.get('user_id')
securityQuestionVO = _make_VO('vo', 'SecurityQuestionVO')
securityQuestionVO.reminderAnswer1 = answer1
securityQuestionVO.reminderQuestion1 = question1
securityQuestionVO.reminderAnswer2 = answer2
securityQuestionVO.reminderQuestion2 = question2
securityQuestionVO.reminderAnswer3 = answer3
securityQuestionVO.reminderQuestion3 = question3
securityQuestionVO.userId = userId
securityQuestionVO.userName = userName
response = lgn_client.service.createSecurityQuestion(securityQuestionVO)
return response
def validate_security_question(answer1, answer2, answer3,
question1, question2, question3, userName):
"""
Create a new security question.
"""
lgn_client = get_lgn_client()
securityQuestionVO = _make_VO('vo', 'SecurityQuestionVO')
securityQuestionVO.answer1 = answer1
securityQuestionVO.question1 = question1
securityQuestionVO.answer2 = answer2
securityQuestionVO.question2 = question2
securityQuestionVO.answer3 = answer3
securityQuestionVO.question3 = question3
securityQuestionVO.userName = userName
response = lgn_client.service.validateSecurityQuestion(securityQuestionVO)
return response
def get_security_questions(userName):
"""
Get the current security questions for a user
"""
lgn_client = get_lgn_client()
response = lgn_client.service.findSecurityQuestionByUserName(userName)
return response