Package: ariadne-token-auth
ariadne-token-authA django app to implement token based authentication in projects which useariadne.Summaryariadne-token-authSummaryInstallingUsing the packageExample ProjectSetupMigrationsSchemaProtecting ViewsConfigurationBonusContributingVersioningAuthorsLicenseAcknowledgementsOthersInstallingThe package can be downloaded from itsgithubrepository.pipinstallgit+https://github.com/IgnisDa/ariadne-token-auth.gitOr usingpoetrypoetryaddgit+https://github.com/IgnisDa/ariadne-token-auth.git#mainUsing the packageExample ProjectYou can have a look at theexample projectfor a fully
working project.Habitracis also a production
website which uses this package to implement authentication.SetupInclude theAuthTokenMiddlewarein yourMIDDLEWAREsettings.MIDDLEWARE=[# Other middleware"ariadne_token_auth.middleware.AuthTokenMiddleware",]Include theAuthTokenBackendin yourAUTHENTICATION_BACKENDSsettings.AUTHENTICATION_BACKENDS=("django.contrib.auth.backends.ModelBackend","ariadne_token_auth.backends.AuthTokenBackend",)Finally addariadne_token_authto yourINSTALLED_APPS.INSTALLED_APPS=[# other apps'ariadne_token_auth',]MigrationsNext, runpython manage.py migrateto commit the auth-token model to your database.SchemaAdd the relevant mutations to your GraphQL schema.fromariadneimportMutationType,make_executable_schemafromariadne_token_auth.apiimportresolversauth_mutation=MutationType()auth_mutation.set_field("getAuthToken",resolvers.get_auth_token)auth_mutation.set_field("deleteAuthToken",resolvers.delete_auth_token)type_defs="""type Mutation {getAuthToken(identifier: String!, password: String!): AuthTokenPayload!deleteAuthToken(token: String!): DeleteTokenPayload!}"""schema=make_executable_schema([type_defs,resolvers.type_defs],auth_mutation)getAuthTokento authenticate an existing user and obtain a corresponding token. The
resolver uses the user model'sUSERNAME_FIELDwhich by default isusername. However
it will work with otherUSERNAME_FIELDs just fine, for example when the default user
identifier isemailinstead ofusername. Theexample projectdoes
this by defining acustom user model.mutationgetAuthToken($identifier:String!,$password:String!){getAuthToken(identifier:$identifier,password:$password){errorauth{token}}}If authentication is successful, you can obtain the auth-token fromresponse.data.getAuthToken.auth.token, and if it is unsuccessful, errors will
be present inresponse.data.getAuthToken.error.deleteAuthTokento delete a logged in user using the above token.mutationdeleteAuthToken($token:String!){deleteAuthToken(token:$token){statuserror}}If the token was correct and deletion was successful, the value ofresponse.data.deleteAuthToken.statuswill be set totrue(or it's equivalent in your
frontend language). Otherwise, the error will be present inresponse.data.deleteAuthToken.errorandresponse.data.deleteAuthToken.statuswill be
set tofalse.Protecting ViewsYou can use thelogin_requireddecorator to protect your graphql queries from
non-authenticated users.fromariadneimportQueryTypefromariadne_token_auth.decoratorsimportlogin_required@query.field("testQuery")@login_requireddeftest_query(self,info,*args,**kwargs):return{"user":info.context.get("request").user}ConfigurationSettings specific to ariadne-token-auth are stored in theARIADNE_TOKEN_AUTHdictionary
insettings.pyfile. The defaults can be seen inutils.pyfile. They can be configured as follows:# settings.pyARIADNE_TOKEN_AUTH={'TOKEN_NAME':'myBearer',# case insensitive'TOKEN_LENGTH':15}With the above settings, you will have to send requests in the following fashion (example
usescurl, but the basic premise stays the same).curl'http://127.0.0.1:8000/graphql/'\-H'Content-Type: application/json'\-H'Authorization: myBearer 8496fda8dedad2235921693717c8dc'\--data-binary'{"query":"query {\n  testQuery {\n    user\n  }\n}"}'BonusYou can find a very easy way to add your*.graphqlfiles to the django auto-reloaderhere.ContributingPlease readCONTRIBUTING.mdfor details on our code
of conduct, and the process for submitting pull requests to us.VersioningWe useSemVerfor versioning. For the versions
available, see thetags on this
repository.AuthorsSee also the list ofcontributorswho participated in this project.LicenseThis project is licensed under theApache-2.0- see theLICENSE.mdfile for detailsAcknowledgementsdjango-token: Model definitions, middleware and authentication backends adapted fromdjango-tokenspackage.ariadne-jwt: Exceptions, and decorators adapted fromariadne-jwtpackage.OthersProject bootstrapped usingcookiecutterby IgnisDa.