-
Notifications
You must be signed in to change notification settings - Fork 92
Implement oidc #628
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
Open
Jeidnx
wants to merge
25
commits into
TeamPiped:master
Choose a base branch
from
Jeidnx:oidc
base: master
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
Implement oidc #628
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
604fa65
Implement oidc
375ee58
Better Error handling for oidc config
143711c
Save redirect in state
18d9317
Show warning message before oidc login
f4b9dff
Only show warning when not redirecting to configured frontend
53d9b9d
Merge branch 'master' into oidc
97889f3
Merge remote-tracking branch 'origin/master' into oidc
FireMasterK 847f80c
Simplify config handling.
FireMasterK 946ac45
Add missing newline.
FireMasterK 0eb2351
Format all code.
FireMasterK 9b7246a
Merge branch 'master' into oidc
Jeidnx e7f2187
Implement account deletion and cleanup some code
c1fde37
Refactor oidc logic into UserHandlers
024435f
Add database migration for username length change.
FireMasterK 470efd8
Revert "Add database migration for username length change."
5f6a83a
Add code from the meeting.
FireMasterK 868103c
Merge branch 'master' into oidc
074e4bc
chore: properly implement oidc
580eb7f
Simplify oidc hash generation.
FireMasterK 9520a3c
Simplify error handling code a little.
FireMasterK b0725f8
Remove debug code and format.
FireMasterK 74a6751
Move OidcData to db + some cleanup
f76f8e0
randomize username
e4ba195
add redirect to oidc delete; more cleanup
77cd736
explicitly reject empty hashes
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,9 @@ | |||||||||||||||||||
|
|
||||||||||||||||||||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||||||||||||||||||||
| import com.fasterxml.jackson.databind.JsonNode; | ||||||||||||||||||||
| import com.nimbusds.oauth2.sdk.auth.ClientAuthentication; | ||||||||||||||||||||
| import com.nimbusds.oauth2.sdk.auth.ClientSecretBasic; | ||||||||||||||||||||
| import com.nimbusds.openid.connect.sdk.claims.UserInfo; | ||||||||||||||||||||
| import com.rometools.rome.feed.synd.SyndFeed; | ||||||||||||||||||||
| import com.rometools.rome.io.SyndFeedInput; | ||||||||||||||||||||
| import io.activej.config.Config; | ||||||||||||||||||||
|
|
@@ -19,7 +22,9 @@ | |||||||||||||||||||
| import me.kavin.piped.server.handlers.auth.StorageHandlers; | ||||||||||||||||||||
| import me.kavin.piped.server.handlers.auth.UserHandlers; | ||||||||||||||||||||
| import me.kavin.piped.utils.*; | ||||||||||||||||||||
| import me.kavin.piped.utils.ErrorResponse; | ||||||||||||||||||||
| import me.kavin.piped.utils.obj.MatrixHelper; | ||||||||||||||||||||
| import me.kavin.piped.utils.obj.OidcProvider; | ||||||||||||||||||||
| import me.kavin.piped.utils.obj.federation.FederatedVideoInfo; | ||||||||||||||||||||
| import me.kavin.piped.utils.resp.*; | ||||||||||||||||||||
| import org.apache.commons.lang3.StringUtils; | ||||||||||||||||||||
|
|
@@ -30,12 +35,18 @@ | |||||||||||||||||||
| import org.schabi.newpipe.extractor.exceptions.ParsingException; | ||||||||||||||||||||
| import org.schabi.newpipe.extractor.localization.DateWrapper; | ||||||||||||||||||||
| import org.xml.sax.InputSource; | ||||||||||||||||||||
| import com.nimbusds.oauth2.sdk.*; | ||||||||||||||||||||
| import com.nimbusds.openid.connect.sdk.*; | ||||||||||||||||||||
| import com.nimbusds.oauth2.sdk.id.*; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| import java.io.ByteArrayInputStream; | ||||||||||||||||||||
| import java.net.InetSocketAddress; | ||||||||||||||||||||
| import java.net.URI; | ||||||||||||||||||||
| import java.util.LinkedList; | ||||||||||||||||||||
| import java.util.Objects; | ||||||||||||||||||||
| import java.util.concurrent.Executor; | ||||||||||||||||||||
| import java.util.concurrent.TimeUnit; | ||||||||||||||||||||
| import java.util.regex.Pattern; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress; | ||||||||||||||||||||
| import static io.activej.http.HttpHeaders.*; | ||||||||||||||||||||
|
|
@@ -293,6 +304,88 @@ AsyncServlet mainServlet(Executor executor) { | |||||||||||||||||||
| LoginRequest.class); | ||||||||||||||||||||
| return getJsonResponse(UserHandlers.registerResponse(body.username, body.password), | ||||||||||||||||||||
| "private"); | ||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||
| return getErrorResponse(e, request.getPath()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| })).map(GET, "/oidc/:provider/:function", AsyncServlet.ofBlocking(executor, request -> { | ||||||||||||||||||||
| try { | ||||||||||||||||||||
| String function = request.getPathParameter("function"); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| OidcProvider provider = findOidcProvider(request.getPathParameter("provider"), Constants.OIDC_PROVIDERS); | ||||||||||||||||||||
| if(provider == null) | ||||||||||||||||||||
| return HttpResponse.ofCode(500).withHtml("Can't find the provider on the server."); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| URI callback = new URI(Constants.PUBLIC_URL + "/oidc/" + provider.name + "/callback"); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| switch (function) { | ||||||||||||||||||||
| case "login" -> { | ||||||||||||||||||||
|
|
||||||||||||||||||||
| State state = new State(); | ||||||||||||||||||||
| Nonce nonce = new Nonce(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| AuthenticationRequest oidcRequest = new AuthenticationRequest.Builder( | ||||||||||||||||||||
| new ResponseType("code"), | ||||||||||||||||||||
| new Scope("openid"), | ||||||||||||||||||||
| provider.clientID, | ||||||||||||||||||||
| callback) | ||||||||||||||||||||
| .endpointURI(provider.authUri) | ||||||||||||||||||||
| .state(state) | ||||||||||||||||||||
| .nonce(nonce) | ||||||||||||||||||||
| .build(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return HttpResponse.redirect302(oidcRequest.toURI().toString()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| case "callback" -> { | ||||||||||||||||||||
| ClientAuthentication clientAuth = new ClientSecretBasic(provider.clientID, provider.clientSecret); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| AuthenticationResponse response = AuthenticationResponseParser.parse( | ||||||||||||||||||||
| URI.create(request.getFullUrl()) | ||||||||||||||||||||
| ); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (response instanceof AuthenticationErrorResponse) { | ||||||||||||||||||||
| // The OpenID provider returned an error | ||||||||||||||||||||
| System.err.println(response.toErrorResponse().getErrorObject()); | ||||||||||||||||||||
| return HttpResponse.ofCode(500).withHtml("OpenID provider returned an error:\n\n" + response.toErrorResponse().getErrorObject().toString()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| AuthenticationSuccessResponse sr = response.toSuccessResponse(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| AuthorizationCode code = sr.getAuthorizationCode(); | ||||||||||||||||||||
| AuthorizationGrant codeGrant = new AuthorizationCodeGrant( | ||||||||||||||||||||
| code, callback | ||||||||||||||||||||
| ); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| TokenRequest tr = new TokenRequest(provider.tokenUri, clientAuth, codeGrant); | ||||||||||||||||||||
| TokenResponse tokenResponse = OIDCTokenResponseParser.parse(tr.toHTTPRequest().send()); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (! tokenResponse.indicatesSuccess()) { | ||||||||||||||||||||
| TokenErrorResponse errorResponse = tokenResponse.toErrorResponse(); | ||||||||||||||||||||
| return HttpResponse.ofCode(500).withHtml("Failure while trying to request token:\n\n" + errorResponse.getErrorObject().getDescription()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| OIDCTokenResponse successResponse = (OIDCTokenResponse)tokenResponse.toSuccessResponse(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| UserInfoRequest ur = new UserInfoRequest(provider.userinfoUri, successResponse.getOIDCTokens().getBearerAccessToken()); | ||||||||||||||||||||
| UserInfoResponse userInfoResponse = UserInfoResponse.parse(ur.toHTTPRequest().send()); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (! userInfoResponse.indicatesSuccess()) { | ||||||||||||||||||||
| System.out.println(userInfoResponse.toErrorResponse().getErrorObject().getCode()); | ||||||||||||||||||||
| System.out.println(userInfoResponse.toErrorResponse().getErrorObject().getDescription()); | ||||||||||||||||||||
| return HttpResponse.ofCode(500).withHtml("Failed to query userInfo:\n\n" + userInfoResponse.toErrorResponse().getErrorObject().getDescription()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| UserInfo userInfo = userInfoResponse.toSuccessResponse().getUserInfo(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| String sessionId = UserHandlers.oidcCallbackResponse(provider.name, userInfo.getSubject().toString()); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return HttpResponse.redirect302(Constants.FRONTEND_URL + "/login?session=" + sessionId); | ||||||||||||||||||||
|
Jeidnx marked this conversation as resolved.
Outdated
|
||||||||||||||||||||
| } | ||||||||||||||||||||
| default -> { | ||||||||||||||||||||
| return HttpResponse.ofCode(500).withHtml("Invalid function `" + function + "`."); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||
| return getErrorResponse(e, request.getPath()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -542,6 +635,14 @@ AsyncServlet mainServlet(Executor executor) { | |||||||||||||||||||
| return new CustomServletDecorator(router); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private static OidcProvider findOidcProvider(String provider, LinkedList<OidcProvider> list){ | ||||||||||||||||||||
| for(int i = 0; i < list.size(); i++) { | ||||||||||||||||||||
| OidcProvider curr = list.get(i); | ||||||||||||||||||||
| if(curr == null || !curr.name.equals(provider)) continue; | ||||||||||||||||||||
| return curr; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+537
to
+541
Member
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.
Suggested change
|
||||||||||||||||||||
| return null; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| private static String[] getArray(String s) { | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (s == null) { | ||||||||||||||||||||
|
|
||||||||||||||||||||
This file contains hidden or 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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -107,11 +107,36 @@ public static byte[] loginResponse(String user, String pass) | |||||||||||||||
| return null; | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| public static String oidcCallbackResponse(String provider, String uid) { | ||||||||||||||||
| try (Session s = DatabaseSessionFactory.createSession()) { | ||||||||||||||||
| String dbName = provider + "-" + uid; | ||||||||||||||||
| System.out.println(dbName); //TODO: | ||||||||||||||||
| CriteriaBuilder cb = s.getCriteriaBuilder(); | ||||||||||||||||
| CriteriaQuery<User> cr = cb.createQuery(User.class); | ||||||||||||||||
| Root<User> root = cr.from(User.class); | ||||||||||||||||
| cr.select(root).where(root.get("username").in( | ||||||||||||||||
| dbName | ||||||||||||||||
| )); | ||||||||||||||||
|
|
||||||||||||||||
| public static byte[] deleteUserResponse(String session, String pass) throws IOException { | ||||||||||||||||
| User dbuser = s.createQuery(cr).uniqueResult(); | ||||||||||||||||
|
|
||||||||||||||||
| if (dbuser == null) { | ||||||||||||||||
| User newuser = new User(dbName, "", Set.of()); | ||||||||||||||||
|
|
||||||||||||||||
| var tr = s.beginTransaction(); | ||||||||||||||||
| s.persist(newuser); | ||||||||||||||||
| tr.commit(); | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| return newuser.getSessionId(); | ||||||||||||||||
| } | ||||||||||||||||
| return dbuser.getSessionId(); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+283
to
+288
Member
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.
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| if (StringUtils.isBlank(session) || StringUtils.isBlank(pass)) | ||||||||||||||||
| ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session and password are required parameters")); | ||||||||||||||||
| } | ||||||||||||||||
| public static byte[] deleteUserResponse(String session, String pass) throws IOException { | ||||||||||||||||
| if (StringUtils.isBlank(session)) | ||||||||||||||||
| ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session is a required parameter")); | ||||||||||||||||
|
|
||||||||||||||||
| try (Session s = DatabaseSessionFactory.createSession()) { | ||||||||||||||||
| User user = DatabaseHelper.getUserFromSession(session); | ||||||||||||||||
|
|
@@ -121,6 +146,13 @@ public static byte[] deleteUserResponse(String session, String pass) throws IOEx | |||||||||||||||
|
|
||||||||||||||||
| String hash = user.getPassword(); | ||||||||||||||||
|
|
||||||||||||||||
| if (hash.equals("")) { | ||||||||||||||||
| //TODO: Authorize against oidc provider before deletion | ||||||||||||||||
| var tr = s.beginTransaction(); | ||||||||||||||||
| s.remove(user); | ||||||||||||||||
| tr.commit(); | ||||||||||||||||
| return mapper.writeValueAsBytes(new DeleteUserResponse(user.getUsername())); | ||||||||||||||||
| } | ||||||||||||||||
| if (!hashMatch(hash, pass)) | ||||||||||||||||
| ExceptionHandler.throwErrorResponse(new IncorrectCredentialsResponse()); | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
This file contains hidden or 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 me.kavin.piped.utils.obj; | ||
|
|
||
| import com.nimbusds.oauth2.sdk.auth.Secret; | ||
| import com.nimbusds.oauth2.sdk.id.ClientID; | ||
|
|
||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
|
|
||
| public class OidcProvider { | ||
| public String name; | ||
| public ClientID clientID; | ||
| public Secret clientSecret; | ||
| public URI authUri; | ||
| public URI tokenUri; | ||
| public URI userinfoUri; | ||
|
|
||
| public OidcProvider(String name, String clientID, String clientSecret, String authUri, String tokenUri, String userinfoUri) throws URISyntaxException { | ||
| this.name = name; | ||
| this.clientID = new ClientID(clientID); | ||
| this.clientSecret = new Secret(clientSecret); | ||
| this.authUri = new URI(authUri); | ||
| this.tokenUri = new URI(tokenUri); | ||
| this.userinfoUri = new URI(userinfoUri); | ||
| } | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.