|
65 | 65 |
|
66 | 66 |
|
67 | 67 | class OAuth2Validator(RequestValidator): |
| 68 | + # Return the given claim only if the given scope is present. |
| 69 | + # Extended as needed for non-standard OIDC claims/scopes. |
| 70 | + # Override by setting to None to ignore scopes. |
| 71 | + # see https://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims |
| 72 | + # For example, for the "nickname" claim, you need the "profile" scope. |
| 73 | + oidc_claim_scope = { |
| 74 | + "sub": "openid", |
| 75 | + "name": "profile", |
| 76 | + "family_name": "profile", |
| 77 | + "given_name": "profile", |
| 78 | + "middle_name": "profile", |
| 79 | + "nickname": "profile", |
| 80 | + "preferred_username": "profile", |
| 81 | + "profile": "profile", |
| 82 | + "picture": "profile", |
| 83 | + "website": "profile", |
| 84 | + "gender": "profile", |
| 85 | + "birthdate": "profile", |
| 86 | + "zoneinfo": "profile", |
| 87 | + "locale": "profile", |
| 88 | + "updated_at": "profile", |
| 89 | + "email": "email", |
| 90 | + "email_versified": "email", |
| 91 | + "address": "address", |
| 92 | + "phone_number": "phone", |
| 93 | + "phone_number_verified": "phone", |
| 94 | + } |
| 95 | + |
68 | 96 | def _extract_basic_auth(self, request): |
69 | 97 | """ |
70 | 98 | Return authentication string if request contains basic auth credentials, |
@@ -760,7 +788,10 @@ def get_oidc_claims(self, token, token_handler, request): |
760 | 788 | claims = {} |
761 | 789 |
|
762 | 790 | for k, v in data.items(): |
763 | | - claims[k] = v(request) if callable(v) else v |
| 791 | + if not self.oidc_claim_scope or ( |
| 792 | + k in self.oidc_claim_scope and self.oidc_claim_scope[k] in token.scopes |
| 793 | + ): |
| 794 | + claims[k] = v(request) if callable(v) else v |
764 | 795 | return claims |
765 | 796 |
|
766 | 797 | def get_id_token_dictionary(self, token, token_handler, request): |
|
0 commit comments