summaryrefslogtreecommitdiff
path: root/inventory/api.py
blob: 91872f557ba74df502eb8b000037b662564fc31b (plain)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import datetime
import functools
import time

from bson import ObjectId
from flask import Blueprint, jsonify, current_app, request, abort, url_for, g
from flask_pymongo import PyMongo
import itsdangerous
import jwt
from marshmallow import ValidationError
import pymongo
import pytz
import requests
import yarl

from .schema import NodeSchema

# Setup PyMongo
mongo = PyMongo(current_app, tz_aware=True)
mongo.db.nodes.create_index([('fields.value', pymongo.TEXT), ('name', pymongo.TEXT)], name='fields.value_text_name_text')
mongo.db.nodes.create_index([('parent_id', pymongo.ASCENDING)], name='parent_id')
mongo.db.nodes.create_index([('user_id', pymongo.ASCENDING)], name='user_id')

app = Blueprint('api', __name__)


# Error handling
@app.errorhandler(400)
def on_invalid_request(e):
    return jsonify({'message': e.description or 'Invalid request'}), 400


@app.errorhandler(403)
def on_forbidden(e):
    return jsonify({'message': e.description or 'Forbidden'}), 403


@app.errorhandler(404)
def on_not_found(e):
    return jsonify({'message': e.description or 'Not found'}), 404


@app.errorhandler(500)
def on_internal_error(e):
    return jsonify({'message': e.description or 'Invalid request'}), 500


@app.errorhandler(ValidationError)
def on_validation_error(e):
    return jsonify({'message': 'Validation error', 'fields': e.messages}), 400


def auth_required(f):
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        authorization = request.headers.get('Authorization')
        if not authorization:
            abort(403)
        auth_type, access_token = authorization.split(None, 1)
        if auth_type != 'Bearer':
            abort(403)
        try:
            token = jwt.decode(access_token, key=current_app.config['JWT_PUBLIC_KEY'],
                    audience=current_app.config['OAUTH_CLIENT_ID'])
        except jwt.InvalidTokenError:
            abort(403)
        user_id = ObjectId(token['sub'])
        user = mongo.db.users.find_one({'_id': user_id})
        if not user:
            abort(403)
        g.user = user
        return f(*args, **kwargs)
    return wrapper


def format_node(node):
    parents = dict((parent['_id'], parent) for parent in node.pop('parents'))
    current = node
    # Recursively assign each node their respective parent
    while current:
        # NOTE: Python assigns from left to right
        current['parent'] = current = parents.get(current.get('parent_id'))

    children = dict((child['_id'], child) for child in node.pop('children'))
    children_list = node['children'] = []
    for child in children.values():
        # Direct children should be assigned to the top-most list
        if child['parent_id'] == node['_id']:
            children_list.append(child)
            continue
        # Otherwise assign to their respective parent
        parent = children[child['parent_id']]
        parent.setdefault('children', []).append(child)

    return node

def format_nodes(nodes):
    return (format_node(node) for node in nodes)

# Routes
@app.route('/nodes')
@auth_required
def root_nodes():
    result = mongo.db.nodes.aggregate([
        {'$match': {'parent_id': None, 'user_id': g.user['_id']}},
        {
            '$graphLookup': {
                'from': 'nodes',
                'startWith': '$parent_id',
                'connectFromField': 'parent_id',
                'connectToField': '_id',
                'as': 'parents',
            },
        },
        {
            '$graphLookup': {
                'from': 'nodes',
                'startWith': '$_id',
                'connectFromField': '_id',
                'connectToField': 'parent_id',
                'as': 'children',
                'maxDepth': 1,
            },
        },
    ])
    schema = NodeSchema(many=True)
    data = schema.dump(format_nodes(result))
    return jsonify(data)


@app.route('/nodes', methods=['POST'])
@auth_required
def add_node():
    data = request.json
    if data is None or not isinstance(data, dict):
        abort(400, 'Payload must be a JSON object')
    schema = NodeSchema()
    node = schema.load(data)
    node['created_at'] = pytz.utc.localize(datetime.datetime.utcnow())
    node['user_id'] = g.user['_id']
    result = mongo.db.nodes.insert_one(node)
    if not result.acknowledged:
        abort(500, 'Write operation not acknowledged')
    node_id = result.inserted_id
    return jsonify({'id': str(node_id), 'url': url_for('.node', node_id=node_id)}), 201


@app.route('/nodes/<ObjectId:node_id>')
@auth_required
def node(node_id):
    result = mongo.db.nodes.aggregate([
        {'$match': {'_id': node_id, 'user_id': g.user['_id']}},
        {
            '$graphLookup': {
                'from': 'nodes',
                'startWith': '$parent_id',
                'connectFromField': 'parent_id',
                'connectToField': '_id',
                'as': 'parents',
            },
        },
        {
            '$graphLookup': {
                'from': 'nodes',
                'startWith': '$_id',
                'connectFromField': '_id',
                'connectToField': 'parent_id',
                'as': 'children',
                'maxDepth': 1,
            },
        },
    ])
    try:
        node = result.next()
    except StopIteration:
        abort(404, 'No node found')

    node = format_node(node)

    schema = NodeSchema()
    return jsonify(schema.dump(node))


