Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/maven/fixes/9.0' into maven/rele…
Browse files Browse the repository at this point in the history
…ase/9.0
  • Loading branch information
metaventis-build committed Oct 9, 2024
2 parents 6021b86 + fd1bc96 commit 56867b5
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.edu_sharing.service.lti13.registration.DynamicRegistrationTokens;
import org.edu_sharing.service.lti13.registration.RegistrationService;
import org.edu_sharing.service.lti13.uoc.Config;
import org.edu_sharing.service.lti13.uoc.elc.spring.lti.security.openid.HttpSessionOIDCLaunchSession;
import org.edu_sharing.service.usage.Usage2Service;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;
Expand All @@ -60,6 +61,7 @@
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.util.*;
import org.edu_sharing.service.lti13.uoc.elc.spring.lti.security.openid.LoginRequestFactory;

@Path("/lti/v13")
@Consumes({ "text/html" })
Expand Down Expand Up @@ -144,13 +146,9 @@ private Response loginInitiationsCore(String iss, String clientId, String ltiDep
RepoTools repoTools = new RepoTools();
ApplicationInfo platform = repoTools.getApplicationInfo(iss, clientId, ltiDeploymentId);
Tool tool = Config.getTool(platform, req,true);
/**
* @TODO
* jakarta/javax lib problem
* justed fixed compile problems
*/

// get data from request
final LoginRequest loginRequest = null;//LoginRequestFactory.from(req);
final LoginRequest loginRequest = LoginRequestFactory.from(req);
if (this.logger.isInfoEnabled()) {
this.logger.info("OIDC launch received with " + loginRequest.toString());
}
Expand Down Expand Up @@ -300,7 +298,7 @@ private Response ltiLaunch(String idToken, String state, HttpServletRequest req,
* jakarta/javax lib problem
* justed fixed compile problems
*/
String sessionNonce = null;//new HttpSessionOIDCLaunchSession(req).getNonce();
String sessionNonce = new HttpSessionOIDCLaunchSession(req).getNonce();
if(!nonce.equals(sessionNonce)){
logger.error("nonce:"+nonce+ " sessionNonce:"+sessionNonce +". maybe jsessionid is not the same for login_initiation and launch url. ");
throw new IllegalStateException("nonce is invalid");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import edu.uoc.elc.lti.tool.Tool;
import edu.uoc.elc.spring.lti.tool.BasicToolDefinition;
import edu.uoc.elc.spring.lti.tool.ToolDefinitionBean;
import edu.uoc.elc.spring.lti.tool.ToolFactory;
import edu.uoc.lti.accesstoken.AccessTokenRequestBuilder;
import edu.uoc.lti.accesstoken.JSONAccessTokenRequestBuilderImpl;
import edu.uoc.lti.claims.ClaimAccessor;
Expand All @@ -16,6 +15,7 @@
import org.edu_sharing.repository.server.tools.ApplicationInfoList;

import jakarta.servlet.http.HttpServletRequest;
import org.edu_sharing.service.lti13.uoc.elc.spring.lti.tool.ToolFactory;

public class Config {
private static BasicToolDefinition getBasicToolDefinition(ApplicationInfo platform){
Expand Down Expand Up @@ -60,12 +60,6 @@ private static ToolDefinitionBean getToolDefinitionBean(ApplicationInfo platform
}

public static Tool getTool(ApplicationInfo platform, HttpServletRequest request, boolean clearSession){
/**
* @TODO
* jakarta/javax lib problem
* justed fixed compile problems
*/
return null;
//return new ToolFactory().from(getToolDefinitionBean(platform), request, clearSession);
return new ToolFactory().from(getToolDefinitionBean(platform), request, clearSession);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.edu_sharing.service.lti13.uoc.elc.spring.lti.security.openid;

import edu.uoc.lti.oidc.OIDCLaunchSession;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;

import jakarta.servlet.http.HttpServletRequest;

/**
* @author [email protected]
*/
@RequiredArgsConstructor
public class HttpSessionOIDCLaunchSession implements OIDCLaunchSession {
private final static String STATE_SESSION_ATTRIBUTE_NAME = "currentLti1.3State";
private final static String NONCE_SESSION_ATTRIBUTE_NAME = "currentLti1.3Nonce";
private final static String TARGETLINK_URI_SESSION_ATTRIBUTE_NAME = "currentLti1.3TargetLinkUri";

private final HttpServletRequest request;

@Override
public void setState(String s) {
setAttribute(STATE_SESSION_ATTRIBUTE_NAME, s);
}

@Override
public void setNonce(String s) {
setAttribute(NONCE_SESSION_ATTRIBUTE_NAME, s);
}

@Override
public void setTargetLinkUri(String s) {
setAttribute(TARGETLINK_URI_SESSION_ATTRIBUTE_NAME, s);
}

private void setAttribute(String name, String value) {
request.getSession().setAttribute(name, value);
}

@Override
public String getState() {
return getAttribute(STATE_SESSION_ATTRIBUTE_NAME);
}

@Override
public String getNonce() {
return getAttribute(NONCE_SESSION_ATTRIBUTE_NAME);
}

@Override
public String getTargetLinkUri() {
return getAttribute(TARGETLINK_URI_SESSION_ATTRIBUTE_NAME);
}

private String getAttribute(String name) {
Object state = request.getSession().getAttribute(name);
return state != null ? state.toString() : null;
}

public void clear() {
final HttpSession session = this.request.getSession(false);
if (session != null) {
setState(null);
setTargetLinkUri(null);
setNonce(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.edu_sharing.service.lti13.uoc.elc.spring.lti.security.openid;

import edu.uoc.elc.lti.tool.oidc.LoginRequest;
import jakarta.servlet.http.HttpServletRequest;

public class LoginRequestFactory {
public static LoginRequest from(HttpServletRequest request) {
return LoginRequest.builder()
.iss(request.getParameter("iss"))
.login_hint(request.getParameter("login_hint"))
.target_link_uri(request.getParameter("target_link_uri"))
.lti_message_hint(request.getParameter("lti_message_hint"))
.lti_deployment_id(request.getParameter("lti_deployment_id"))
.client_id(request.getParameter("client_id"))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.edu_sharing.service.lti13.uoc.elc.spring.lti.security.utils;

import jakarta.servlet.http.HttpServletRequest;

public class TokenFactory {
public static String from(HttpServletRequest httpServletRequest) {
String token = httpServletRequest.getParameter("jwt");
if (token == null || "".equals(token)) {
token = httpServletRequest.getParameter("id_token");
}
return token;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.edu_sharing.service.lti13.uoc.elc.spring.lti.tool;

import edu.uoc.elc.lti.tool.Tool;
import edu.uoc.elc.lti.tool.ToolDefinition;



import edu.uoc.elc.spring.lti.tool.ToolDefinitionBean;
import edu.uoc.elc.spring.lti.tool.ToolDefinitionFactory;
import jakarta.servlet.http.HttpServletRequest;
import org.edu_sharing.service.lti13.uoc.elc.spring.lti.security.openid.HttpSessionOIDCLaunchSession;
import org.edu_sharing.service.lti13.uoc.elc.spring.lti.security.utils.TokenFactory;

/**
* @author [email protected]
*/
public class ToolFactory {
HttpSessionOIDCLaunchSession oidcLaunchSession;

public ToolFactory() {
}

public Tool from(ToolDefinitionBean toolDefinitionBean, HttpServletRequest request) {
return this.from(toolDefinitionBean, request, false);
}

public Tool from(ToolDefinitionBean toolDefinitionBean, HttpServletRequest request, boolean clearSession) {
this.oidcLaunchSession = new HttpSessionOIDCLaunchSession(request);
if (clearSession) {
this.oidcLaunchSession.clear();
}

ToolDefinition toolDefinition = ToolDefinitionFactory.from(toolDefinitionBean);
Tool tool = new Tool(toolDefinition, toolDefinitionBean.getClaimAccessor(), this.oidcLaunchSession, toolDefinitionBean.getBuilders());
String token = TokenFactory.from(request);
String state = request.getParameter("state");
tool.validate(token, state);
return tool;
}
}

0 comments on commit 56867b5

Please sign in to comment.