-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support anonymous launches LTI 1.3 launches.
If the user isn't logged into the service providing access to the tool then when the launch is done we don't get a subject claim in the JWT. This is still a valid launch and it's up to the consuming tool to decide how they want to handle these launches.
- Loading branch information
Showing
5 changed files
with
128 additions
and
6 deletions.
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
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
89 changes: 89 additions & 0 deletions
89
src/main/java/uk/ac/ox/ctl/lti13/security/oauth2/core/user/LtiOauth2User.java
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package uk.ac.ox.ctl.lti13.security.oauth2.core.user; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.AuthorityUtils; | ||
import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames; | ||
import org.springframework.security.oauth2.core.oidc.OidcIdToken; | ||
import org.springframework.security.oauth2.core.oidc.OidcUserInfo; | ||
import org.springframework.security.oauth2.core.oidc.user.OidcUser; | ||
import org.springframework.util.Assert; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.LinkedHashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
|
||
/** | ||
* LTI launches can happen when there isn't a user logged in. In this situation | ||
* there isn't a subject claim and so we need to support this. | ||
*/ | ||
public class LtiOauth2User implements OidcUser { | ||
|
||
public static final String ANONYMOUS = "anonymous"; | ||
|
||
private final OidcIdToken idToken; | ||
private final Set<GrantedAuthority> authorities; | ||
private final String nameAttributeKey; | ||
|
||
public LtiOauth2User(Collection<? extends GrantedAuthority> authorities, OidcIdToken idToken) { | ||
this(authorities, idToken, IdTokenClaimNames.SUB); | ||
} | ||
/** | ||
* Constructs a {@code DefaultOAuth2User} using the provided parameters. | ||
* | ||
* @param authorities the authorities granted to the user | ||
* @param idToken the ID Token containing claims about the user | ||
*/ | ||
public LtiOauth2User(Collection<? extends GrantedAuthority> authorities, OidcIdToken idToken, String nameAttributeKey) { | ||
Assert.notNull(idToken, "idToken cannot be null"); | ||
Assert.hasText(nameAttributeKey, "nameAttributeKey cannot be empty"); | ||
this.authorities = (authorities != null) | ||
? Collections.unmodifiableSet(new LinkedHashSet<>(this.sortAuthorities(authorities))) | ||
: Collections.unmodifiableSet(new LinkedHashSet<>(AuthorityUtils.NO_AUTHORITIES)); | ||
this.idToken = idToken; | ||
this.nameAttributeKey = nameAttributeKey; | ||
} | ||
|
||
private Set<GrantedAuthority> sortAuthorities(Collection<? extends GrantedAuthority> authorities) { | ||
SortedSet<GrantedAuthority> sortedAuthorities = new TreeSet<>( | ||
Comparator.comparing(GrantedAuthority::getAuthority)); | ||
sortedAuthorities.addAll(authorities); | ||
return sortedAuthorities; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
String name = idToken.getClaimAsString(nameAttributeKey); | ||
return name == null ? ANONYMOUS : name; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getClaims() { | ||
return idToken.getClaims(); | ||
} | ||
|
||
@Override | ||
public OidcUserInfo getUserInfo() { | ||
// In the LTI launches we never do additional user lookups so we just always return null. | ||
return null; | ||
} | ||
|
||
@Override | ||
public OidcIdToken getIdToken() { | ||
return idToken; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getAttributes() { | ||
return idToken.getClaims(); | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return authorities; | ||
} | ||
} |
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
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