Skip to content

Commit b61ba54

Browse files
authored
Merge pull request #57 from zfi/master
Merging branch 1.1 into Master
2 parents 97dc3d1 + e24ca53 commit b61ba54

File tree

6 files changed

+115
-74
lines changed

6 files changed

+115
-74
lines changed

Failures.py

Lines changed: 96 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,131 @@
11
import logging
2+
'''
3+
Failure messages
4+
5+
These functions provide for a standard return message for all known
6+
and expected error conditions.
7+
8+
The return payload is a JSON document and an HTTP error code. The JSON
9+
document includes these elements:
10+
11+
success - boolean
12+
message - a short message that uniquely identifies the error
13+
code - a distrinct return code that allows the client to act on
14+
then specic error condition encountered.
15+
field - optional data element that identified the name of the
16+
data element involved in the error.
17+
data - optional data element that provides an id or key value
18+
for the data set being processed when the error occurred.
19+
20+
The HTTP error code indicates whether the request succeeded or failed. If
21+
the call is successful, the service will return a code 200 and a message
22+
of "OK".
23+
24+
If the request is unsuccessful and the error is due to faulty client data,
25+
return an HTTP error code of 401 if the user is unknown or authentication fails.
26+
Return an HTTP 500 error if the failure is due to an issue within the server,
27+
such as unable to access the back-end database.
28+
29+
'''
230

331

432
def unknown_user_id(id_user):
533
logging.debug('Failures: Unknown user id: %s', id_user)
634
return {
7-
'success': False,
8-
'message': 'Unknown user',
9-
'code': 400,
10-
'data': id_user
11-
}, 500
35+
'success': False,
36+
'message': 'Unknown user',
37+
'code': 400,
38+
'data': id_user
39+
}, 400
1240

1341

1442
def unknown_user_email(email):
1543
logging.debug('Failures: Unknown user email: %s', email)
1644
return {
17-
'success': False,
18-
'message': 'Unknown user',
19-
'code': 400,
20-
'data': email
21-
}, 500
45+
'success': False,
46+
'message': 'Unknown user',
47+
'code': 400,
48+
'data': email
49+
}, 400
2250

2351

2452
def unknown_user_screen_name(screen_name):
2553
logging.debug('Failures: Unknown user by screen name: %s', screen_name)
2654
return {
27-
'success': False,
28-
'message': 'Unknown user screen name',
29-
'code': 400,
30-
'data': screen_name
31-
}, 500
55+
'success': False,
56+
'message': 'Unknown user screen name',
57+
'code': 400,
58+
'data': screen_name
59+
}, 400
3260

3361

3462
def email_already_in_use(email):
3563
logging.debug('Failures: Email already in use: %s', email)
3664
return {
37-
'success': False,
38-
'message': 'Email already in use',
39-
'code': 450,
40-
'data': email
41-
}, 500
65+
'success': False,
66+
'message': 'Email already in use',
67+
'code': 450,
68+
'data': email
69+
}, 400
4270

4371

4472
def email_not_confirmed(email):
4573
logging.debug('Failures: Email %s not confirmed', email)
4674
return {
47-
'success': False,
48-
'message': 'Email not confirmed',
49-
'code': 430
50-
}, 401
75+
'success': False,
76+
'message': 'Email not confirmed',
77+
'code': 430,
78+
'data': email
79+
}, 401
5180

5281

5382
def user_blocked(email):
5483
logging.debug('Failures: User %s blocked', email)
5584
return {
56-
'success': False,
57-
'message': 'User is blocked',
58-
'code': 420
59-
}, 401
85+
'success': False,
86+
'message': 'User is blocked',
87+
'code': 420,
88+
'data': email
89+
}, 403
6090

6191

6292
def not_a_number(field, value):
6393
logging.error('Failures: Not a valid number: %s -> %s', field, value)
6494
return {
65-
'success': False,
66-
'message': 'Not a valid number',
67-
'code': 310,
68-
'field': field,
69-
'value': value
70-
}, 400
95+
'success': False,
96+
'message': 'Not a valid number',
97+
'code': 310,
98+
'field': field,
99+
'value': value
100+
}, 400
71101

72102

73103
def passwords_do_not_match():
74104
logging.debug('Failures: Passwords do not match')
75105
return {
76-
'success': False,
77-
'message': "Password confirm doesn't match",
78-
'code': 460
79-
}, 500
106+
'success': False,
107+
'message': "Password confirm doesn't match",
108+
'code': 460
109+
}, 400
80110

81111

82112
def password_complexity():
83113
logging.debug('Failures: Password is not complex enough')
84114
return {
85-
'success': False,
86-
'message': "Password is not complex enough",
87-
'code': 490
88-
}, 500
115+
'success': False,
116+
'message': "Password is not complex enough",
117+
'code': 490
118+
}, 400
89119

90120

91121
def screen_name_already_in_use(screen_name):
92122
logging.debug('Failures: Screen name already in use: %s', screen_name)
93123
return {
94-
'success': False,
95-
'message': "Screenname already in use",
96-
'data': screen_name,
97-
'code': 500
98-
}, 500
124+
'success': False,
125+
'message': "Screenname already in use",
126+
'code': 500,
127+
'data': screen_name
128+
}, 400
99129

