diff --git a/pom.xml b/pom.xml index 4bb9444..5b354f5 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,12 @@ spring-boot-starter-web + + org.imsglobal + basiclti-util + 1.1.2 + + mysql mysql-connector-java @@ -38,6 +44,28 @@ lombok true + + org.webjars + bootstrap + + + org.springframework.boot + spring-boot-devtools + true + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.security + spring-security-test + test + org.springframework.boot spring-boot-starter-test @@ -57,6 +85,11 @@ org.springframework.boot spring-boot-maven-plugin + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.0 + diff --git a/src/main/java/com/wmi/lti/config/MvcConfig.java b/src/main/java/com/wmi/lti/config/MvcConfig.java new file mode 100644 index 0000000..e200aba --- /dev/null +++ b/src/main/java/com/wmi/lti/config/MvcConfig.java @@ -0,0 +1,16 @@ +package com.wmi.lti.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class MvcConfig implements WebMvcConfigurer { + + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/home").setViewName("home"); + registry.addViewController("/").setViewName("home"); + registry.addViewController("/login").setViewName("login"); + } + +} diff --git a/src/main/java/com/wmi/lti/config/WebSecurityConfig.java b/src/main/java/com/wmi/lti/config/WebSecurityConfig.java new file mode 100644 index 0000000..a979219 --- /dev/null +++ b/src/main/java/com/wmi/lti/config/WebSecurityConfig.java @@ -0,0 +1,43 @@ +package com.wmi.lti.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; + +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .antMatchers("/", "/home").permitAll() + .anyRequest().authenticated() + .and() + .formLogin() + .loginPage("/login") + .permitAll() + .and() + .logout() + .permitAll(); + } + + @Bean + @Override + public UserDetailsService userDetailsService() { + UserDetails user = + User.withDefaultPasswordEncoder() + .username("adrianw") + .password("eduserwis1245") + .roles("USER") + .build(); + + return new InMemoryUserDetailsManager(user); + } +} diff --git a/src/main/java/com/wmi/lti/controllers/HomeController.java b/src/main/java/com/wmi/lti/controllers/HomeController.java new file mode 100644 index 0000000..b53dfea --- /dev/null +++ b/src/main/java/com/wmi/lti/controllers/HomeController.java @@ -0,0 +1,20 @@ +package com.wmi.lti.controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +public class HomeController { + + @GetMapping("/home") + public String home( + @RequestParam(name = "name", required = false, defaultValue = "") + String name, Model model) { + + //model.addAttribute("message", name); + + return "home"; //view + } +} diff --git a/src/main/java/com/wmi/lti/controllers/StartController.java b/src/main/java/com/wmi/lti/controllers/StartController.java new file mode 100644 index 0000000..6cd60ab --- /dev/null +++ b/src/main/java/com/wmi/lti/controllers/StartController.java @@ -0,0 +1,47 @@ +package com.wmi.lti.controllers; + +import org.imsglobal.lti.launch.LtiOauthSigner; +import org.imsglobal.lti.launch.LtiSigner; +import org.imsglobal.lti.launch.LtiSigningException; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.User; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.Map; + +@Controller +public class StartController { + + @GetMapping("/start") + public String start( + @RequestParam(name = "key", required = false, defaultValue = "") + String key, @RequestParam(name = "secret", required = false, defaultValue = "") + String secret, @RequestParam(name = "url", required = false, defaultValue = "") + String url, Model model) { + + LtiSigner signer = new LtiOauthSigner(); + Map parameters = new HashMap<>(); + parameters.put("lti_message_type","basic-lti-launch-request"); + parameters.put("lti_version", "LTI-1p0"); + parameters.put("resource_link_id", "gsdgsdgsdgsdsgddsg3g3"); + //parameters.put("resource_link_id", ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()); + try { + Map signedParameters = signer.signParameters(parameters, key, secret, url, "POST"); + String link = url + "?" + "oauth_nonce=" + signedParameters.get("oauth_nonce") + "&oauth_signature=" + signedParameters.get("oauth_signature") + "&oauth_consumer_key=" + signedParameters.get("oauth_consumer_key") + "&oauth_signature_method=" + signedParameters.get("oauth_signature_method") + "&oauth_timestamp=" + signedParameters.get("oauth_timestamp") + "&oauth_version=" + signedParameters.get("oauth_version") + "<i_message_type=" + signedParameters.get("lti_message_type") + "<i_version=" + signedParameters.get("lti_version") + "&resource_link_id=" + signedParameters.get("resource_link_id"); + model.addAttribute("parameters", signedParameters); + model.addAttribute("launchUrl", link); + } catch (LtiSigningException e) { + e.printStackTrace(); + } + //model.addAttribute("message", name); + return "start"; //view + + } +} + + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..6b40d1d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,6 @@ +spring.jpa.hibernate.ddl-auto=update +spring.datasource.url=jdbc:mysql://localhost:3306/lti +spring.datasource.username=lti +spring.datasource.password=YnBDC0hqqhKaxt94 +spring.thymeleaf.cache=false diff --git a/src/main/resources/static/css/main.css b/src/main/resources/static/css/main.css new file mode 100644 index 0000000..c0482a0 --- /dev/null +++ b/src/main/resources/static/css/main.css @@ -0,0 +1,15 @@ +body { + padding-top: 0rem; +} +.starter-template { + padding: 3rem 1.5rem; + text-align: center; +} + +h1{ + color:#0000FF; +} + +h2{ + color:#FF0000; +} \ No newline at end of file diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html new file mode 100644 index 0000000..321b855 --- /dev/null +++ b/src/main/resources/templates/home.html @@ -0,0 +1,15 @@ + + + + + + + LTI + + + + +This is tool consumer for LTI
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html new file mode 100644 index 0000000..87b744c --- /dev/null +++ b/src/main/resources/templates/login.html @@ -0,0 +1,20 @@ + + + + Login + + +
+ Invalid username and password. +
+
+ You have been logged out. +
+
+
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/start.html b/src/main/resources/templates/start.html new file mode 100644 index 0000000..d74faea --- /dev/null +++ b/src/main/resources/templates/start.html @@ -0,0 +1,16 @@ + + + + + + + LTI start + + + + +
+ Launch Tool +
+ + \ No newline at end of file