File size: 519 Bytes
5fe2042
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import List

import falcon


class StaticTokenAuthMiddleware:
    def __init__(self, tokens: List[str]):
        self._tokens = set(tokens)

    def process_request(self, request, _):
        if request.method == 'OPTIONS':
            return

        if request.path == '/':
            return

        token = request.get_header('Authorization')
        if token is None or token.split(' ')[-1] not in self._tokens:
            raise falcon.HTTPUnauthorized