100130

101131
def rate_exceeded(time):
@@ -106,37 +136,38 @@ def rate_exceeded(time):
106136
"""
107137
logging.debug('Failures: Rate exceeded')
108138
return {
109-
'success': False,
110-
'message': 'Insufficient bucket tokens',
111-
'data': time,
112-
'code': 470
113-
}, 500
139+
'success': False,
140+
'message': 'Insufficient bucket tokens',
141+
'code': 470,
142+
'data': time
143+
}, 400
114144

115145

116146
def wrong_password(email):
117147
logging.debug('Failures: Wrong password for %s', email)
118148
return {
119-
'success': False,
120-
'message': 'Wrong password',
121-
'code': 410
149+
'success': False,
150+
'message': 'Wrong password',
151+
'code': 410,
152+
'data': email
122153
}, 401
123154

124155

125156
def unknown_bucket_type(bucket_type):
126157
logging.debug('Failures: Unknown bucket type: %s', bucket_type)
127158
return {
128-
'success': False,
129-
'message': 'Unknown bucket type',
130-
'code': 180,
131-
'data': bucket_type
132-
}, 500
159+
'success': False,
160+
'message': 'Unknown bucket type',
161+
'code': 180,
162+
'data': bucket_type
163+
}, 400
133164

134165

135166
def wrong_auth_source(auth_source):
136167
logging.debug('Failures: Wrong auth source: %s', auth_source)
137168
return {
138-
'success': False,
139-
'message': 'Wrong auth source',
140-
'code': 480,
141-
'data': auth_source
142-
}, 500
169+
'success': False,
170+
'message': 'Wrong auth source',
171+
'code': 480,
172+
'data': auth_source
173+
}, 500

app/AuthToken/controllers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def post(self):
3939
# Parse numbers
4040
try:
4141
id_user = int(id_user)
42-
except:
42+
except ValueError:
4343
return Failures.not_a_number('idUser', id_user)
4444

4545
# Validate user exists, is validated and is not blocked

app/Authenticate/controllers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,36 @@
1515
authenticate_app = Blueprint('authenticate', __name__, url_prefix='/authenticate')
1616
api = Api(authenticate_app)
1717

18-
18+
# Authenticate a login attempt using local auth
1919
class AuthenticateLocalUser(Resource):
2020

2121
def post(self):
2222
# Get values
2323
server = request.headers.get('server')
2424
email = request.form.get('email')
2525
password = request.form.get('password')
26-
#browser = request.form.get('browser')
27-
#ip_address = request.form.get('ipAddress')
2826

2927
# Validate required fields
3028
validation = Validation()
3129
validation.add_required_field('server', server)
3230
validation.add_required_field('email', email)
3331
validation.add_required_field('password', password)
34-
#validation.add_required_field('browser', browser)
35-
#validation.add_required_field('ipAddress', ip_address)
32+
3633
if not validation.is_valid():
3734
return validation.get_validation_response()
3835

3936
# Validate user exists, is validated and is not blocked
4037
user = user_services.get_user_by_email(email)
38+
4139
if user is None:
4240
return Failures.unknown_user_email(email)
41+
4342
if not user.confirmed:
4443
return Failures.email_not_confirmed(email)
44+
4545
if user.blocked:
4646
return Failures.user_blocked(email)
47+
4748
if user.auth_source != 'local':
4849
return Failures.wrong_auth_source(user.auth_source)
4950

app/LocalUser/controllers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def post(self):
6363
if confirm_token is None:
6464
# Unknown token
6565
return {'success': False, 'code': 510}
66+
6667
if confirm_token.id_user != user.id:
6768
# Token is not for this user
6869
return {'success': False, 'code': 510}
@@ -214,6 +215,9 @@ def get(self, email):
214215
if user.auth_source != 'local':
215216
return Failures.wrong_auth_source(user.auth_source)
216217

218+
if not user.confirmed:
219+
return Failures.email_not_confirmed(user.email)
220+
217221
success, code, message = user_service.send_password_reset(user.id, server)
218222

219223
db.session.commit()

app/RateLimiting/controllers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ def get(self, bucket_type, id_user, count):
7676
# Parse numbers
7777
try:
7878
id_user = int(id_user)
79-
except:
79+
except ValueError:
8080
return Failures.not_a_number('idUser', id_user)
8181

8282
try:
8383
count = int(count)
84-
except:
84+
except ValueError:
8585
return Failures.not_a_number('count', count)
8686

8787
# Validate user exists, is validated and is not blocked

app/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@
2424
app = Flask(__name__)
2525

2626
# Application version (major,minor,patch-level)
27-
version = "1.1.7"
27+
version = "1.1.9"
2828

2929
"""
3030
Change Log
3131
32+
1.1.9 Update failure module to include missing return elements.
33+
34+
1.1.8 Fail any attempt to reset an account password is the account
35+
email address has not yet been confirmed.
36+
3237
1.1.7 Update application logging to separate application events from
3338
those logged by the uwsgi servivce
3439

0 commit comments

Comments
 (0)