-
Notifications
You must be signed in to change notification settings - Fork 16
/
app.py
299 lines (243 loc) · 8.49 KB
/
app.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
from flask import Flask, jsonify, abort, make_response, request
from flask_sqlalchemy import SQLAlchemy
from functools import wraps
from database import db
from models import User, Group
def create_app():
"""
Instantiate Flask
Implemented as a factory method to avoid a circular import error.
"""
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://localhost/scim"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
return app
app = create_app()
def auth_required(func):
"""Flask decorator to require the presence of a valid Authorization header."""
@wraps(func)
def check_auth(*args, **kwargs):
if request.headers["Authorization"].split("Bearer ")[1] == "123456789":
return func(*args, **kwargs)
else:
return make_response(jsonify({"error": "Unauthorized"}), 403)
return check_auth
@app.route("/scim/v2/Users", methods=["GET"])
@auth_required
def get_users():
"""Get SCIM Users"""
start_index = 1
count = None
if "start_index" in request.args:
start_index = int(request.args["startIndex"])
if "count" in request.args:
count = int(request.args["count"])
if "filter" in request.args:
single_filter = request.args["filter"].split(" ")
filter_value = single_filter[2].strip('"')
users = User.query.filter_by(userName=filter_value).first()
if not users:
users = []
else:
users = [users]
else:
users = User.query.paginate(start_index, count, False).items
serialized_users = [e.serialize() for e in users]
return make_response(
jsonify(
{
"schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
"totalResults": len(users),
"startIndex": start_index,
"itemsPerPage": len(users),
"Resources": serialized_users,
}
),
200,
)
@app.route("/scim/v2/Users/<string:user_id>", methods=["GET"])
@auth_required
def get_user(user_id):
"""Get SCIM User"""
user = User.query.get(user_id)
if not user:
return make_response(
jsonify(
{
"schemas": ["urn:ietf:params:scim:api:messages:2.0:Error"],
"detail": "User not found",
"status": 404,
}
),
404,
)
return jsonify(user.serialize())
@app.route("/scim/v2/Users", methods=["POST"])
@auth_required
def create_user():
"""Create SCIM User"""
active = request.json.get("active")
displayName = request.json.get("displayName")
emails = request.json.get("emails")
externalId = request.json.get("externalId")
groups = request.json.get("groups")
locale = request.json.get("locale")
givenName = request.json["name"].get("givenName")
middleName = request.json["name"].get("middleName")
familyName = request.json["name"].get("familyName")
password = request.json.get("password")
schemas = request.json.get("schemas")
userName = request.json.get("userName")
existing_user = User.query.filter_by(userName=userName).first()
if existing_user:
return make_response(
jsonify(
{
"schemas": ["urn:ietf:params:scim:api:messages:2.0:Error"],
"detail": "User already exists in the database.",
"status": 409,
}
),
409,
)
else:
try:
user = User(
active=active,
displayName=displayName,
emails_primary=emails[0]["primary"],
emails_value=emails[0]["value"],
emails_type=emails[0]["type"],
externalId=externalId,
locale=locale,
givenName=givenName,
middleName=middleName,
familyName=familyName,
password=password,
userName=userName,
)
db.session.add(user)
if groups:
for group in groups:
existing_group = Group.query.get(group["value"])
if existing_group:
existing_group.users.append(user)
else:
new_group = Group(displayName=group["displayName"])
db.session.add(new_group)
new_group.users.append(user)
db.session.commit()
return make_response(jsonify(user.serialize()), 201)
except Exception as e:
return str(e)
@app.route("/scim/v2/Users/<string:user_id>", methods=["PUT"])
@auth_required
def update_user(user_id):
"""Update SCIM User"""
user = User.query.get(user_id)
if not user:
return make_response(
jsonify(
{
"schemas": ["urn:ietf:params:scim:api:messages:2.0:Error"],
"detail": "User not found",
"status": 404,
}
),
404,
)
else:
groups = request.json.get("groups")
user.active = request.json.get("active")
user.displayName = request.json.get("displayName")
user.emails = request.json.get("emails")
user.externalId = request.json.get("externalId")
user.locale = request.json.get("locale")
user.name = request.json.get("name")
user.familyName = request.json["name"].get("familyName")
user.middleName = request.json["name"].get("middleName")
user.givenName = request.json["name"].get("givenName")
user.password = request.json.get("password")
user.schemas = request.json.get("schemas")
user.userName = request.json.get("userName")
db.session.commit()
return make_response(jsonify(user.serialize()), 200)
@app.route("/scim/v2/Users/<string:user_id>", methods=["PATCH"])
@auth_required
def deactivate_user(user_id):
"""Deactivate SCIM User"""
is_user_active = request.json["Operations"][0]["value"]["active"]
user = User.query.get(user_id)
user.active = is_user_active
db.session.commit()
return make_response("", 204)
@app.route("/scim/v2/Users/<string:user_id>", methods=["DELETE"])
@auth_required
def delete_user(user_id):
"""Delete SCIM User"""
user = User.query.get(user_id)
db.session.delete(user)
db.session.commit()
return make_response("", 204)
@app.route("/scim/v2/Groups", methods=["GET"])
@auth_required
def get_groups():
"""Get SCIM Groups"""
groups = Group.query.all()
return jsonify([e.serialize() for e in groups])
@app.route("/scim/v2/Groups/<string:group_id>", methods=["GET"])
@auth_required
def get_group(group_id):
"""Get SCIM Group"""
group = Group.query.get(group_id)
if not group:
abort(404)
return jsonify(group.serialize())
@app.route("/scim/v2/Groups", methods=["POST"])
@auth_required
def create_group():
"""Create SCIM Group"""
displayName = request.json["displayName"]
members = request.json["members"]
try:
group = Group(
displayName=displayName,
)
db.session.add(group)
db.session.commit()
return make_response(jsonify(group.serialize()), 201)
except Exception as e:
return str(e)
@app.route("/scim/v2/Groups/<string:group_id>", methods=["PATCH", "PUT"])
@auth_required
def update_group(group_id):
"""
Update SCIM Group
Accounts for the different requests sent by Okta depending
on if the group was created via template or app wizard integration.
"""
if "members" in request.json:
members = request.json["members"]
else:
members = request.json["Operations"][0]["value"]
if request.json["Operations"][0]["op"] == "replace":
return make_response("", 204)
group = Group.query.get(group_id)
db.session.add(group)
for member in members:
existing_user = User.query.get(member["value"])
if existing_user:
group.users.append(existing_user)
db.session.commit()
return make_response(jsonify(group.serialize()), 200)
@app.route("/scim/v2/Groups/<string:group_id>", methods=["DELETE"])
@auth_required
def delete_group(group_id):
"""Delete SCIM Group"""
group = Group.query.get(group_id)
db.session.delete(group)
db.session.commit()
return make_response("", 204)
if __name__ == "__main__":
app.run(debug=True)