Django: DoesNotExist: SocialApp matching query does not exist - python

I'm trying to activate social logins in my Django web application, which comes from open source software in this GitHub repository (so I didn't write it); and am running into this well-known issue:
DoesNotExist: SocialApp matching query does not exist.
The base settings file is located here. I do not modify that file at all. Instead, I import (inherit) it at the top of my deploy.py settings file, and make overrides and customization there.
Specifically related to this issue, here are the relevant overrides and additions that I made in deploy.py to enable Google and Twitter social authentication, both of which result in the same error:
INSTALLED_APPS.remove('allauth.socialaccount.providers.persona') # Remove
INSTALLED_APPS.append('allauth.socialaccount.providers.google') # Add
INSTALLED_APPS.append('allauth.socialaccount.providers.twitter') # Add
_GOOGLE = {
'SCOPE': ['email', 'https://www.googleapis.com/auth/userinfo.profile'],
'AUTH_PARAMS': {'access_type': 'online'},
'PROVIDER_KEY': get_env("GOOGLE_PROVIDER_KEY"), # Stored in secrets.env
'PROVIDER_SECRET_KEY': get_env("GOOGLE_PROVIDER_SECRET_KEY"), # Stored in secrets.env
}
SOCIALACCOUNT_PROVIDERS['google'] = _GOOGLE # This isn't enabled in biostar.settings.base
_TWITTER = {
'SCOPE': ['email'],
'AUTH_PARAMS': {'access_type': 'online'},
'PROVIDER_KEY': get_env("TWITTER_PROVIDER_KEY"), # Stored in secrets.env
'PROVIDER_SECRET_KEY': get_env("TWITTER_PROVIDER_SECRET_KEY"), # Stored in secrets.env
}
SOCIALACCOUNT_PROVIDERS['twitter'] = _TWITTER
I show two provider examples here -- Twitter and Google -- to show the pattern of what I am doing, and to show that the issue isn't provider-specific; though let's try to focus on just Twitter to keep things simple.
Now according to this document -- which comes from a close fork of the above project -- in addition to what I implemented above (programmatically), it is necessary to also set up the same Social Accounts and the Keys/Secret-keys for them in the Social Apps section of the Django Admin panel. (Note that that fork was worked on by more-or-less the same team; so this requirement likely applies to the original upstream implementation, as well). The relevant part of that document instructs the following:
After restoring, you need to re-enter social login info.
Unfortunately, even though this information is required to be in the config
environment that initializes biostar, it also requires it
(redundantly) to be in the database as well. So go to the Django Admin panel
and click on Social Apps and then go through each app and fill in the
appropriate values.
So I went ahead and did that, too.
No matter what I try (different providers, different keys, django panel additions, no django panel additions, etc), I get the aforementioned exception, as though I'm missing a step.
Incidentally, I tried both key-pairs for Twitter (because I always forget which pair to use; so tried both Consumer Key (API Key) + Consumer Secret (API Secret) and Access Token + Access Token Secret, though it's really the former from what I read online). Just mentioning this for completeness.
What am I missing? Any ideas?
Thank you in advance! :)
EDIT-1: Here is a full paste of the exception: https://pastebin.com/0UBAfAtu

I was getting the same error again and again and solve with this ..
The reason we get this error SITE_ID = 1
When "SocialApp matching query does not exist" occurs,
this ID needs to be replaced.
SITE_ID = 1
hope for anyone else facing this issue will get help for this.

Following below steps will resolve this issue:
Go to your database table named django_site.
Look at the id number of the site mentioned as a chosen site in the Social Applications.
Now configure SITE_ID to id number in setting.py of the Django project.

I had a similar problem and as people have said, you need to know the SITE_ID!
You have to make sure that on the Django admin dashboard, you have added your social application under SOCIAL ACCOUNTS.
When you're creating the social application, make sure that have listed the right websites for "chosen sites". If you're on local development, paste your computer IP in there. There will be a new SITE_ID associated with each "Chosen site" that you add to your application. Make sure you're using the right one that's associated to your IP. Personally, after adding my IP, I then just played and incremented my SITE_ID variable until it worked.
Posting this in case someone in a similar situation in the future lands on this page.

Related

Should I disable BroswableAPIRenderer for production environment?

Hi Guys I am deploying a django project to elastic-beanstalk on AWS it currently is working fine, however I would like to know if it's a good or bad practice to allow the BrowsableAPIRenderer to be able to handle requests on my browser, I try to find anything related to it but there is really not too much documentation if not any at all. My App has a really strict permission policy, even when I access through the browsable API, it returns the following:
{
"detail": "Authentication credentials were not provided."
}
however it displays some sort of "information" about the endpoint. It's in that part where I find it difficult to define if I should allow it so that other developers can easily know what is going on, or on the other hand if it is a big risk to be accessible to the public.
You may find this question and its answers useful. Based on this answer, it is true that providing the BrowsableAPIRenderer would help development:
It provides simple UI interface to interact with model objects.
It can provide detailed debugging information.
A list of URLs can be shown in some API root
Based on your current settings, a user would at least need to log on either in DRF login page or Django ADMIN page to see and interact with your API.
You can enable the BrowsableAPI in development but disable it in your production settings following this answer.
In production, I wouldn't want other users, who have write permission, to interact with the APIs via BrowsableAPI. It will force the other users to use the front end app or other secure app to interact with APIs. That would provide a secure layer to forbid descriptive actions done using BrowsableAPI.

