-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/GMOD/Apollo into develop
- Loading branch information
Showing
9 changed files
with
152 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
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 |
---|---|---|
@@ -1,6 +1,18 @@ | ||
import com.brandseye.cors.CorsFilter | ||
import grails.plugin.springwebsocket.GrailsSimpAnnotationMethodMessageHandler | ||
|
||
// Place your Spring DSL code here | ||
beans = { | ||
corsFilter(CorsFilter) | ||
|
||
webSocketConfig org.bbop.apollo.websocket.WebSocketConfig | ||
|
||
grailsSimpAnnotationMethodMessageHandler( | ||
GrailsSimpAnnotationMethodMessageHandler, | ||
ref("clientInboundChannel"), | ||
ref("clientOutboundChannel"), | ||
ref("brokerMessagingTemplate") | ||
) { | ||
destinationPrefixes = ["/app"] | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
src/groovy/org/bbop/apollo/websocket/ApolloHandshakeHandler.groovy
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,38 @@ | ||
package org.bbop.apollo.websocket | ||
|
||
|
||
import java.security.Principal | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.server.ServerHttpRequest | ||
import org.springframework.http.server.ServerHttpResponse | ||
import org.springframework.web.socket.WebSocketHandler | ||
import org.springframework.web.socket.server.HandshakeHandler | ||
import org.springframework.web.socket.server.HandshakeFailureException | ||
import org.springframework.web.socket.server.support.DefaultHandshakeHandler | ||
|
||
class ApolloHandshakeHandler implements HandshakeHandler { | ||
|
||
DefaultHandshakeHandler defaultHandshakeHandler = new DefaultHandshakeHandler() | ||
AuthenticatingHandshakeHandler handshakeHandler = new AuthenticatingHandshakeHandler() | ||
|
||
final boolean doHandshake( | ||
ServerHttpRequest request, | ||
ServerHttpResponse response, | ||
WebSocketHandler wsHandler, | ||
Map<String, Object> attributes | ||
) throws HandshakeFailureException { | ||
|
||
Principal user = defaultHandshakeHandler.determineUser(request, wsHandler, attributes) | ||
if (user == null) { | ||
Principal newUser = handshakeHandler.determineUser(request, wsHandler, attributes) | ||
if (newUser == null) { | ||
response.setStatusCode(HttpStatus.FORBIDDEN) | ||
return false | ||
} | ||
return handshakeHandler.doHandshake(request, response, wsHandler, attributes) | ||
} | ||
return defaultHandshakeHandler.doHandshake(request, response, wsHandler, attributes) | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/groovy/org/bbop/apollo/websocket/AuthenticatingHandshakeHandler.groovy
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,42 @@ | ||
package org.bbop.apollo.websocket | ||
|
||
|
||
import grails.util.Holders | ||
import org.apache.shiro.SecurityUtils | ||
|
||
import javax.security.auth.Subject | ||
import java.security.Principal | ||
|
||
import org.apache.shiro.authc.UsernamePasswordToken | ||
import org.bbop.apollo.websocket.StompPrincipal | ||
import org.springframework.http.server.ServerHttpRequest | ||
import org.springframework.web.socket.WebSocketHandler | ||
import org.springframework.web.socket.server.HandshakeHandler | ||
import org.springframework.web.socket.server.support.DefaultHandshakeHandler | ||
|
||
class AuthenticatingHandshakeHandler extends DefaultHandshakeHandler { | ||
def usernamePasswordAuthenticatorService = Holders.grailsApplication.mainContext.getBean('usernamePasswordAuthenticatorService') | ||
|
||
@Override | ||
Principal determineUser( | ||
ServerHttpRequest request, | ||
WebSocketHandler wsHandler, | ||
Map<String, Object> attributes | ||
) { | ||
// https://stackoverflow.com/a/41584987/10750707 | ||
def query = request.getURI().getQuery() | ||
def queryParams = query.split('&') | ||
def mapParams = queryParams.collectEntries { param -> param.split('=').collect { URLDecoder.decode(it) }} | ||
String username = mapParams["username"] | ||
String password = mapParams["password"] | ||
if (!(username && password)) { | ||
return null | ||
} | ||
UsernamePasswordToken authToken = new UsernamePasswordToken(username, password) | ||
if(usernamePasswordAuthenticatorService.authenticate(authToken,null)){ | ||
return request.getPrincipal() | ||
} | ||
return null | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/groovy/org/bbop/apollo/websocket/StompPrincipal.groovy
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,17 @@ | ||
package org.bbop.apollo.websocket | ||
|
||
|
||
import java.security.Principal | ||
|
||
class StompPrincipal implements Principal { | ||
String userName | ||
|
||
StompPrincipal(userName) { | ||
this.userName = userName | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.userName | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/groovy/org/bbop/apollo/websocket/WebSocketConfig.groovy
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,23 @@ | ||
package org.bbop.apollo.websocket | ||
|
||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.messaging.simp.config.MessageBrokerRegistry | ||
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry | ||
|
||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { | ||
|
||
private final def apolloHandshakeHandler = new ApolloHandshakeHandler() | ||
|
||
@Override | ||
void registerStompEndpoints(StompEndpointRegistry ser) { | ||
ser.addEndpoint("/stomp") | ||
.setAllowedOrigins("*") | ||
.setHandshakeHandler(apolloHandshakeHandler) | ||
.withSockJS() | ||
} | ||
|
||
} |