auth: added invisible login
This commit is contained in:
parent
c4251caeb5
commit
e19fd11863
3 changed files with 18 additions and 12 deletions
21
api42.py
21
api42.py
|
|
@ -40,15 +40,18 @@ class Api:
|
|||
|
||||
def get_access_token(self, token: str, state: str, domain: str) -> str:
|
||||
self.add_rate()
|
||||
print('->', domain)
|
||||
r = requests.post(f"{self.intra}/oauth/token", data={
|
||||
"grant_type": "authorization_code",
|
||||
"client_id": self.key,
|
||||
"client_secret": self.secret,
|
||||
"code": token,
|
||||
"state": state,
|
||||
"redirect_uri": config.redirect_url.replace('{current_domain}', domain)
|
||||
})
|
||||
r = None
|
||||
try:
|
||||
r = requests.post(f"{self.intra}/oauth/token", data={
|
||||
"grant_type": "authorization_code",
|
||||
"client_id": self.key,
|
||||
"client_secret": self.secret,
|
||||
"code": token,
|
||||
"state": state,
|
||||
"redirect_uri": config.redirect_url.replace('{current_domain}', domain)
|
||||
})
|
||||
except Exception as e:
|
||||
return ""
|
||||
if r.status_code != 200:
|
||||
return ""
|
||||
print('token', "Authorization: Bearer " + r.json()["access_token"])
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ def auth():
|
|||
return '', 500
|
||||
cookie = db.create_cookie(user_id, request.headers.get('User-Agent'))
|
||||
db.close()
|
||||
resp = make_response(redirect('/', 307))
|
||||
previous = request.cookies.get("previous") or "/"
|
||||
resp = make_response(redirect(previous, 307))
|
||||
resp.set_cookie('token', cookie, expires=time.time() + 30 * 86400, httponly=True) # 30 days
|
||||
return resp
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from globals import *
|
|||
from functools import wraps
|
||||
from db import Db
|
||||
import config
|
||||
from flask import request, redirect
|
||||
from flask import request, redirect, make_response
|
||||
import json
|
||||
import requests
|
||||
import urllib.parse
|
||||
|
|
@ -31,7 +31,9 @@ def auth_required(function):
|
|||
userid = db.get_user_by_bookie(token)
|
||||
if userid == 0:
|
||||
db.close()
|
||||
return redirect("/redirect_42", 307)
|
||||
resp = make_response(redirect("/redirect_42", 307))
|
||||
resp.set_cookie("previous", str(request.url_rule), secure=True, max_age=None, httponly=True)
|
||||
return resp
|
||||
details = db.get_user_by_id(userid['userid'])
|
||||
db.close()
|
||||
userid['campus'] = details['campus']
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue