Package: siwe-auth-django
Siwe Authentication - DjangoSiwe Authentication is a Django app designed for Ethereum-based authentication using the Sign-In with Ethereum (EIP-4361) standard. It allows users to sign in using their Ethereum wallets, and provides flexible settings for customization.Table Of ContentsGet StartedInstallationConfigurationConfigure your settings.pyConfigure your urls.pyRun migrationsUsageCustom GroupsDjango User ModelContrubutingLicenseGet StartedInstallationInstall the package using pip with the following command:pipinstallsiwe-auth-djangoConfigurationAdd'siwe_auth'toINSTALLED_APPSin your settings.py file:# settings.pyINSTALLED_APPS=[# ...'siwe_auth',# ...]Add authentication configurations in your settings.py file:# settings.pyAUTH_USER_MODEL="siwe_auth.Wallet"AUTHENTICATION_BACKENDS=["siwe_auth.backends.SiweBackend","django.contrib.auth.backends.ModelBackend"# this is necessary if you want to use superusers in django admin authentication]SESSION_COOKIE_AGE=3*60*60If you need to create a customized auth user model refer toDjango User Modelsection (Recommended).Add theSIWE_AUTHconfiguration in your settings.py file:Available settings:"CSRF_EXEMPT": Flag indicating whether CSRF protection is exempted for Siwe Authentication views (if you are creating an REST API must beTrue)."PROVIDER": Ethereum provider URL (it is required)."CREATE_GROUPS_ON_AUTH": Flag indicating whether to create groups on user authentication."CREATE_ENS_PROFILE_ON_AUTH": Flag indicating whether to create ENS profiles on user authentication."CUSTOM_GROUPS": List of custom groups to be created on user authentication. If you need to create more group manager refer toCustom Groupssection.# settings.pyfromsiwe_authimportgroup# needed if you want to set custom groups# ...SIWE_AUTH={"CSRF_EXEMPT":True,# default False"PROVIDER":"https://mainnet.infura.io/v3/...",# required"CREATE_GROUPS_ON_AUTH":True,# default False"CREATE_ENS_PROFILE_ON_AUTH":True,# default True"CUSTOM_GROUPS":[("usdt_owners",groups.ERC20OwnerManager(config={'contract':'0x82E...550'})),("nft_owners",groups.ERC721OwnerManager(config={'contract':'0x785...3A5'})),# ...],# default []}Include the Siwe Authentication URLs in your project's urls.py:# urls.pyfromdjango.urlsimportinclude,pathurlpatterns=[# ...path('api/auth',include('siwe_auth.urls',namespace='siwe_auth')),# ...]Run migrations:pythonmanage.pymigrateUsageYou need to follow this steps to successful authentication using SIWE protocol (EIP-4361):Get nonce: GET Method/api/auth/nonce.Use that nonce to create a SIWE message in frontend and sign the message with your metamask or another wallet.Login: POST Method/api/auth/login, using the message and signature. Example request body:{"message":{"domain":"your_domain.com","address":"0xA8f1...61905","statement":"This is a test statement.","uri":"https://your_domain.com","version":"1","chainId":1,"nonce":"2483e73dedffbd2616773506","issuedAt":"2024-01-27T18:43:48.011Z"},"signature":"0xf5b4ea...7bda4e177276dd1c"}Now you have the sessionid in cookies so you can use it for authenticated required views.Refresh the sessionid: POST Methodapi/auth/refresh.Verify if you are authenticated: GET Methodapi/auth/verify.Logout: POST Methodapi/auth/logout.Custom GroupsThree custom group managers are provided by default:ERC20OwnerManagerERC721OwnerManagerERC1155OwnerManagerYou can create more group managers by extending theGroupManagerclass:fromweb3importHTTPProviderfromsiwe_auth.groupsimportGroupManagerclassMyCustomGroup(GroupManager):def__init__(self,config:dict):# Your custom logicpassdefis_member(self,wallet:object,provider:HTTPProvider)->bool:# Your custom logic to determine membershippassYou can create custom groups in your settings.py:# settings.pyfromsiwe_authimportgroup# needed if you want to set custom groups# ...SIWE_AUTH={# ..."CUSTOM_GROUPS":[("usdt_owners",groups.ERC20OwnerManager(config={'contract':'0x82E...550'})),("nft_owners",groups.ERC721OwnerManager(config={'contract':'0x785...3A5'})),("token_owners",groups.ERC1154OwnerManager(config={'contract':'0x872...5F5'})),# ...],# default []}Then you can manage these groups with the django GroupManager, example:fromdjango.contrib.auth.modelsimportGroup# ...usdt_owners_group=Group.objects.get(name='usdt_owners')all_usdt_owners=usdt_owners_group.user_set.all()# ...Django User ModelBy default, Siwe Authentication uses theWalletmodel as the user model. If you prefer to use a specific user model, you can either use the providedAbstractWalletmodel or create your own user model. For more details, refer to theConfigurationsection.# Django project your_app/models.pyfromsiwe_auth.modelsimportAbstractWalletclassMyUserModel(AbstractWallet):# Add your custom fields hereIf you use a customized user model you need to register a customized admin site.# Django project your_app/admin.pyfromdjango.contribimportadminfromdjango.contrib.authimportget_user_modelfromsiwe_auth.adminimportWalletBaseAdminWalletModel=get_user_model()# You can inherit from siwe_auth.admin.WalletBaseAdmin or from django.contrib.auth.admin.BaseUserAdmin if you preffer.classWalletAdmin(WalletBaseAdmin):# Add your custom configuration hereadmin.site.register(WalletModel,WalletAdmin)ContributingContributions are welcome! Please create issues for bugs or feature requests. Pull requests are encouraged.LicenseThis project is licensed under the MIT License - see the LICENSE file for details.