Merge pull request #3354 from cmintey/signing-algorithm-env-var
Add OIDC environment variable for specififying the signing algorithm
This commit is contained in:
5 files changed
+26
-8
No files matched your search
@@ -46,6 +46,14 @@ Before you can start using OIDC Authentication, you must first configure a new c
|
||||
|
||||
Take the client id and your discovery URL and update your environment variables to include the required OIDC variables described in [Installation - Backend Configuration](../installation/backend-config.md#openid-connect-oidc).
|
||||
|
||||
### Groups
|
||||
|
||||
There are two (optional) [environment variables](../installation/backend-config.md#openid-connect-oidc) that can control which of the users in your IdP can log in to Mealie and what permissions they will have. The groups should be **defined in your IdP** and be returned in the `groups` claim.
|
||||
|
||||
`OIDC_USER_GROUP`: Users must be a part of this group (within your IdP) to be able to log in.
|
||||
|
||||
`OIDC_ADMIN_GROUP`: Users that are in this group (within your IdP) will be made an **admin** in Mealie.
|
||||
|
||||
## Examples
|
||||
|
||||
Example configurations for several Identity Providers have been provided by the Community in the [GitHub Discussions](https://github.com/mealie-recipes/mealie/discussions/categories/oauth-provider-example).
|
||||
|
||||
@@ -85,11 +85,12 @@ For usage, see [Usage - OpenID Connect](../authentication/oidc.md)
|
||||
| OIDC_SIGNUP_ENABLED | True | Enables new users to be created when signing in for the first time with OIDC |
|
||||
| OIDC_CONFIGURATION_URL | None | The URL to the OIDC configuration of your provider. This is usually something like https://auth.example.com/.well-known/openid-configuration |
|
||||
| OIDC_CLIENT_ID | None | The client id of your configured client in your provider |
|
||||
| OIDC_USER_GROUP| None | If specified, only users belonging to this group will be able to successfully authenticate, regardless of the OIDC_ADMIN_GROUP |
|
||||
| OIDC_ADMIN_GROUP | None | If specified, users belonging to this group will be made an admin |
|
||||
| OIDC_USER_GROUP| None | If specified, only users belonging to this group will be able to successfully authenticate, regardless of the `OIDC_ADMIN_GROUP`. For more information see [this page](../authentication/oidc.md#groups) |
|
||||
| OIDC_ADMIN_GROUP | None | If specified, users belonging to this group will be made an admin. For more information see [this page](../authentication/oidc.md#groups) |
|
||||
| OIDC_AUTO_REDIRECT | False | If `True`, then the login page will be bypassed an you will be sent directly to your Identity Provider. You can still get to the login page by adding `?direct=1` to the login URL |
|
||||
| OIDC_PROVIDER_NAME | OAuth | The provider name is shown in SSO login button. "Login with <OIDC_PROVIDER_NAME\>" |
|
||||
| OIDC_REMEMBER_ME | False | Because redirects bypass the login screen, you cant extend your session by clicking the "Remember Me" checkbox. By setting this value to true, a session will be extended as if "Remember Me" was checked |
|
||||
| OIDC_SIGNING_ALGORITHM | RS256 | The algorithm used to sign the id token (examples: RS256, HS256) |
|
||||
|
||||
### Themeing
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
<div id="redoc-container"></div>
|
||||
<script src="https://cdn.jsdelivr.net/npm/redoc/bundles/redoc.standalone.js"> </script>
|
||||
<script>
|
||||
var spec = {"openapi": "3.1.0", "info": {"title": "Mealie", "description": "\nMealie is a web application for managing your recipes, meal plans, and shopping lists. This is the Restful\nAPI interactive documentation that can be used to explore the API. If you're justing getting started with\nthe API and want to get started quickly, you can use the\n[API Usage | Mealie Docs](https://nightly.mealie.io/documentation/getting-started/api-usage/)\nas a reference for how to get started.\n\n\nAs of this release <b>develop</b>, Mealie is still in rapid development and therefore some of these APIs may\nchange from version to version.\n\n\nIf you have any questions or comments about mealie, please use the discord server to talk to the developers or other\ncommunity members. If you'd like to file an issue, please use the\n[GitHub Issue Tracker | Mealie](https://github.com/mealie-recipes/mealie/issues/new/choose)\n\n\n## Helpful Links\n- [Home Page](https://mealie.io)\n- [Documentation](https://nightly.mealie.io)\n- [Discord](https://discord.gg/QuStdQGSGK)\n- [Demo](https://demo.mealie.io)\n- [Beta](https://demo.mealie.io)\n", "version": "develop"}, "paths": {"/api/app/about": {"get": {"tags": ["App: About"], "summary": "Get App Info", "description": "Get general application information", "operationId": "get_app_info_api_app_about_get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/AppInfo"}}}}}}}, "/api/app/about/startup-info": {"get": {"tags": ["App: About"], "summary": "Get Startup Info", "description": "returns helpful startup information", "operationId": "get_startup_info_api_app_about_startup_info_get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/AppStartupInfo"}}}}}}}, "/api/app/about/theme": {"get": {"tags": ["App: About"], "summary": "Get App Theme", "description": "Get's the current theme settings", "operationId": "get_app_theme_api_app_about_theme_get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/AppTheme"}}}}}}}, "/api/app/about/oidc": {"get": {"tags": ["App: About"], "summary": "Get Oidc Info", "description": "Get's the current OIDC configuration needed for the frontend", "operationId": "get_oidc_info_api_app_about_oidc_get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/OIDCInfo"}}}}}}}, "/api/auth/token": {"post": {"tags": ["Users: Authentication"], "summary": "Get Token", "operationId": "get_token_api_auth_token_post", "requestBody": {"content": {"application/x-www-form-urlencoded": {"schema": {"allOf": [{"$ref": "#/components/schemas/Body_get_token_api_auth_token_post"}], "title": "Body"}}}}, "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {}}}}, "422": {"description": "Validation Error", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/HTTPValidationError"}}}}}}}, "/api/auth/refresh": {"get": {"tags": ["Users: Authentication"], "summary": "Refresh Token", "description": "Use a valid token to get another token", "operationId": "refresh_token_api_auth_refresh_get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {}}}}}, "security": [{"OAuth2PasswordBearer": []}]}}, "/api/users/register": {"post": {"tags": ["Users: Registration"], "summary": "Register New User", "operationId": "register_new_user_api_users_register_post", "parameters": [{"name": "group_id", "in": "query", "required": false, "schema": {"anyOf": [{"type": "string", "format": "uuid4"}, {"type": "null"}], "title": "Group Id"}}, {"name": "accept-language", "in": "header", "required": false, "schema": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Accept-Language"}}], "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/CreateUserRegistration"}}}}, "responses": {"201": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/UserOut"}}}}, "422": {"description": "Validation Error", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/HTTPValidationError"}}}}}}}, "/api/users/group-users": {"get": {"tags": ["Users: CRUD"], "summary": "Get All Group Users", "description": "Returns all users from the current group", "operationId": "get_all_group_users_api_users_group_users_get", "security": [{"OAuth2PasswordBearer": []}, {"OAuth2PasswordBearer": []}], "parameters": [{"name": "page", "in": "query", "required": false, "schema": {"type": "integer", "default": 1, "title": "Page"}}, {"name": "perPage", "in": "query", "required": false, "schema": {"type": "integer", "default": 50, "title": "Perpage"}}, {"name": "orderBy", "in":Line truncated
|
||||
var spec = {"openapi": "3.1.0", "info": {"title": "Mealie", "description": "\nMealie is a web application for managing your recipes, meal plans, and shopping lists. This is the Restful\nAPI interactive documentation that can be used to explore the API. If you're justing getting started with\nthe API and want to get started quickly, you can use the\n[API Usage | Mealie Docs](https://nightly.mealie.io/documentation/getting-started/api-usage/)\nas a reference for how to get started.\n\n\nAs of this release <b>develop</b>, Mealie is still in rapid development and therefore some of these APIs may\nchange from version to version.\n\n\nIf you have any questions or comments about mealie, please use the discord server to talk to the developers or other\ncommunity members. If you'd like to file an issue, please use the\n[GitHub Issue Tracker | Mealie](https://github.com/mealie-recipes/mealie/issues/new/choose)\n\n\n## Helpful Links\n- [Home Page](https://mealie.io)\n- [Documentation](https://nightly.mealie.io)\n- [Discord](https://discord.gg/QuStdQGSGK)\n- [Demo](https://demo.mealie.io)\n- [Beta](https://demo.mealie.io)\n", "version": "develop"}, "paths": {"/api/app/about": {"get": {"tags": ["App: About"], "summary": "Get App Info", "description": "Get general application information", "operationId": "get_app_info_api_app_about_get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/AppInfo"}}}}}}}, "/api/app/about/startup-info": {"get": {"tags": ["App: About"], "summary": "Get Startup Info", "description": "returns helpful startup information", "operationId": "get_startup_info_api_app_about_startup_info_get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/AppStartupInfo"}}}}}}}, "/api/app/about/theme": {"get": {"tags": ["App: About"], "summary": "Get App Theme", "description": "Get's the current theme settings", "operationId": "get_app_theme_api_app_about_theme_get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/AppTheme"}}}}}}}, "/api/app/about/oidc": {"get": {"tags": ["App: About"], "summary": "Get Oidc Info", "description": "Get's the current OIDC configuration needed for the frontend", "operationId": "get_oidc_info_api_app_about_oidc_get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/OIDCInfo"}}}}}}}, "/api/auth/token": {"post": {"tags": ["Users: Authentication"], "summary": "Get Token", "operationId": "get_token_api_auth_token_post", "requestBody": {"content": {"application/x-www-form-urlencoded": {"schema": {"allOf": [{"$ref": "#/components/schemas/Body_get_token_api_auth_token_post"}], "title": "Body"}}}}, "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {}}}}, "422": {"description": "Validation Error", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/HTTPValidationError"}}}}}}}, "/api/auth/refresh": {"get": {"tags": ["Users: Authentication"], "summary": "Refresh Token", "description": "Use a valid token to get another token", "operationId": "refresh_token_api_auth_refresh_get", "responses": {"200": {"description": "Successful Response", "content": {"application/json": {"schema": {}}}}}, "security": [{"OAuth2PasswordBearer": []}]}}, "/api/users/register": {"post": {"tags": ["Users: Registration"], "summary": "Register New User", "operationId": "register_new_user_api_users_register_post", "parameters": [{"name": "group_id", "in": "query", "required": false, "schema": {"anyOf": [{"type": "string", "format": "uuid4"}, {"type": "null"}], "title": "Group Id"}}, {"name": "accept-language", "in": "header", "required": false, "schema": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Accept-Language"}}], "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/CreateUserRegistration"}}}}, "responses": {"201": {"description": "Successful Response", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/UserOut"}}}}, "422": {"description": "Validation Error", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/HTTPValidationError"}}}}}}}, "/api/users/group-users": {"get": {"tags": ["Users: CRUD"], "summary": "Get All Group Users", "description": "Returns all users from the current group", "operationId": "get_all_group_users_api_users_group_users_get", "security": [{"OAuth2PasswordBearer": []}, {"OAuth2PasswordBearer": []}], "parameters": [{"name": "page", "in": "query", "required": false, "schema": {"type": "integer", "default": 1, "title": "Page"}}, {"name": "perPage", "in": "query", "required": false, "schema": {"type": "integer", "default": 50, "title": "Perpage"}}, {"name": "orderBy", "in":Line truncated
|
||||
Redoc.init(spec, {}, document.getElementById("redoc-container"));
|
||||
</script>
|
||||
|
||||
|
||||
@@ -3,13 +3,14 @@ from functools import lru_cache
|
||||
|
||||
import requests
|
||||
from authlib.jose import JsonWebKey, JsonWebToken, JWTClaims, KeySet
|
||||
from authlib.jose.errors import ExpiredTokenError
|
||||
from authlib.jose.errors import ExpiredTokenError, UnsupportedAlgorithmError
|
||||
from authlib.oidc.core import CodeIDToken
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
from mealie.core import root_logger
|
||||
from mealie.core.config import get_app_settings
|
||||
from mealie.core.security.providers.auth_provider import AuthProvider
|
||||
from mealie.core.settings.settings import AppSettings
|
||||
from mealie.db.models.users.users import AuthMethod
|
||||
from mealie.repos.all_repositories import get_repositories
|
||||
from mealie.schema.user.auth import OIDCRequest
|
||||
@@ -26,11 +27,11 @@ class OpenIDProvider(AuthProvider[OIDCRequest]):
|
||||
async def authenticate(self) -> tuple[str, timedelta] | None:
|
||||
"""Attempt to authenticate a user given a username and password"""
|
||||
|
||||
claims = self.get_claims()
|
||||
settings = get_app_settings()
|
||||
claims = self.get_claims(settings)
|
||||
if not claims:
|
||||
return None
|
||||
|
||||
settings = get_app_settings()
|
||||
repos = get_repositories(self.session)
|
||||
|
||||
user = self.try_get_user(claims.get("email"))
|
||||
@@ -76,13 +77,20 @@ class OpenIDProvider(AuthProvider[OIDCRequest]):
|
||||
self._logger.info("[OIDC] Found user but their AuthMethod does not match OIDC")
|
||||
return None
|
||||
|
||||
def get_claims(self) -> JWTClaims | None:
|
||||
def get_claims(self, settings: AppSettings) -> JWTClaims | None:
|
||||
"""Get the claims from the ID token and check if the required claims are present"""
|
||||
required_claims = {"preferred_username", "name", "email"}
|
||||
jwks = OpenIDProvider.get_jwks()
|
||||
if not jwks:
|
||||
return None
|
||||
claims = JsonWebToken(["RS256"]).decode(s=self.data.id_token, key=jwks, claims_cls=CodeIDToken)
|
||||
|
||||
algorithm = settings.OIDC_SIGNING_ALGORITHM
|
||||
try:
|
||||
claims = JsonWebToken([algorithm]).decode(s=self.data.id_token, key=jwks, claims_cls=CodeIDToken)
|
||||
except UnsupportedAlgorithmError:
|
||||
self._logger.error(
|
||||
f"[OIDC] Unsupported algorithm '{algorithm}'. Unable to decode id token due to mismatched algorithm."
|
||||
)
|
||||
|
||||
try:
|
||||
claims.validate()
|
||||
|
||||
@@ -182,6 +182,7 @@ class AppSettings(BaseSettings):
|
||||
OIDC_AUTO_REDIRECT: bool = False
|
||||
OIDC_PROVIDER_NAME: str = "OAuth"
|
||||
OIDC_REMEMBER_ME: bool = False
|
||||
OIDC_SIGNING_ALGORITHM: str = "RS256"
|
||||
|
||||
@property
|
||||
def OIDC_READY(self) -> bool:
|
||||
|
||||
Reference in new issue
Block a user