70 lines
No EOL
1.8 KiB
Python
70 lines
No EOL
1.8 KiB
Python
#!/usr/bin/env python
|
|
import os
|
|
import time
|
|
from flask import Flask, abort, request, jsonify, g, url_for
|
|
import jwt
|
|
from pipewire_python import link
|
|
|
|
inputs = link.list_inputs()
|
|
outputs = link.list_outputs()
|
|
source = outputs[-1]
|
|
sink = inputs[-1]
|
|
|
|
|
|
# initialization
|
|
app = Flask(__name__)
|
|
app.config['SECRET_KEY'] = 'the quick brown fox jumps over the lazy dog'
|
|
|
|
class User():
|
|
def generate_auth_token(self, expires_in=600):
|
|
return jwt.encode(
|
|
{'id': self.id, 'exp': time.time() + expires_in},
|
|
app.config['SECRET_KEY'], algorithm='HS256')
|
|
|
|
@staticmethod
|
|
def verify_auth_token(token):
|
|
try:
|
|
data = jwt.decode(token, app.config['SECRET_KEY'],
|
|
algorithms=['HS256'])
|
|
except:
|
|
return
|
|
return User.query.get(data['id'])
|
|
|
|
|
|
# @auth.verify_password
|
|
# def verify_password(username_or_token, password):
|
|
# # first try to authenticate by token
|
|
# user = User.verify_auth_token(username_or_token)
|
|
# if not user:
|
|
# g.user = user
|
|
# return True
|
|
|
|
@app.route('/api/token')
|
|
# @auth.login_required
|
|
def get_auth_token():
|
|
token = g.user.generate_auth_token(600)
|
|
return jsonify({'token': token.decode('ascii'), 'duration': 600})
|
|
|
|
|
|
@app.route('/api/resource')
|
|
# @auth.login_required
|
|
def get_resource():
|
|
return jsonify({'data': 'Hello, %s!' % g.user.username})
|
|
|
|
|
|
@app.route('/api/connect')
|
|
# @auth.login_required
|
|
def connect():
|
|
source.connect(sink)
|
|
return jsonify({'data': 'connect'})
|
|
|
|
@app.route('/api/disconnect')
|
|
# @auth.login_required
|
|
def disconnect():
|
|
source.disconnect(sink)
|
|
return jsonify({'data': 'disconnect'})
|
|
|
|
if __name__ == '__main__':
|
|
# if not os.path.exists('db.sqlite'):
|
|
# db.create_all()
|
|
app.run(debug=True, host='0.0.0.0', port=5000) |