forked from adb014/CTFd-SSO-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
48 lines (40 loc) · 1.56 KB
/
models.py
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
from CTFd.config import process_boolean_str
from CTFd.models import db
from CTFd.utils import get_app_config
#from authlib.integrations.flask_client import token_update
def fetch_token():
return request.cookies.get("token")
class OAuthClients(db.Model):
__tablename__ = "oauth_clients"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text)
client_id = db.Column(db.Text)
client_secret = db.Column(db.Text)
access_token_url = db.Column(db.Text)
authorize_url = db.Column(db.Text)
api_base_url = db.Column(db.Text)
server_metadata_url = db.Column(db.Text)
# In a later update you will be able to customize the login button
color = db.Column(db.Text)
icon = db.Column(db.Text)
# Allow the OAuth provider to be individually enabled/disabled
enabled = db.Column(db.Boolean, default=False)
def register(self, oauth):
if process_boolean_str(get_app_config("OAUTH_HAS_ROLES")):
scope = 'profile openid roles'
else:
scope = 'profile openid email'
oauth.register(
name=self.id,
client_id=self.client_id,
client_secret=self.client_secret,
access_token_url=self.access_token_url,
authorize_url=self.authorize_url,
api_base_url=self.api_base_url,
server_metadata_url=self.server_metadata_url,
fetch_token=fetch_token,
client_kwargs={'scope': scope}
)
def disconnect(self, oauth):
oauth._registry[self.id] = None
oauth._clients[self.id] = None