@@ -64,6 +64,7 @@ def save_user(self, request, user, form, commit=True):
64
64
class SocialAccountAdapter (DefaultSocialAccountAdapter ):
65
65
def pre_social_login (self , request , sociallogin ):
66
66
self ._filter_email_addresses (sociallogin )
67
+ self ._block_use_of_old_github_oauth_app (request , sociallogin )
67
68
self ._connect_github_app_to_existing_github_account (request , sociallogin )
68
69
69
70
def _filter_email_addresses (self , sociallogin ):
@@ -134,3 +135,42 @@ def _can_use_github_app(self, user):
134
135
Only staff users can use the GitHub App for now.
135
136
"""
136
137
return user .is_staff
138
+
139
+ def _block_use_of_old_github_oauth_app (self , request , sociallogin ):
140
+ """
141
+ Block the use of the old GitHub OAuth app if the user is already using the new GitHub App.
142
+
143
+ This is a temporary measure to block the use of the old GitHub OAuth app
144
+ until we switch our login to always use the new GitHub App.
145
+
146
+ If the user has its account still connected to the old GitHub OAuth app,
147
+ we allow them to use it, since there is no difference between using the two apps
148
+ for logging in.
149
+ """
150
+ provider = sociallogin .account .get_provider ()
151
+
152
+ # If the provider is not GitHub, nothing to do.
153
+ if provider .id != GitHubProvider .id :
154
+ return
155
+
156
+ # If the user is still using the old GitHub OAuth app, nothing to do.
157
+ if sociallogin .is_existing :
158
+ return
159
+
160
+ has_gh_app_social_account = SocialAccount .objects .filter (
161
+ provider = GitHubAppProvider .id ,
162
+ uid = sociallogin .account .uid ,
163
+ ).exists ()
164
+
165
+ # If there is no existing GitHub App account, nothing to do.
166
+ if not has_gh_app_social_account :
167
+ return
168
+
169
+ # Show a warning to the user and redirect them to the GitHub App login page.
170
+ messages .warning (
171
+ request ,
172
+ "You already migrated from our old GitHub OAuth app. "
173
+ "Click below to sign in with the new GitHub App." ,
174
+ )
175
+ url = reverse ("githubapp_login" )
176
+ raise ImmediateHttpResponse (HttpResponseRedirect (url ))
0 commit comments