2020-05-21 11:20:27 +00:00
|
|
|
import flask
|
|
|
|
from flask import Blueprint, redirect, request
|
|
|
|
from flask import current_app, session
|
|
|
|
from flask import jsonify
|
|
|
|
from flask.helpers import make_response
|
|
|
|
from flask.templating import render_template
|
2022-02-19 22:16:13 +00:00
|
|
|
from flask.typing import ResponseReturnValue
|
2020-05-21 11:20:27 +00:00
|
|
|
|
|
|
|
from flask import Blueprint, render_template, request, url_for
|
|
|
|
import logging
|
|
|
|
|
2020-05-26 20:55:37 +00:00
|
|
|
from ..model import User
|
2020-05-21 11:20:27 +00:00
|
|
|
from ..auth_providers import LdapAuthProvider
|
2022-02-19 22:16:13 +00:00
|
|
|
from ..hydra import hydra_service
|
|
|
|
from ory_hydra_client.api.admin import introspect_o_auth_2_token
|
|
|
|
from ory_hydra_client.models import GenericError
|
2020-05-21 11:20:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
api_views = Blueprint('api', __name__, url_prefix='/api')
|
|
|
|
|
|
|
|
|
|
|
|
@api_views.route('/users', methods=['GET'])
|
2022-02-19 22:16:13 +00:00
|
|
|
def user_list() -> ResponseReturnValue:
|
2020-05-21 11:20:27 +00:00
|
|
|
if 'authorization' not in request.headers:
|
|
|
|
return '', 403
|
|
|
|
token = request.headers['authorization'].replace('Bearer ', '')
|
2022-02-19 22:16:13 +00:00
|
|
|
token_info = introspect_o_auth_2_token.sync(_client=hydra_service.hydra_client)
|
2020-05-21 11:20:27 +00:00
|
|
|
|
2022-02-19 22:16:13 +00:00
|
|
|
if token_info is None or isinstance(token_info, GenericError):
|
|
|
|
return 'internal errror', 500
|
|
|
|
|
|
|
|
if not isinstance(token_info.scope, str) or 'lc_i_userlist' not in token_info.scope.split(' '):
|
2020-05-21 11:20:27 +00:00
|
|
|
return '', 403
|
|
|
|
|
2020-05-27 15:56:10 +00:00
|
|
|
return jsonify([
|
|
|
|
{'username': str(user.username), 'email': str(user.email)}
|
|
|
|
for user in User.query_().all()])
|