5
5
from flask import Flask , Response , send_from_directory
6
6
from flask .sessions import SecureCookieSessionInterface
7
7
from flask_mongoengine import MongoEngine
8
- from flask_security import ConfirmRegisterForm , MongoEngineUserDatastore , Security
8
+ from flask_security import ConfirmRegisterForm , MongoEngineUserDatastore , Security , UserDatastore
9
9
from werkzeug .exceptions import NotFound
10
10
from wtforms import StringField , ValidationError
11
11
12
- from common import DIContainer
12
+ from common import AccountRole , DIContainer
13
13
from monkey_island .cc .flask_utils import FlaskDIWrapper
14
14
from monkey_island .cc .models import Role , User
15
15
from monkey_island .cc .resources import (
@@ -91,16 +91,16 @@ def setup_authentication(app, data_dir):
91
91
92
92
# The database object needs to be created after we configure the flask application
93
93
db = MongoEngine (app )
94
-
95
94
user_datastore = MongoEngineUserDatastore (db , User , Role )
96
95
96
+ _create_roles (user_datastore )
97
+
97
98
# Only one user can be registered in the Island, so we need a custom validator
98
99
def validate_no_user_exists_already (_ , field ):
99
100
if user_datastore .find_user ():
100
101
raise ValidationError ("A user already exists. Only a single user can be registered." )
101
102
102
103
class CustomConfirmRegisterForm (ConfirmRegisterForm ):
103
-
104
104
# We don't use the email, but the field is required by ConfirmRegisterForm.
105
105
# Email validators need to be overriden, otherwise an error about invalid email is raised.
106
106
# Added custom validator to the email field because we have to override
@@ -109,6 +109,11 @@ class CustomConfirmRegisterForm(ConfirmRegisterForm):
109
109
"Email" , default = "dummy@dummy.com" , validators = [validate_no_user_exists_already ]
110
110
)
111
111
112
+ def to_dict (self , only_user ):
113
+ registration_dict = super ().to_dict (only_user )
114
+ registration_dict .update ({"roles" : [AccountRole .ISLAND_INTERFACE .name ]})
115
+ return registration_dict
116
+
112
117
app .security = Security (
113
118
app ,
114
119
user_datastore ,
@@ -121,6 +126,11 @@ class CustomConfirmRegisterForm(ConfirmRegisterForm):
121
126
app .session_interface = disable_session_cookies ()
122
127
123
128
129
+ def _create_roles (user_datastore : UserDatastore ):
130
+ user_datastore .find_or_create_role (name = AccountRole .ISLAND_INTERFACE .name )
131
+ user_datastore .find_or_create_role (name = AccountRole .AGENT .name )
132
+
133
+
124
134
def init_app_config (app , mongo_url , data_dir : Path ):
125
135
app .config ["MONGO_URI" ] = mongo_url
126
136
app .config ["MONGODB_SETTINGS" ] = [
@@ -207,7 +217,11 @@ def init_rpc_endpoints(api: FlaskDIWrapper):
207
217
api .add_resource (TerminateAllAgents )
208
218
209
219
210
- def init_app (mongo_url : str , container : DIContainer , data_dir : Path ):
220
+ def init_app (
221
+ mongo_url : str ,
222
+ container : DIContainer ,
223
+ data_dir : Path ,
224
+ ):
211
225
"""
212
226
Simple docstirng for init_app
213
227
0 commit comments