-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow multipe auth after unification
- Loading branch information
Showing
17 changed files
with
276 additions
and
212 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,9 +108,10 @@ $ curl --user [email protected]:PASSWORD http://localhost:8100/whoami | |
|
||
Ralph LRS API server supports OpenID Connect (OIDC) on top of OAuth 2.0 for authentication and authorization. | ||
|
||
To enable OIDC auth, you should set the `RALPH_RUNSERVER_AUTH_BACKEND` environment variable as follows: | ||
|
||
To enable OIDC auth, you should modify the `RALPH_RUNSERVER_AUTH_BACKENDS` environment variable by adding (or replacing) `oidc`: | ||
```bash | ||
RALPH_RUNSERVER_AUTH_BACKEND=oidc | ||
RALPH_RUNSERVER_AUTH_BACKENDS=basic,oidc | ||
``` | ||
and you should define the `RALPH_RUNSERVER_AUTH_OIDC_ISSUER_URI` environment variable with your identity provider's Issuer Identifier URI as follows: | ||
```bash | ||
|
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 |
---|---|---|
@@ -1,11 +1,48 @@ | ||
"""Main module for Ralph's LRS API authentication.""" | ||
|
||
from fastapi import Depends, HTTPException, status | ||
from fastapi.security import SecurityScopes | ||
|
||
from ralph.api.auth.basic import get_basic_auth_user | ||
from ralph.api.auth.oidc import get_oidc_user | ||
from ralph.conf import settings | ||
from ralph.conf import AuthBackend, settings | ||
|
||
|
||
def get_authenticated_user( | ||
security_scopes: SecurityScopes = SecurityScopes([]), | ||
basic_auth_user=Depends(get_basic_auth_user), | ||
oidc_auth_user=Depends(get_oidc_user), | ||
): | ||
"""Authenticate user with any allowed method, using credentials in the header.""" | ||
if AuthBackend.BASIC not in settings.RUNSERVER_AUTH_BACKENDS: | ||
basic_auth_user = None | ||
if AuthBackend.OIDC not in settings.RUNSERVER_AUTH_BACKENDS: | ||
oidc_auth_user = None | ||
|
||
if basic_auth_user is not None: | ||
user = basic_auth_user | ||
auth_method = "Basic" | ||
elif oidc_auth_user is not None: | ||
user = oidc_auth_user | ||
auth_method = "Bearer" | ||
else: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Invalid authentication credentials", | ||
headers={ | ||
"WWW-Authenticate": ",".join( | ||
[val.value for val in settings.RUNSERVER_AUTH_BACKENDS] | ||
) | ||
}, | ||
) | ||
|
||
# At startup, select the authentication mode that will be used | ||
if settings.RUNSERVER_AUTH_BACKEND == settings.AuthBackends.OIDC: | ||
get_authenticated_user = get_oidc_user | ||
else: | ||
get_authenticated_user = get_basic_auth_user | ||
# Restrict access by scopes | ||
if settings.LRS_RESTRICT_BY_SCOPES: | ||
for requested_scope in security_scopes.scopes: | ||
if not user.scopes.is_authorized(requested_scope): | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail=f'Access not authorized to scope: "{requested_scope}".', | ||
headers={"WWW-Authenticate": auth_method}, | ||
) | ||
return user |
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
Oops, something went wrong.