-
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.
Add a User-Agent challenge when validating tokens
- Loading branch information
Showing
14 changed files
with
205 additions
and
38 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
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
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
18 changes: 18 additions & 0 deletions
18
src/main/java/ubc/pavlab/rdp/security/SecureTokenChallenge.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,18 @@ | ||
package ubc.pavlab.rdp.security; | ||
|
||
import ubc.pavlab.rdp.exception.TokenException; | ||
import ubc.pavlab.rdp.model.Token; | ||
|
||
public interface SecureTokenChallenge<T> { | ||
|
||
/** | ||
* @param token the token being challenged | ||
* @throws TokenException | ||
*/ | ||
void challenge( Token token, T object ) throws TokenException; | ||
|
||
/** | ||
* Indicate if the challenge supports the given class. | ||
*/ | ||
boolean supports( Class<?> clazz ); | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/ubc/pavlab/rdp/security/UserAgentChallenge.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,56 @@ | ||
package ubc.pavlab.rdp.security; | ||
|
||
import nl.basjes.parse.useragent.UserAgent; | ||
import nl.basjes.parse.useragent.UserAgentAnalyzer; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Component; | ||
import ubc.pavlab.rdp.exception.TokenException; | ||
import ubc.pavlab.rdp.model.Token; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Challenge the token by checking the User-Agent header. | ||
* | ||
* @author poirigui | ||
*/ | ||
@Component | ||
public class UserAgentChallenge implements SecureTokenChallenge<HttpServletRequest> { | ||
|
||
private final static Set<String> ACCEPTED_AGENT_CLASSES = new HashSet<>( Arrays.asList( "Browser", "Browser Webview" ) ); | ||
|
||
private final UserAgentAnalyzer userAgentAnalyzer; | ||
|
||
public UserAgentChallenge() { | ||
this.userAgentAnalyzer = UserAgentAnalyzer | ||
.newBuilder() | ||
.withField( "AgentClass" ) | ||
.useJava8CompatibleCaching() | ||
.withCache( 10000 ) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void challenge( Token token, HttpServletRequest request ) throws TokenException { | ||
String userAgent = request.getHeader( "User-Agent" ); | ||
if ( StringUtils.isBlank( userAgent ) ) { | ||
throw new TokenException( "The User-Agent header is missing or blank." ); | ||
} | ||
// unfortunately, Yauaa is not thread safe | ||
UserAgent.ImmutableUserAgent ua; | ||
synchronized ( userAgentAnalyzer ) { | ||
ua = userAgentAnalyzer.parse( userAgent ); | ||
} | ||
if ( !ACCEPTED_AGENT_CLASSES.contains( ua.get( "AgentClass" ).getValue() ) ) { | ||
throw new TokenException( "Unacceptable User-Agent header." ); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean supports( Class<?> clazz ) { | ||
return HttpServletRequest.class.isAssignableFrom( clazz ); | ||
} | ||
} |
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
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
Oops, something went wrong.