Django multiple sites : Prevent cross-site authentification

I'm currently developing 2 different sites at the same time: one of them is a heavily customized django-admin interface, and the other a "stand-alone" website that will share it's database with the previous one .
Even though they are related, I'd like my users not to loosely identify between the two sites : they are both able to be independant of the other.
However, a problem arises when someone is logged in the "admin" site : when they go to the other website, they are automatically logged. Won't happen the other way unless I allow it though, as the admin site requires special permissions in the User model.
I already created an UserProfile that can differentiate an user of one of the sites or of the both.
So, knowing all this, how can I make sure that the customers of the admin site don't get to be authenticated when in the other web site (without, of course, logging them out from the first one) ?
Thanks !
EDIT : To format it better, here is what I got , summed up :
One admin application / site                                                      Both running
One related application / site                                                   on same server,
                                                                                      sharing settings and urls.py
If some is logged in admin, I want to require them to create a new session to log on [related site] : this, without logging them off the admin site.
What changes should I do to this configuration to achieve this ?
Put different SESSION_COOKIE_DOMAIN and SESSION_COOKIE_NAME for each appication. Hope this solve your issue.
SESSION_COOKIE_DOMAIN = 'site1.com' #site2.com for other
SESSION_COOKIE_NAME = 'sid1' #sid2 for other

Adding custom attributes to django scheema

I am trying to authenticate my django application written in python with okta IDP. I have almost configured everything at SP side and IDP side too. Now I need to pass a custom variable from IDP which assert SP that user is a publisher,editor or admin and further save this to the django format database (in auth_user_groups table). Anyone have tried doing this, or anyone has idea about this?
I am able to get the custom variable values by attributes mappings from IDP. But this allows me to save the custom attributes only on the user table. please let me know if i have not made myself clear here about my question.
Once again I have a privilege to answer my own question. So hear is the solution.
Django has a user profile module which is to be turned on by giving the module location in the settings.py
i.e -
"AUTH_PROFILE_MODULE = appTitle.UserProfile"
The UserProfile needs to be specified in modules.py specifying the required structure of user profile u need for your app.
Now doing sync -db django creates the Database table for your user profile and further on the same user profile pysaml adds the value (CustomAttribute) which come on the saml Assertion.
more explanations on this can be found on django documentations too.
If any one still faces any issue please let me know.

Configuring multi-tenancy

In a local dev env, I'm currently attempting to hack my way to multi-tenancy using Mezzanine:
Mezzanine makes use of Django’s sites app to support multiple sites in a single project. This functionality is always “turned on” in Mezzanine:
That's pretty much as far as an entry-level tut for multi-tenancy on mezzanine gets. Great, so I go to the admin site, and add a site:
Domain name-----|----Display name
127.0.0.1:8000----|----English Site
127.0.0.1:8000/es|----Spanish Site
Now, I am stuck. I have fiddled around with url.py, but figured that's not where to start, considering I need to know something to map the url to. Views? Lost.
Any ideas?
(Included translation is not an option given the web service will never translate as good as a human.)
Multitenancy in mezzanine is done via domain names. You will need to run http://dev.site and http://esdev.site or similar in development (add the entries to your hosts file and make sure they match the listings in the sites part of admin).
In production you'll also want to use two different domains too.
For example, my personal site http://dpn.name/ and my business site http://behest.com.au/ are both running off the same mezzanine install.
Later on when you have the right setup, you'll be able to add new posts and pages to each specific site by either logging into the admin via each domain name, or changing the currently active site in the admin (the drop down is in the top right if you have multiple sites set up)
Hope that helps, please let me know if you need more info.

How do you change <title> of a Satchmo Store?

This seems like something that should be obvious, but I simply can't find it.
I created a Satchmo store using clonesatchmo.py, as indicated by the installation instructions.
I have tried to change the name of the site through the admin interface (going to Sites and changing the display name) and by editing local_setting.py, changing the line:
SITE_NAME = "Simple Satchmo"
to
SITE_NAME = "Anything Else"
By neither change impacts the actual store. Simple Satchmo remains in the title field, mocking me. So, what idiotic thing am I doing wrong?
You need to go to Admin, Store configurations and change the Store Name for your store.
Modify the tittle by web site Admin on both Shop / Store Configuration: Store Name.
and Sites / Sites: Display Name. They should be the same.
The latter name is used by some utils related to Django, like sending registration email.
Setting the line SITE_NAME has any effect only before the first running "clonesatchmo.py" or "manage.py satchmo_load_store". Note that the line is preceded by a comment
# These are used when loading the test data
This is not enough explanatory IMO. I will suggest in the development team to remove it by installation script in Satchmo 0.9.3. It will be at least better explained now.

Categories