sordina / servant-options

MIT License
11 stars 0 forks source link

Using with BasicAuth #3

Closed newlandsvalley closed 4 years ago

newlandsvalley commented 4 years ago

I'm attempting to protect my API with BasicAuth, and get this error when using servant-options:

No instance for (Servant.Foreign.Internal.GenerateList
                         NoContent
                         (Servant.Foreign.Internal.Foreign
                            NoContent
                            (BasicAuth "tunebank-realm" UserName
                             :> (QueryParam "page" Int
                                 :> (QueryParam "size" Int :> Get '[JSON] UserList)))))
sordina commented 4 years ago

Hi @newlandsvalley I'll see if I can replicate this!

sordina commented 4 years ago

What version of Servant are you using?

newlandsvalley commented 4 years ago

Hi, @sordina - many thanks. I'm using 0.16.2. I must admit, I'm not entirely sure what I'm doing at the moment!

sordina commented 4 years ago

Hi @newlandsvalley I've pushed a couple of examples to GitHub that replicates your issue:

2ea2c3c..ef7b541

I'll see if I can fix the issue!

sordina commented 4 years ago

You can work around this with something like:

type UserAPI' = "users" :> QueryParam "sortby" SortBy :> Get '[JSON] [User]
type UserAPI = BasicAuth "foo-realm" User :> UserAPI'

app :: [User] -> Application
app us = serveWithContext
  (Proxy :: Proxy UserAPI)
  basicAuthServerContext $ apiImpl

main :: IO ()
main = run 8081 (provideOptions (Proxy :: Proxy UserAPI') app)
sordina commented 4 years ago

Although this obviously isn't ideal, especially if your auth routes are nested!

sordina commented 4 years ago

Looks like the issue is mentioned upstream in servant-foreign:

https://github.com/haskell-servant/servant/issues/535

newlandsvalley commented 4 years ago

Very many thanks for making such an effort to investigate this, @sordina. In actual fact I realise now that I can get by with simpleCors so it's no longer holding me back. I hadn't spotted the servant-foreign issue.

sordina commented 4 years ago

No problems @newlandsvalley I'll let you know if I figure out how to implement the fix!

sordina commented 4 years ago

@newlandsvalley I got it working with the following orphan instance:

instance (HasForeign lang ftype api) => HasForeign lang ftype (BasicAuth a b :> api) where
  type Foreign ftype (BasicAuth a b :> api) = Foreign ftype api
  foreignFor lang proxy1 Proxy req = foreignFor lang proxy1 (Proxy :: Proxy api) req

1c3faf3..aa9338b

newlandsvalley commented 4 years ago

Wow!