-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ 增加OIDC登入功能 * ✨ added init_user and use username to identify the user instead of email * ✨ added well-known url for oidc config discovery * 🔥 remove default values for required oidc variables * 🎨 added response.raise_for_status() * 🎨 format * 🎨 add mozilla_django_oidc.middleware.SessionRefresh in condition * ✨ 增加OIDC登入功能 * ✨ added init_user and use username to identify the user instead of email * ✨ added well-known url for oidc config discovery * 🔥 remove default values for required oidc variables * 🎨 added response.raise_for_status() * 🎨 format * 🎨 add mozilla_django_oidc.middleware.SessionRefresh in condition * 🎨 conditional import mozilla_django_oidc Co-authored-by: Leo Q <LeoQuote@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from mozilla_django_oidc import auth | ||
from django.core.exceptions import SuspiciousOperation | ||
from common.auth import init_user | ||
|
||
|
||
class OIDCAuthenticationBackend(auth.OIDCAuthenticationBackend): | ||
def create_user(self, claims): | ||
"""Return object for a newly created user account.""" | ||
email = claims.get("email") | ||
username = claims.get("preferred_username") | ||
display = claims.get("name") | ||
if not email or not username or not display: | ||
raise SuspiciousOperation( | ||
"email and name and preferred_username should not be empty" | ||
) | ||
user = self.UserModel.objects.create_user( | ||
username, email=email, display=display | ||
) | ||
init_user(user) | ||
return user | ||
|
||
def describe_user_by_claims(self, claims): | ||
username = claims.get("preferred_username") | ||
return "username {}".format(username) | ||
|
||
def filter_users_by_claims(self, claims): | ||
"""Return all users matching the username.""" | ||
username = claims.get("preferred_username") | ||
if not username or username == "admin": | ||
return self.UserModel.objects.none() | ||
return self.UserModel.objects.filter(username__iexact=username) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters