SUAP OAuth2 Configuration¶
Basic Configuration¶
In your settings.py:
SUAP_AUTH = {
'CLIENT_ID': 'your-client-id',
'CLIENT_SECRET': 'your-client-secret',
'REDIRECT_URI': 'https://your-application.com/auth/suap/callback/',
}
Configuration Options¶
Key |
Default |
Description |
|---|---|---|
|
required |
SUAP Application Client ID |
|
required |
SUAP Application Client Secret |
|
required |
Callback URL registered in SUAP |
|
|
Base URL of the SUAP server |
|
|
OAuth2 requested scopes |
|
|
Field of the |
|
Dictionary of attribute mapping rules |
|
|
|
List of fetchers executed in the Chain of Responsibility |
|
|
List of SUAP endpoints to query and merge |
|
|
List of mappers executed in the Chain of Responsibility |
|
|
|
|
|
Direct redirect to SUAP or intermediate login page |
|
|
If |
|
|
Default values assigned when creating a new user |
|
|
Additional values applied only to the first created user in the database. |
|
|
List of mapped fields saved on creation ( |
|
|
List of mapped fields synchronized on each login ( |
|
|
Authentication backend class path |
Complete Example with Multiple Endpoints and Mappers¶
SUAP_AUTH = {
'CLIENT_ID': 'your-client-id',
'CLIENT_SECRET': 'your-client-secret',
'REDIRECT_URI': 'https://your-application.com/auth/suap/callback/',
'USER_INFO_ENDPOINTS': [
"/api/rh/eu/",
"/api/rh/meus-dados/",
{
"endpoint": "/api/rh/meus-vinculos/",
"namespace": "vinculos",
"extract_list": "results",
},
],
'USER_INFO_MAPPERS': [
"django_suap_auth.mappers.DefaultAttrMapUserMapper",
"my_app.mappers.CustomProfileUserMapper",
],
'USER_ATTR_MAP': {
"username": "identificacao",
"email": "email",
"rg": "rg",
"cargo": "vinculo.cargo",
"setor": "vinculo.setor_suap",
"foto": {
"key": "url_foto_75x100",
"transform": "django_suap_auth.transformers.fetch_image_file",
},
"is_servidor": lambda info: any(v.get("tipo") == "servidor" for v in info.get("vinculos", [])),
},
}
User Creation Control¶
By default, the library automatically creates a user in Django on their first login via SUAP. This behavior is fully configurable:
1. Disable Automatic Creation via Configuration¶
If the CREATE_USER option is set to False, login will be denied for users who do not have a pre-existing local account in Django, raising a django_suap_auth.exceptions.SuapUserNotAllowedError exception:
SUAP_AUTH = {
'CLIENT_ID': 'your-client-id',
'CLIENT_SECRET': 'your-client-secret',
'REDIRECT_URI': 'https://your-application.com/auth/suap/callback/',
'CREATE_USER': False, # Disables automatic user creation on login
}
2. First User as Superuser (FIRST_USER_DEFAULTS)¶
By default, if no user exists in the database at the moment of the first login, the first created user automatically receives is_staff = True and is_superuser = True (via FIRST_USER_DEFAULTS = {"is_staff": True, "is_superuser": True}).
To disable this automatic promotion of the first user, set the key to None in your settings.py:
SUAP_AUTH = {
'CLIENT_ID': 'your-client-id',
'CLIENT_SECRET': 'your-client-secret',
'REDIRECT_URI': 'https://your-application.com/auth/suap/callback/',
'FIRST_USER_DEFAULTS': None, # First created user will not be staff/superuser
}
3. Dynamic Decision via Custom Backend¶
To apply dynamic conditional rules during login (e.g. allowing user registration only for employees or specific link types), subclass SuapAuthBackend or SuapProfileAuthBackend and override get_or_create_user or create_user:
from django_suap_auth.profile.backends import SuapProfileAuthBackend
from django_suap_auth.exceptions import SuapUserNotAllowedError
class CustomSuapAuthBackend(SuapProfileAuthBackend):
def get_or_create_user(self, lookup_field, lookup_value, mapped_attrs, cfg):
raw_info = mapped_attrs.get("suap_data", {})
# Example: only allow automatic creation for Staff/Employees
if raw_info.get("tipo_vinculo") != "Servidor":
raise SuapUserNotAllowedError("Automatic creation allowed only for staff members.")
return super().get_or_create_user(lookup_field, lookup_value, mapped_attrs, cfg)