-
Notifications
You must be signed in to change notification settings - Fork 57
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
Implement oidc #628
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
Jeidnx 375ee58
Better Error handling for oidc config
Jeidnx 143711c
Save redirect in state
Jeidnx 18d9317
Show warning message before oidc login
Jeidnx f4b9dff
Only show warning when not redirecting to configured frontend
Jeidnx 53d9b9d
Merge branch 'master' into oidc
Jeidnx 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
Jeidnx c1fde37
Refactor oidc logic into UserHandlers
Jeidnx 024435f
Add database migration for username length change.
FireMasterK 470efd8
Revert "Add database migration for username length change."
Jeidnx 5f6a83a
Add code from the meeting.
FireMasterK 868103c
Merge branch 'master' into oidc
Jeidnx 074e4bc
chore: properly implement oidc
Jeidnx 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
Jeidnx f76f8e0
randomize username
Jeidnx e4ba195
add redirect to oidc delete; more cleanup
Jeidnx 77cd736
explicitly reject empty hashes
Jeidnx 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
Remove debug code and format.
- Loading branch information
commit b0725f882a2bc02695bc885b250b7d3a08549aa5
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 |
---|---|---|
|
@@ -131,7 +131,7 @@ public static byte[] loginResponse(String user, String pass) | |
} | ||
} | ||
|
||
public static HttpResponse oidcLoginResponse(OidcProvider provider, String redirectUri) throws Exception{ | ||
public static HttpResponse oidcLoginResponse(OidcProvider provider, String redirectUri) throws Exception { | ||
if (StringUtils.isBlank(redirectUri)) { | ||
return HttpResponse.ofCode(400).withHtml("redirect is a required parameter"); | ||
} | ||
|
@@ -146,9 +146,10 @@ public static HttpResponse oidcLoginResponse(OidcProvider provider, String redir | |
AuthenticationRequest oidcRequest = new AuthenticationRequest.Builder( | ||
new ResponseType("code"), | ||
new Scope("openid"), | ||
provider.clientID, callback) | ||
.endpointURI(provider.authUri) | ||
.codeChallenge(codeVerifier, CodeChallengeMethod.S256) | ||
provider.clientID, callback | ||
) | ||
.endpointURI(provider.authUri) | ||
.codeChallenge(codeVerifier, CodeChallengeMethod.S256) | ||
.state(new State(state)) | ||
.nonce(data.nonce).build(); | ||
|
||
|
@@ -164,6 +165,7 @@ public static HttpResponse oidcLoginResponse(OidcProvider provider, String redir | |
oidcRequest.toURI().toString() + | ||
"\">here</a></body></html>"); | ||
} | ||
|
||
public static HttpResponse oidcCallbackResponse(OidcProvider provider, URI requestUri) throws Exception { | ||
AuthenticationSuccessResponse authResponse = parseOidcUri(requestUri); | ||
|
||
|
@@ -196,20 +198,18 @@ public static HttpResponse oidcCallbackResponse(OidcProvider provider, URI reque | |
|
||
try { | ||
provider.validator.validate(idToken, data.nonce); | ||
} catch (BadJOSEException e) { | ||
System.out.println("Invalid token received: " + e.toString()); | ||
return HttpResponse.ofCode(400).withHtml("Received a bad token. Please try again"); | ||
} catch (JOSEException e) { | ||
System.out.println("Token processing error" + e.toString()); | ||
return HttpResponse.ofCode(500).withHtml("Internal processing error. Please try again"); | ||
} | ||
} catch (BadJOSEException e) { | ||
System.err.println("Invalid token received: " + e); | ||
return HttpResponse.ofCode(400).withHtml("Received a bad token. Please try again"); | ||
} catch (JOSEException e) { | ||
System.err.println("Token processing error: " + e); | ||
return HttpResponse.ofCode(500).withHtml("Internal processing error. Please try again"); | ||
} | ||
|
||
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( | ||
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.
|
||
"The userinfo endpoint returned an error. Please try again or contact your oidc admin\n\n" + | ||
userInfoResponse.toErrorResponse().getErrorObject().getDescription()); | ||
|
@@ -245,19 +245,19 @@ public static HttpResponse oidcCallbackResponse(OidcProvider provider, URI reque | |
} | ||
|
||
public static HttpResponse oidcDeleteRequest(String session) throws Exception { | ||
|
||
if (StringUtils.isBlank(session)) { | ||
return HttpResponse.ofCode(400).withHtml("session is a required parameter"); | ||
} | ||
|
||
OidcProvider provider = null; | ||
try (Session s = DatabaseSessionFactory.createSession()) { | ||
|
||
User user = DatabaseHelper.getUserFromSession(session); | ||
try (Session s = DatabaseSessionFactory.createSession()) { | ||
User user = DatabaseHelper.getUserFromSession(session); | ||
|
||
if (user == null) { | ||
return HttpResponse.ofCode(400).withHtml("User not found"); | ||
} | ||
if (user == null) { | ||
return HttpResponse.ofCode(400).withHtml("User not found"); | ||
} | ||
|
||
CriteriaBuilder cb = s.getCriteriaBuilder(); | ||
CriteriaQuery<OidcUserData> cr = cb.createQuery(OidcUserData.class); | ||
|
@@ -266,41 +266,50 @@ public static HttpResponse oidcDeleteRequest(String session) throws Exception { | |
|
||
OidcUserData oidcUserData = s.createQuery(cr).uniqueResult(); | ||
|
||
for (OidcProvider test: Constants.OIDC_PROVIDERS) { | ||
if (test.name.equals(oidcUserData.getProvider())) { | ||
provider = test; | ||
} | ||
if (oidcUserData == null) { | ||
return HttpResponse.ofCode(400).withHtml("User doesn't have an oidc account"); | ||
} | ||
|
||
for (OidcProvider oidcProvider : Constants.OIDC_PROVIDERS) { | ||
if (oidcProvider.name.equals(oidcUserData.getProvider())) { | ||
provider = oidcProvider; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (provider == null) { | ||
if (provider == null) { | ||
return HttpResponse.ofCode(400).withHtml("Invalid user"); | ||
} | ||
CodeVerifier pkceVerifier = new CodeVerifier(); | ||
|
||
URI callback = URI.create(String.format("%s/oidc/%s/delete", Constants.PUBLIC_URL, provider.name)); | ||
OidcData data = new OidcData(session + "|" + Instant.now().getEpochSecond(), pkceVerifier); | ||
String state = data.getState(); | ||
PENDING_OIDC.put(state, data); | ||
|
||
AuthenticationRequest oidcRequest = new AuthenticationRequest.Builder( | ||
new ResponseType("code"), | ||
new Scope("openid"), provider.clientID, callback) | ||
.endpointURI(provider.authUri) | ||
.codeChallenge(pkceVerifier, CodeChallengeMethod.S256) | ||
.state(new State(state)) | ||
.nonce(data.nonce) | ||
// This parameter is optional and the idp does't have to honor it. | ||
.maxAge(0) | ||
.build(); | ||
} | ||
|
||
CodeVerifier pkceVerifier = new CodeVerifier(); | ||
|
||
URI callback = URI.create(String.format("%s/oidc/%s/delete", Constants.PUBLIC_URL, provider.name)); | ||
OidcData data = new OidcData(session + "|" + Instant.now().getEpochSecond(), pkceVerifier); | ||
String state = data.getState(); | ||
PENDING_OIDC.put(state, data); | ||
|
||
AuthenticationRequest oidcRequest = new AuthenticationRequest.Builder( | ||
new ResponseType("code"), | ||
new Scope("openid"), provider.clientID, callback | ||
) | ||
.endpointURI(provider.authUri) | ||
.codeChallenge(pkceVerifier, CodeChallengeMethod.S256) | ||
.state(new State(state)) | ||
.nonce(data.nonce) | ||
// This parameter is optional and the idp doesn't have to honor it. | ||
.maxAge(0) | ||
.build(); | ||
|
||
return HttpResponse.redirect302(oidcRequest.toURI().toString()); | ||
} | ||
|
||
public static HttpResponse oidcDeleteCallback(OidcProvider provider, URI requestUri) throws Exception { | ||
|
||
AuthenticationSuccessResponse sr = parseOidcUri(requestUri); | ||
|
||
OidcData data = UserHandlers.PENDING_OIDC.get(sr.getState().toString()); | ||
OidcData data = PENDING_OIDC.get(sr.getState().toString()); | ||
|
||
if (data == null) { | ||
return HttpResponse.ofCode(400).withHtml( | ||
"Your oidc provider sent invalid state data. Try again or contact your oidc admin" | ||
|
@@ -331,13 +340,13 @@ public static HttpResponse oidcDeleteCallback(OidcProvider provider, URI request | |
IDTokenClaimsSet claims; | ||
try { | ||
claims = provider.validator.validate(idToken, data.nonce); | ||
} catch (BadJOSEException e) { | ||
System.out.println("Invalid token received: " + e.toString()); | ||
return HttpResponse.ofCode(400).withHtml("Received a bad token. Please try again"); | ||
} catch (JOSEException e) { | ||
System.out.println("Token processing error" + e.toString()); | ||
return HttpResponse.ofCode(500).withHtml("Internal processing error. Please try again"); | ||
} | ||
} catch (BadJOSEException e) { | ||
System.err.println("Invalid token received: " + e); | ||
return HttpResponse.ofCode(400).withHtml("Received a bad token. Please try again"); | ||
} catch (JOSEException e) { | ||
System.err.println("Token processing error: " + e); | ||
return HttpResponse.ofCode(500).withHtml("Internal processing error. Please try again"); | ||
} | ||
|
||
Long authTime = (Long) claims.getNumberClaim("auth_time"); | ||
|
||
|
@@ -356,6 +365,7 @@ public static HttpResponse oidcDeleteCallback(OidcProvider provider, URI request | |
s.remove(DatabaseHelper.getUserFromSession(session)); | ||
tr.commit(); | ||
} | ||
|
||
return HttpResponse.redirect302(Constants.FRONTEND_URL + "/preferences?deleted=" + session); | ||
} | ||
|
||
|
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.
withPlainText