-
Notifications
You must be signed in to change notification settings - Fork 486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API OIDC authentication mechanism #10905
Open
ErykKul
wants to merge
17
commits into
develop
Choose a base branch
from
authn-arch-api-oidc
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
81d0ee0
api oidc authentication mechanism
ErykKul 96fab76
replaced tabs with spaces
ErykKul cbde18f
better error handling for not authenticated users
ErykKul b137bbc
Update docker-compose-dev.yml
ErykKul e94ebc4
changed the demo user to admin and better error when user is not foun…
ErykKul 2a2c583
admin user email fix
ErykKul b7937e6
restored kaycloak config to original
ErykKul d1120f8
restored bearer token config
ErykKul eea06bd
removed unused import
ErykKul 1a5cc5d
simplified config
ErykKul 5c1dc24
improved implementation with exposed tokens, no unverified emails blo…
ErykKul 575d653
only verified email can be used to log in
ErykKul eea3b5c
fixed email verified check
ErykKul 0703a65
oidc JSF log in
ErykKul ff8bb52
bearer token and oidc provider refactoring to use the new payara mech…
ErykKul 61703e8
fixed log in issues
ErykKul 004613f
redirect to first log in page and user lookup by user record identifier
ErykKul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -398,7 +398,7 @@ | |
"emailVerified" : true, | ||
"firstName" : "Dataverse", | ||
"lastName" : "Admin", | ||
"email" : "dataverse-admin@mailinator.com", | ||
"email" : "[email protected]", | ||
"credentials" : [ { | ||
"id" : "28f1ece7-26fb-40f1-9174-5ffce7b85c0a", | ||
"type" : "password", | ||
|
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
34 changes: 34 additions & 0 deletions
34
src/main/java/edu/harvard/iq/dataverse/api/OpenIDAuthentication.java
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,34 @@ | ||
package edu.harvard.iq.dataverse.api; | ||
|
||
import java.io.IOException; | ||
|
||
import fish.payara.security.annotations.OpenIdAuthenticationDefinition; | ||
import jakarta.annotation.security.DeclareRoles; | ||
import jakarta.ejb.EJB; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.annotation.HttpConstraint; | ||
import jakarta.servlet.annotation.ServletSecurity; | ||
import jakarta.servlet.annotation.WebServlet; | ||
import jakarta.servlet.http.HttpServlet; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@WebServlet("/oidc/login") | ||
@OpenIdAuthenticationDefinition( | ||
providerURI="#{openIdConfigBean.providerURI}", | ||
clientId="#{openIdConfigBean.clientId}", | ||
clientSecret="#{openIdConfigBean.clientSecret}", | ||
redirectURI="#{openIdConfigBean.redirectURI}", | ||
scope="email" | ||
) | ||
@DeclareRoles("all") | ||
@ServletSecurity(@HttpConstraint(rolesAllowed = "all")) | ||
public class OpenIDAuthentication extends HttpServlet { | ||
@EJB | ||
OpenIDConfigBean openIdConfigBean; | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||
response.getWriter().println("..."); | ||
ErykKul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/edu/harvard/iq/dataverse/api/OpenIDCallback.java
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,49 @@ | ||
package edu.harvard.iq.dataverse.api; | ||
|
||
import static edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder.jsonObjectBuilder; | ||
import edu.harvard.iq.dataverse.authorization.AuthenticationServiceBean; | ||
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser; | ||
import fish.payara.security.openid.api.OpenIdContext; | ||
import jakarta.ejb.Stateless; | ||
import jakarta.inject.Inject; | ||
import jakarta.json.JsonObjectBuilder; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
@Stateless | ||
@Path("callback") | ||
public class OpenIDCallback extends AbstractApiBean { | ||
@Inject | ||
OpenIdContext openIdContext; | ||
|
||
@Inject | ||
protected AuthenticationServiceBean authSvc; | ||
|
||
@Path("token") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docs please! (API Guide.) |
||
@GET | ||
public Response token(@Context ContainerRequestContext crc) { | ||
return Response.seeOther(crc.getUriInfo().getBaseUri().resolve("callback/session")).build(); | ||
} | ||
|
||
@Path("session") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docs here too, please. |
||
@GET | ||
public Response session(@Context ContainerRequestContext crc) { | ||
try { | ||
final String email = openIdContext.getAccessToken().getJwtClaims().getStringClaim("email").orElse(null); | ||
final AuthenticatedUser authUser = authSvc.getAuthenticatedUserByEmail(email); | ||
if (authUser != null) { | ||
return ok( | ||
jsonObjectBuilder() | ||
.add("user", authUser.toJson()) | ||
.add("session", crc.getCookies().get("JSESSIONID").getValue())); | ||
} else { | ||
return notFound("user with email " + email + " not found"); | ||
} | ||
} catch (final Exception ignore) { | ||
return authenticatedUserRequired(); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/edu/harvard/iq/dataverse/api/OpenIDConfigBean.java
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,25 @@ | ||
package edu.harvard.iq.dataverse.api; | ||
|
||
import edu.harvard.iq.dataverse.settings.JvmSettings; | ||
import jakarta.ejb.Stateless; | ||
import jakarta.inject.Named; | ||
|
||
@Stateless | ||
@Named("openIdConfigBean") | ||
public class OpenIDConfigBean implements java.io.Serializable { | ||
public String getProviderURI() { | ||
return JvmSettings.API_OIDC_PROVIDER_URI.lookup(); | ||
} | ||
|
||
public String getClientId() { | ||
return JvmSettings.API_OIDC_CLIENT_ID.lookup(); | ||
} | ||
|
||
public String getClientSecret() { | ||
return JvmSettings.API_OIDC_CLIENT_SECRET.lookup(); | ||
} | ||
|
||
public String getRedirectURI() { | ||
return JvmSettings.API_OIDC_REDIRECT_URI.lookup(); | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please remind me why this change is necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We talked about matching users from OIDC to Dataverse users multiple times at LIBIS. We feel like matching users by verified emails would be the best approach. The user with "[email protected]" does not exist in the dev env, so it did not work. Matching by email would mean that you can log in with google, facebook, whatever, as long as you use the same email.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reverted it and implemented the regular UserRecordIdentifier lookup, however, user is not known:
I have also implemented the redirect to the first login page if user is not known:
After that, I can log in:
Is it as it supposed to be? I have also noticed that oauth tokens are stored in the DB. The side effect of creating a new user after OIDC authentication is that it inserts one token there...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I can just pass null as token data to prevent storing it in DB. I am not sure why we store them, I assume it is like the cache for bearer tokens? Something to investigate a little bit more.