Skip to content

Commit

Permalink
extend LTI with U4I
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Krusche committed Jun 10, 2019
1 parent dad0604 commit 6c24bbc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ public class BitbucketService implements VersionControlService {
@Value("${artemis.version-control.secret}")
private String BITBUCKET_PASSWORD;

@Value("${artemis.lti.user-prefix}")
private String USER_PREFIX = "";
@Value("${artemis.lti.user-prefix-edx}")
private String USER_PREFIX_EDX = "";

@Value("${artemis.lti.user-prefix-u4i}")
private String USER_PREFIX_U4I = "";

private final UserService userService;

Expand All @@ -65,7 +68,7 @@ public URL copyRepository(URL templateRepositoryUrl, String username) {

@Override
public void configureRepository(URL repositoryUrl, String username) {
if (username.startsWith(USER_PREFIX)) {
if (username.startsWith(USER_PREFIX_EDX) || username.startsWith((USER_PREFIX_U4I))) {
// It is an automatically created user

User user = userService.getUserByLogin(username).get();
Expand All @@ -83,7 +86,6 @@ public void configureRepository(URL repositoryUrl, String username) {
* This might throw exceptions, for example if the group does not exist on Bitbucket. We can safely ignore them.
*/
}

}
else {
log.debug("Bitbucket user {} already exists", username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
@Transactional
public class LtiService {

public static final String TUMX = "TUMx";

public static final String U4I = "U4I";

private final Logger log = LoggerFactory.getLogger(LtiService.class);

@Value("${artemis.lti.oauth-key}")
Expand All @@ -59,11 +63,17 @@ public class LtiService {
@Value("${artemis.lti.oauth-secret}")
private String OAUTH_SECRET;

@Value("${artemis.lti.user-prefix}")
private String USER_PREFIX = "";
@Value("${artemis.lti.user-prefix-edx}")
private String USER_PREFIX_EDX = "edx";

@Value("${artemis.lti.user-prefix-u4i}")
private String USER_PREFIX_U4I = "u4i";

@Value("${artemis.lti.user-group-name}")
private String USER_GROUP_NAME = "lti";
@Value("${artemis.lti.user-group-name-edx}")
private String USER_GROUP_NAME_EDX = "edx";

@Value("${artemis.lti.user-group-name-u4i}")
private String USER_GROUP_NAME_U4I = "u4i";

private final UserService userService;

Expand Down Expand Up @@ -200,7 +210,16 @@ private Optional<Authentication> authenticateLtiUser(LtiLaunchRequestDTO launchR

String email = launchRequest.getLis_person_contact_email_primary() != null ? launchRequest.getLis_person_contact_email_primary()
: launchRequest.getUser_id() + "@lti.artemis.ase.in.tum.de";
String username = this.USER_PREFIX + (launchRequest.getLis_person_sourcedid() != null ? launchRequest.getLis_person_sourcedid() : launchRequest.getUser_id());
final String username;
if (TUMX.equals(launchRequest.getContext_label())) {
username = this.USER_PREFIX_EDX + (launchRequest.getLis_person_sourcedid() != null ? launchRequest.getLis_person_sourcedid() : launchRequest.getUser_id());
}
else if (U4I.equals(launchRequest.getContext_label())) {
username = this.USER_PREFIX_U4I + (launchRequest.getLis_person_sourcedid() != null ? launchRequest.getLis_person_sourcedid() : launchRequest.getUser_id());
}
else {
throw new InternalAuthenticationServiceException("Unknown context_label sent in LTI Launch Request: " + launchRequest.toString());
}
String fullname = launchRequest.getLis_person_sourcedid() != null ? launchRequest.getLis_person_sourcedid() : launchRequest.getUser_id();

// Check if there is an existing mapping for the user ID
Expand Down Expand Up @@ -235,10 +254,20 @@ private Optional<Authentication> authenticateLtiUser(LtiLaunchRequestDTO launchR

// 4. Case: Create new user
User user = userRepository.findOneByLogin(username).orElseGet(() -> {
User newUser = userService.createUser(username, "", USER_GROUP_NAME, fullname, email, null, "en");

// add user to LTI group
newUser.setGroups(new ArrayList<>(Collections.singletonList(USER_GROUP_NAME)));
final User newUser;
if (TUMX.equals(launchRequest.getContext_label())) {
newUser = userService.createUser(username, "", USER_GROUP_NAME_EDX, fullname, email, null, "en");
// add user to edx group
newUser.setGroups(new ArrayList<>(Collections.singletonList(USER_GROUP_NAME_EDX)));
}
else if (U4I.equals(launchRequest.getContext_label())) {
newUser = userService.createUser(username, "", USER_GROUP_NAME_U4I, fullname, email, null, "en");
// add user to u4i group
newUser.setGroups(new ArrayList<>(Collections.singletonList(USER_GROUP_NAME_U4I)));
}
else {
throw new InternalAuthenticationServiceException("Unknown context_label sent in LTI Launch Request: " + launchRequest.toString());
}

// set random password
String randomEncryptedPassword = passwordEncoder.encode(RandomUtil.generatePassword());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class LtiLaunchRequestDTO {

private String context_id;

private String context_label;

private String oauth_version;

private String oauth_signature_method;
Expand Down Expand Up @@ -218,14 +220,24 @@ public void setOauth_signature(String oauth_signature) {
this.oauth_signature = oauth_signature;
}

public String getContext_label() {
return context_label;
}

public void setContext_label(String context_label) {
this.context_label = context_label;
}

@Override
public String toString() {
return "LtiLaunchRequestDTO{" + "lis_person_sourcedid='" + lis_person_sourcedid + '\'' + ", lis_person_contact_email_primary='" + lis_person_contact_email_primary + '\''
+ ", lis_outcome_service_url='" + lis_outcome_service_url + '\'' + ", lti_message_type='" + lti_message_type + '\'' + ", lti_version='" + lti_version + '\''
+ ", context_id='" + context_id + '\'' + ", oauth_version='" + oauth_version + '\'' + ", oauth_signature_method='" + oauth_signature_method + '\''
+ ", oauth_timestamp=" + oauth_timestamp + ", roles='" + roles + '\'' + ", launch_presentation_locale='" + launch_presentation_locale + '\''
+ ", custom_component_display_name='" + custom_component_display_name + '\'' + ", launch_presentation_return_url='" + launch_presentation_return_url + '\''
+ ", lis_result_sourcedid='" + lis_result_sourcedid + '\'' + ", resource_link_id='" + resource_link_id + '\'' + ", user_id='" + user_id + '\'' + ", oauth_nonce="
+ oauth_nonce + ", oauth_consumer_key='" + oauth_consumer_key + '\'' + ", oauth_signature='" + oauth_signature + '\'' + '}';
+ ", context_id='" + context_id + '\'' + ", context_label='" + context_label + '\'' + ", oauth_version='" + oauth_version + '\'' + ", oauth_signature_method='"
+ oauth_signature_method + '\'' + ", oauth_timestamp=" + oauth_timestamp + ", roles='" + roles + '\'' + ", launch_presentation_locale='"
+ launch_presentation_locale + '\'' + ", custom_component_display_name='" + custom_component_display_name + '\'' + ", launch_presentation_return_url='"
+ launch_presentation_return_url + '\'' + ", lis_result_sourcedid='" + lis_result_sourcedid + '\'' + ", resource_link_id='" + resource_link_id + '\''
+ ", user_id='" + user_id + '\'' + ", oauth_nonce=" + oauth_nonce + ", oauth_consumer_key='" + oauth_consumer_key + '\'' + ", oauth_signature='" + oauth_signature
+ '\'' + '}';
}

}
6 changes: 4 additions & 2 deletions src/main/resources/config/application-artemis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ artemis:
id: artemis_lti
oauth-key: artemis_lti_key
oauth-secret: <secret> # only important for online courses on the edX platform, can typically be ignored
user-prefix: edx_
user-group-name: edx
user-prefix_edx: edx_
user-prefix_u4i: u4i_
user-group-name_edx: edx
user-group-name_u4i: u4i
git:
name: ArTEMiS
email: [email protected]

0 comments on commit 6c24bbc

Please sign in to comment.