3

Quoting from the WebSharper 2.5 alpah docs the remoting component assumes that:

RPC-callable methods are safe to call from the web by an unauthenticated client.

Is there anyway to secure remote calls so they can only be called from an authenticated client?

2 Answers2

5

One of the samples in the WebSharper website is a chat application that seems to do just that by providing a Login method that returns an authentication token, which is then required to call the other functions:

[<Rpc>]
let Login (user: string) : Option<Auth.Token> =
    let s = State.Get()
    if s.Users.ContainsKey user then
        None
    else
        // (snip)
        user |> Auth.Generate |> Some

[<Rpc>]
let Poll (auth: Auth.Token) (time: int) =
    // (snip)

The full chat sample can be found here: http://www.websharper.com/samples/Chat

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
2

Just been playing with this myself. Turns out if you're using Forms Authentication you can read the current HTTPContext from inside RPC methods so you can do something like this:

[<Rpc>]
let protectedMethod () =
  match IntelliFactory.WebSharper.Sitelets.UserSession.GetLoggedInUser() with
  | Some(username) ->
    // User is authenticated... do stuff
    ()
  | None -> failwith "Authentication failed"
Oenotria
  • 1,692
  • 11
  • 24