Compatibility with play-socket.io


#1

Hi,

I am wondering if this library would be compatible with the play-socket.io project. It seems like it would work in theory by injecting the silhouette environment into the engine, but I can’t really tell. Does anyone have any experience with this? Here’s a link to the project:

And the relevant authentication section:

I guess the question boils down to I have a raw RequestHeader. How can I use that to manually authenticate with my silhouette environment? Happy to submit a PR to the docs if I can get this working.

Thanks!


#2

It was actually quite easy. I simply followed the example in the WebSocket section of the official documentation and injected the silhouette environment into my engine provider. Here is my code for anyone interested:

@Singleton
class SocketIOEngineProvider @Inject()(socketIO: SocketIO,
                                       silhouette: Silhouette[DefaultEnv])(
    implicit mat: Materializer,
    ec: ExecutionContext)
    extends Provider[EngineIOController]
    with Logger {

  override lazy val get: EngineIOController =
    socketIO.builder
      .onConnectAsync { (request, _) =>
        {
          implicit val req: Request[AnyContentAsEmpty.type] =
            Request(request, AnyContentAsEmpty)
          silhouette
            .SecuredRequestHandler { securedRequest =>
              Future.successful(
                HandlerResult(Ok, Some(securedRequest.identity)))
            }
            .map {
              case HandlerResult(_, Some(user)) => Right(user)
              case HandlerResult(r, None) => Left(r)
            }
        }
      }
      .createController()
}

#3

Would be great to have an example on https://www.silhouette.rocks/docs/endpoints#section-websockets


#4

Hi Akkie,

I just submitting an example to the docs. Please review when you can.

Best,

Julian


#5

Great, Thanks! I’ve merged it :+1: