-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·343 lines (286 loc) · 11.7 KB
/
Copy pathapp.py
File metadata and controls
executable file
·343 lines (286 loc) · 11.7 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
import os
import secrets
from datetime import datetime
from pathlib import Path
from flask import Flask, render_template, redirect, url_for, request, flash, g, make_response
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
from flask_wtf import FlaskForm
from flask_wtf.csrf import CSRFProtect
from wtforms import TextAreaField, SubmitField
from wtforms.validators import DataRequired, Length
from oauthlib.oauth2 import WebApplicationClient
import requests
from dotenv import load_dotenv
import sys
load_dotenv()
TARGET = os.getenv('TARGET', '').upper().strip()
REMOTE_ADDRESS = os.getenv('REMOTE_ADDRESS', '').strip()
if TARGET == 'LOCAL':
DEFAULT_YANDEX_REDIRECT_URI = 'http://127.0.0.1:5001/login/yandex/authorized'
DEFAULT_GOOGLE_REDIRECT_URI = 'http://127.0.0.1:5001/login/google/authorized'
elif TARGET == 'REMOTE':
if not REMOTE_ADDRESS:
print(" ERROR: TARGET is set to REMOTE but REMOTE_ADDRESS is not defined")
print(" Please set REMOTE_ADDRESS to your server's IP or domain name")
sys.exit(1)
REMOTE_ADDRESS = REMOTE_ADDRESS.replace('http://', '').replace('https://', '').rstrip('/')
DEFAULT_YANDEX_REDIRECT_URI = f'https://{REMOTE_ADDRESS}/login/yandex/authorized'
DEFAULT_GOOGLE_REDIRECT_URI = f'https://{REMOTE_ADDRESS}/login/google/authorized'
else:
print(" ERROR: TARGET environment variable is not set correctly")
print(" TARGET should be either 'LOCAL' or 'REMOTE'")
print(" - LOCAL: for development on localhost")
print(" - REMOTE: for deployment on a remote server")
sys.exit(1)
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY') or secrets.token_hex(32)
app.config['SESSION_COOKIE_SECURE'] = (TARGET == 'REMOTE')
app.config['SESSION_COOKIE_HTTPONLY'] = True
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
app.config['REMEMBER_COOKIE_SECURE'] = (TARGET == 'REMOTE')
app.config['REMEMBER_COOKIE_HTTPONLY'] = True
app.config['REMEMBER_COOKIE_DURATION'] = 2592000
app.config['REMEMBER_COOKIE_SAMESITE'] = 'Lax'
# disable cache
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 30
app.config['TEMPLATES_AUTO_RELOAD'] = True
# /disable cache
csrf = CSRFProtect(app)
base_dir = Path(__file__).parent.absolute()
instance_path = base_dir / 'instance'
os.makedirs(instance_path, exist_ok=True)
database_path = instance_path / 'site.db'
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{database_path}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login_manager = LoginManager(app)
login_manager.login_view = 'index'
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
yandex_id = db.Column(db.String(255), unique=True)
google_id = db.Column(db.String(255), unique=True)
name = db.Column(db.String(255))
email = db.Column(db.String(255), unique=True)
comments = db.relationship('Comment', backref='user', lazy=True)
class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
class CommentForm(FlaskForm):
body = TextAreaField('Comment', validators=[DataRequired(), Length(min=1, max=500)])
submit = SubmitField('Отправить')
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
YANDEX_CLIENT_ID = os.getenv('YANDEX_CLIENT_ID')
YANDEX_CLIENT_SECRET = os.getenv('YANDEX_CLIENT_SECRET')
YANDEX_REDIRECT_URI = os.getenv('YANDEX_REDIRECT_URI') or DEFAULT_YANDEX_REDIRECT_URI
GOOGLE_CLIENT_ID = os.getenv('GOOGLE_CLIENT_ID')
GOOGLE_CLIENT_SECRET = os.getenv('GOOGLE_CLIENT_SECRET')
GOOGLE_REDIRECT_URI = os.getenv('GOOGLE_REDIRECT_URI') or DEFAULT_GOOGLE_REDIRECT_URI
yandex_client = WebApplicationClient(YANDEX_CLIENT_ID) if YANDEX_CLIENT_ID else None
google_client = WebApplicationClient(GOOGLE_CLIENT_ID) if GOOGLE_CLIENT_ID else None
@app.before_request
def before_request():
# disable cache
try:
app.jinja_env.cache.clear()
except Exception:
pass
# /disable cache
g.gtm_data = {
'page_path': request.path,
'user_authenticated': current_user.is_authenticated,
'user_id': current_user.id if current_user.is_authenticated else None,
'user_email': current_user.email if current_user.is_authenticated else None,
'auth_method': None,
}
if current_user.is_authenticated:
if current_user.yandex_id:
g.gtm_data['auth_method'] = 'yandex'
elif current_user.google_id:
g.gtm_data['auth_method'] = 'google'
@app.context_processor
def inject_gtm_data():
gtm_data = getattr(g, 'gtm_data', {})
gtm_json = {
'user_authenticated': gtm_data.get('user_authenticated', False),
'user_id': gtm_data.get('user_id'),
'user_email': gtm_data.get('user_email'),
'auth_method': gtm_data.get('auth_method'),
'page_path': gtm_data.get('page_path', ''),
}
gtm_json = {k: v for k, v in gtm_json.items() if v is not None}
return {
'gtm_data': gtm_json,
'TARGET': TARGET,
}
# disable cache
@app.after_request
def add_no_cache_headers(response):
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '0'
response.headers['Surrogate-Control'] = 'no-store'
response.headers.pop('ETag', None)
response.headers.pop('Last-Modified', None)
return response
# /disable cache
@app.route('/')
def index():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/contacts')
def contacts():
return render_template('contacts.html')
@app.route('/projects')
def projects():
return render_template('projects.html')
@app.route('/comments', methods=['GET', 'POST'])
def comments():
form = CommentForm()
comments_list = Comment.query.order_by(Comment.created_at.desc()).all()
if current_user.is_authenticated and form.validate_on_submit():
comment = Comment(body=form.body.data, user_id=current_user.id)
db.session.add(comment)
db.session.commit()
flash('Комментарий успешно добавлен', 'success')
return redirect(url_for('comments'))
return render_template('comments.html', comments=comments_list, form=form)
@app.route('/delete_comment/<int:comment_id>', methods=['POST'])
@login_required
def delete_comment(comment_id):
comment = Comment.query.get_or_404(comment_id)
if comment.user_id != current_user.id:
flash('Вы можете удалять только свои комментарии.', 'danger')
return redirect(url_for('comments'))
db.session.delete(comment)
db.session.commit()
flash('Комментарий удалён.', 'success')
return redirect(url_for('comments'))
@app.route('/login/yandex')
def login_yandex():
if not yandex_client:
flash('Yandex OAuth is disabled.', 'danger')
return redirect(url_for('comments'))
request_uri = yandex_client.prepare_request_uri(
'https://oauth.yandex.ru/authorize',
redirect_uri=YANDEX_REDIRECT_URI,
scope=['login:info', 'login:email', 'login:avatar'],
)
return redirect(request_uri)
@app.route('/login/yandex/authorized')
def callback_yandex():
if not yandex_client:
flash('Yandex OAuth is disabled.', 'danger')
return redirect(url_for('comments'))
code = request.args.get('code')
token_url, headers, body = yandex_client.prepare_token_request(
'https://oauth.yandex.ru/token',
authorization_response=request.url,
redirect_url=YANDEX_REDIRECT_URI,
code=code
)
token_response = requests.post(
token_url,
headers=headers,
data=body,
auth=(YANDEX_CLIENT_ID, YANDEX_CLIENT_SECRET),
)
yandex_client.parse_request_body_response(token_response.text)
uri, headers, body = yandex_client.add_token('https://login.yandex.ru/info?format=json')
userinfo_response = requests.get(uri, headers=headers, data=body)
user_data = userinfo_response.json()
unique_id = user_data.get('id')
users_email = user_data.get('default_email')
users_name = user_data.get('real_name') or user_data.get('display_name')
user = User.query.filter_by(yandex_id=unique_id).first()
if not user:
existing_user = User.query.filter_by(email=users_email).first()
if existing_user:
existing_user.yandex_id = unique_id
db.session.commit()
user = existing_user
else:
user = User(yandex_id=unique_id, name=users_name, email=users_email)
db.session.add(user)
db.session.commit()
login_user(user, remember=True)
flash(f'You have successfully logged in via Yandex as {user.name or user.email}!', 'success')
return redirect(url_for('comments'))
@app.route('/login/google')
def login_google():
if not google_client:
flash('Google OAuth is disabled.', 'danger')
return redirect(url_for('comments'))
request_uri = google_client.prepare_request_uri(
'https://accounts.google.com/o/oauth2/auth',
redirect_uri=GOOGLE_REDIRECT_URI,
scope=[
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'
],
access_type='offline',
prompt='consent'
)
return redirect(request_uri)
@app.route('/login/google/authorized')
def callback_google():
if not google_client:
flash('Google OAuth is disabled.', 'danger')
return redirect(url_for('comments'))
code = request.args.get('code')
token_url, headers, body = google_client.prepare_token_request(
'https://oauth2.googleapis.com/token',
authorization_response=request.url,
redirect_url=GOOGLE_REDIRECT_URI,
code=code
)
token_response = requests.post(
token_url,
headers=headers,
data=body,
auth=(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET),
)
google_client.parse_request_body_response(token_response.text)
uri, headers, body = google_client.add_token('https://www.googleapis.com/oauth2/v3/userinfo')
userinfo_response = requests.get(uri, headers=headers, data=body)
user_data = userinfo_response.json()
unique_id = user_data.get('sub')
users_email = user_data.get('email')
users_name = user_data.get('name')
user = User.query.filter_by(google_id=unique_id).first()
if not user:
existing_user = User.query.filter_by(email=users_email).first()
if existing_user:
existing_user.google_id = unique_id
db.session.commit()
user = existing_user
else:
user = User(google_id=unique_id, name=users_name, email=users_email)
db.session.add(user)
db.session.commit()
login_user(user, remember=True)
flash(f'You have successfully logged in via Google as {user.name or user.email}!', 'success')
return redirect(url_for('comments'))
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('You have been logged out.', 'info')
return redirect(url_for('index'))
if __name__ == '__main__':
with app.app_context():
db.create_all()
print("Database is ready")
print(f"Application started. HTTPS: {TARGET == 'REMOTE'}")
if TARGET == 'LOCAL':
app.run(host='0.0.0.0', port=5001, debug=True)
else:
from waitress import serve
serve(app, host='0.0.0.0', port=8000)