@app.route('/nodes/<ObjectId:node_id>', methods=['PUT'])
@auth_required
def update_node(node_id):
    data = request.json
    if data is None or not isinstance(data, dict):
        abort(400, 'Payload must be a JSON object')
    schema = NodeSchema()
    node = schema.load(data)
    node['updated_at'] = pytz.utc.localize(datetime.datetime.utcnow())
    result = mongo.db.nodes.update_one({'_id': node_id, 'user_id': g.user['_id']}, {'$set': node})
    if not result.acknowledged:
        abort(500, 'Write operation not acknowledged')
    return '', 204


@app.route('/nodes/<ObjectId:node_id>', methods=['DELETE'])
@auth_required
def delete_node(node_id):
    result = mongo.db.nodes.delete_one({'_id': node_id, 'user_id': g.user['_id']})
    if result.deleted_count == 0:
        abort(404, 'No node found')
    return jsonify({}), 204


@app.route('/search', methods=['POST'])
@auth_required
def find_nodes():
    if 'q' not in request.form:
        abort(400, 'Missing q argument')
    schema = NodeSchema(many=True)
    data = schema.dump(mongo.db.nodes.find({'$text': {'$search': request.form['q']}, 'user_id': g.user['_id']}))
    return jsonify(data)


@app.route('/auth/request', methods=['POST'])
def auth_request():
    signer = itsdangerous.URLSafeSerializer(current_app.config['SECRET_KEY'])
    state = signer.dumps(int(time.time()))
    redirect_uri = request.form.get('redirect_uri')
    url = (yarl.URL(current_app.config['OAUTH_URL']) / 'authorize').with_query(
        response_type='code',
        client_id=current_app.config['OAUTH_CLIENT_ID'],
        redirect_uri=str(redirect_uri),
        state=state,
    )
    return jsonify(url=str(url))


def set_refresh_token_cookie(response, refresh_token='', clear=False):
    response.set_cookie(key='refresh_token', value=refresh_token, path=url_for('.auth_refresh'),
            max_age=0 if clear else (86400 * 30), samesite='strict', httponly=True, secure=request.scheme == 'https')


@app.route('/auth/response', methods=['POST'])
def auth_response():
    code = request.form.get('code')
    state = request.form.get('state')
    signer = itsdangerous.URLSafeSerializer(current_app.config['SECRET_KEY'])
    try:
        signer.loads(state)
    except itsdangerous.exc.BadSignature:
        abort(500, 'Invalid state')
    redirect_uri = request.form.get('redirect_uri')
    url = yarl.URL(current_app.config['OAUTH_URL']) / 'token'
    response = requests.post(str(url), data={
        'grant_type': 'authorization_code',
        'code': code,
        'client_id': current_app.config['OAUTH_CLIENT_ID'],
        'client_secret': current_app.config['OAUTH_CLIENT_SECRET'],
        'redirect_uri': redirect_uri,
    })
    token = response.json()
    error = token.get('error')
    if error:
        abort(500, error)

    token_data = jwt.decode(token['access_token'], key=current_app.config['JWT_PUBLIC_KEY'],
            audience=current_app.config['OAUTH_CLIENT_ID'])
    # We're assuming sub is an ObjectId (this is true for jab)
    user_id = ObjectId(token_data['sub'])
    user = mongo.db.users.find_one({'_id': user_id})
    if not user:
        url = yarl.URL(current_app.config['OAUTH_URL']).parent / 'api' / 'user'
        response = requests.get(str(url), headers={
            'Authorization': 'Bearer {}'.format(token['access_token']),
        })
        userdata = response.json()
        user = {
            '_id': user_id,
            'username': userdata['username'],
        }
        result = mongo.db.users.insert_one(user)
        if not result.acknowledged:
            abort(500, 'Write not acknowledged')

    r = jsonify(access_token=token['access_token'])
    set_refresh_token_cookie(r, token['refresh_token'])
    return r


@app.route('/auth/refresh', methods=['POST'])
def auth_refresh():
    refresh_token = request.cookies.get('refresh_token')
    if not refresh_token:
        abort(403)
    url = yarl.URL(current_app.config['OAUTH_URL']) / 'token'
    response = requests.post(str(url), data={
        'grant_type': 'refresh_token',
        'client_id': current_app.config['OAUTH_CLIENT_ID'],
        'client_secret': current_app.config['OAUTH_CLIENT_SECRET'],
        'refresh_token': refresh_token,
    })
    if not response.ok:
        abort(403)
    data = response.json()
    r = jsonify(access_token=data['access_token'])
    set_refresh_token_cookie(r, data['refresh_token'])
    return r


@app.route('/auth/logout', methods=['POST'])
def auth_logout():
    r = jsonify()
    set_refresh_token_cookie(r, clear=True)
    return r


@app.route('/user')
@auth_required
def user_info():
    url = yarl.URL(current_app.config['OAUTH_URL']).parent / 'api' / 'user'
    response = requests.get(str(url), headers={'Authorization': request.headers.get('Authorization')})
    data = response.json()
    return jsonify(username=data['username'])


@app.route('/')
def index():
    return 'api'