Extract token from Headers

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Extract token from Headers

Corentin Dupont
Hello,
I was wondering if there is a way to manipulate headers with Servant.
I have:

newtype Token = Token Text
type API = Header "Authorization" Token :> Get '[JSON] MyData

However, with this method I get "Bearer XXXXX". However, a Token should be only the XXX part...
Any way to extract it automatically?

Thanks
Corentin

--
Reply | Threaded
Open this post in threaded view
|

Re: Extract token from Headers

erik

On Wed, Nov 7, 2018 at 9:20 AM Corentin Dupont <[hidden email]> wrote:
Hello,
I was wondering if there is a way to manipulate headers with Servant.
I have:

newtype Token = Token Text
type API = Header "Authorization" Token :> Get '[JSON] MyData

However, with this method I get "Bearer XXXXX". However, a Token should be only the XXX part...
Any way to extract it automatically?

Thanks
Corentin

--


--
Erik Aker

--
Reply | Threaded
Open this post in threaded view
|

Re: Extract token from Headers

Jonathan Jouty-2
Hi,

Additionally you could implement `FromHttpApiData` using `extractBearerAuth :: ByteString -> Maybe ByteString` from

(or do it yourself if you don't want to depend on `wai-extra`)

On Wed, 7 Nov 2018 at 11:43, erik <[hidden email]> wrote:

On Wed, Nov 7, 2018 at 9:20 AM Corentin Dupont <[hidden email]> wrote:
Hello,
I was wondering if there is a way to manipulate headers with Servant.
I have:

newtype Token = Token Text
type API = Header "Authorization" Token :> Get '[JSON] MyData

However, with this method I get "Bearer XXXXX". However, a Token should be only the XXX part...
Any way to extract it automatically?

Thanks
Corentin

--


--
Erik Aker

--


--
Jonathan Jouty

--
Reply | Threaded
Open this post in threaded view
|

Re: Extract token from Headers

Corentin Dupont
In reply to this post by erik
Cool!
So I tried:

data Token = Token Text deriving (Eq, Show)

instance FromHttpApiData KC.Token where
  parseHeader ((stripPrefix "Bearer ") . decodeUtf8 -> Just tok) = Right $ Token tok


However It seems I also need MimeRender (otherwise the function 'serve' complains that there is no instance:

instance MimeRender PlainText KC.Token where
  mimeRender _ (Token tok) = mimeRender PlainText tok


Is this correct?
It complains:
Data constructor not in scope: PlainText :: Proxy ctype0

Where can I find PlainText? I imported Servant.API.ContentTypes to no avail.


On Wed, Nov 7, 2018 at 6:43 PM erik <[hidden email]> wrote:

On Wed, Nov 7, 2018 at 9:20 AM Corentin Dupont <[hidden email]> wrote:
Hello,
I was wondering if there is a way to manipulate headers with Servant.
I have:

newtype Token = Token Text
type API = Header "Authorization" Token :> Get '[JSON] MyData

However, with this method I get "Bearer XXXXX". However, a Token should be only the XXX part...
Any way to extract it automatically?

Thanks
Corentin

--


--
Erik Aker

--
Reply | Threaded
Open this post in threaded view
|

Re: Extract token from Headers

Corentin Dupont
Ok, I managed to solve it this way:

import qualified Data.ByteString.Lazy as BL

instance MimeRender PlainText Token where
  mimeRender _ (Token tok) = BL.fromStrict $ encodeUtf8 tok



On Thu, Nov 8, 2018 at 10:55 AM Corentin Dupont <[hidden email]> wrote:
Cool!
So I tried:

data Token = Token Text deriving (Eq, Show)

instance FromHttpApiData KC.Token where
  parseHeader ((stripPrefix "Bearer ") . decodeUtf8 -> Just tok) = Right $ Token tok


However It seems I also need MimeRender (otherwise the function 'serve' complains that there is no instance:

instance MimeRender PlainText KC.Token where
  mimeRender _ (Token tok) = mimeRender PlainText tok


Is this correct?
It complains:
Data constructor not in scope: PlainText :: Proxy ctype0

Where can I find PlainText? I imported Servant.API.ContentTypes to no avail.


On Wed, Nov 7, 2018 at 6:43 PM erik <[hidden email]> wrote:

On Wed, Nov 7, 2018 at 9:20 AM Corentin Dupont <[hidden email]> wrote:
Hello,
I was wondering if there is a way to manipulate headers with Servant.
I have:

newtype Token = Token Text
type API = Header "Authorization" Token :> Get '[JSON] MyData

However, with this method I get "Bearer XXXXX". However, a Token should be only the XXX part...
Any way to extract it automatically?

Thanks
Corentin

--


--
Erik Aker

--
Reply | Threaded
Open this post in threaded view
|

Re: Extract token from Headers

Alp Mestanogullari
In reply to this post by Corentin Dupont
It should be (Proxy :: Proxy PlainText), not PlainText, in the RHS of the mimeRender definition.

Otherwise, I think you can make Token a newtype of Text and use GeneralizedNewtypeDeriving to get your MimeRender PlainText instance:

newtype Token = Token Text deriving (Eq, Show, MimeRender PlainText)


On Thu, Nov 8, 2018 at 10:56 AM Corentin Dupont <[hidden email]> wrote:
Cool!
So I tried:

data Token = Token Text deriving (Eq, Show)

instance FromHttpApiData KC.Token where
  parseHeader ((stripPrefix "Bearer ") . decodeUtf8 -> Just tok) = Right $ Token tok


However It seems I also need MimeRender (otherwise the function 'serve' complains that there is no instance:

instance MimeRender PlainText KC.Token where
  mimeRender _ (Token tok) = mimeRender PlainText tok


Is this correct?
It complains:
Data constructor not in scope: PlainText :: Proxy ctype0

Where can I find PlainText? I imported Servant.API.ContentTypes to no avail.


On Wed, Nov 7, 2018 at 6:43 PM erik <[hidden email]> wrote:

On Wed, Nov 7, 2018 at 9:20 AM Corentin Dupont <[hidden email]> wrote:
Hello,
I was wondering if there is a way to manipulate headers with Servant.
I have:

newtype Token = Token Text
type API = Header "Authorization" Token :> Get '[JSON] MyData

However, with this method I get "Bearer XXXXX". However, a Token should be only the XXX part...
Any way to extract it automatically?

Thanks
Corentin

--


--
Erik Aker

--


--
Alp Mestanogullari

--
Reply | Threaded
Open this post in threaded view
|

Re: Extract token from Headers

Nickolay Kudasov
Corentin,

I think it is a bit strange that you're asked to provide MimeRender instance. What is your exact API?

Here's a minimal working Servant server with Token used both in request and response headers:

Kind regards,
Nick

On Thu, 8 Nov 2018 at 13:06 Alp Mestanogullari <[hidden email]> wrote:
It should be (Proxy :: Proxy PlainText), not PlainText, in the RHS of the mimeRender definition.

Otherwise, I think you can make Token a newtype of Text and use GeneralizedNewtypeDeriving to get your MimeRender PlainText instance:

newtype Token = Token Text deriving (Eq, Show, MimeRender PlainText)


On Thu, Nov 8, 2018 at 10:56 AM Corentin Dupont <[hidden email]> wrote:
Cool!
So I tried:

data Token = Token Text deriving (Eq, Show)

instance FromHttpApiData KC.Token where
  parseHeader ((stripPrefix "Bearer ") . decodeUtf8 -> Just tok) = Right $ Token tok


However It seems I also need MimeRender (otherwise the function 'serve' complains that there is no instance:

instance MimeRender PlainText KC.Token where
  mimeRender _ (Token tok) = mimeRender PlainText tok


Is this correct?
It complains:
Data constructor not in scope: PlainText :: Proxy ctype0

Where can I find PlainText? I imported Servant.API.ContentTypes to no avail.


On Wed, Nov 7, 2018 at 6:43 PM erik <[hidden email]> wrote:

On Wed, Nov 7, 2018 at 9:20 AM Corentin Dupont <[hidden email]> wrote:
Hello,
I was wondering if there is a way to manipulate headers with Servant.
I have:

newtype Token = Token Text
type API = Header "Authorization" Token :> Get '[JSON] MyData

However, with this method I get "Bearer XXXXX". However, a Token should be only the XXX part...
Any way to extract it automatically?

Thanks
Corentin

--


--
Erik Aker

--


--
Alp Mestanogullari

--

--
Reply | Threaded
Open this post in threaded view
|

Re: Extract token from Headers

Alp Mestanogullari
Nickolay, it's because you're using JSON, whose MimeRender/MimeUnrender instances forward everything to the corresponding aeson classes. While Corentin is using PlainText, which doesn't have any such classes to send everything to. Therefore, you end up having to define MimeRender instances for your user types.

On Thu, Nov 8, 2018 at 12:57 PM Nickolay Kudasov <[hidden email]> wrote:
Corentin,

I think it is a bit strange that you're asked to provide MimeRender instance. What is your exact API?

Here's a minimal working Servant server with Token used both in request and response headers:

Kind regards,
Nick

On Thu, 8 Nov 2018 at 13:06 Alp Mestanogullari <[hidden email]> wrote:
It should be (Proxy :: Proxy PlainText), not PlainText, in the RHS of the mimeRender definition.

Otherwise, I think you can make Token a newtype of Text and use GeneralizedNewtypeDeriving to get your MimeRender PlainText instance:

newtype Token = Token Text deriving (Eq, Show, MimeRender PlainText)


On Thu, Nov 8, 2018 at 10:56 AM Corentin Dupont <[hidden email]> wrote:
Cool!
So I tried:

data Token = Token Text deriving (Eq, Show)

instance FromHttpApiData KC.Token where
  parseHeader ((stripPrefix "Bearer ") . decodeUtf8 -> Just tok) = Right $ Token tok


However It seems I also need MimeRender (otherwise the function 'serve' complains that there is no instance:

instance MimeRender PlainText KC.Token where
  mimeRender _ (Token tok) = mimeRender PlainText tok


Is this correct?
It complains:
Data constructor not in scope: PlainText :: Proxy ctype0

Where can I find PlainText? I imported Servant.API.ContentTypes to no avail.


On Wed, Nov 7, 2018 at 6:43 PM erik <[hidden email]> wrote:

On Wed, Nov 7, 2018 at 9:20 AM Corentin Dupont <[hidden email]> wrote:
Hello,
I was wondering if there is a way to manipulate headers with Servant.
I have:

newtype Token = Token Text
type API = Header "Authorization" Token :> Get '[JSON] MyData

However, with this method I get "Bearer XXXXX". However, a Token should be only the XXX part...
Any way to extract it automatically?

Thanks
Corentin

--


--
Erik Aker

--


--
Alp Mestanogullari

--

--


--
Alp Mestanogullari

--
Reply | Threaded
Open this post in threaded view
|

MQTT

Corentin Dupont
Hello again,
is there any plan to support MQTT in Servant?
It's a rather simple protocol that defines PUBLISH and SUBSCRIBE (instead of POST/GET...).

--
Reply | Threaded
Open this post in threaded view
|

Re: Extract token from Headers

Corentin Dupont
In reply to this post by Nickolay Kudasov
Thanks a lot for your example, it is working now.

On Thu, Nov 8, 2018 at 12:57 PM Nickolay Kudasov <[hidden email]> wrote:
Corentin,

I think it is a bit strange that you're asked to provide MimeRender instance. What is your exact API?

Here's a minimal working Servant server with Token used both in request and response headers:

Kind regards,
Nick

On Thu, 8 Nov 2018 at 13:06 Alp Mestanogullari <[hidden email]> wrote:
It should be (Proxy :: Proxy PlainText), not PlainText, in the RHS of the mimeRender definition.

Otherwise, I think you can make Token a newtype of Text and use GeneralizedNewtypeDeriving to get your MimeRender PlainText instance:

newtype Token = Token Text deriving (Eq, Show, MimeRender PlainText)


On Thu, Nov 8, 2018 at 10:56 AM Corentin Dupont <[hidden email]> wrote:
Cool!
So I tried:

data Token = Token Text deriving (Eq, Show)

instance FromHttpApiData KC.Token where
  parseHeader ((stripPrefix "Bearer ") . decodeUtf8 -> Just tok) = Right $ Token tok


However It seems I also need MimeRender (otherwise the function 'serve' complains that there is no instance:

instance MimeRender PlainText KC.Token where
  mimeRender _ (Token tok) = mimeRender PlainText tok


Is this correct?
It complains:
Data constructor not in scope: PlainText :: Proxy ctype0

Where can I find PlainText? I imported Servant.API.ContentTypes to no avail.


On Wed, Nov 7, 2018 at 6:43 PM erik <[hidden email]> wrote:

On Wed, Nov 7, 2018 at 9:20 AM Corentin Dupont <[hidden email]> wrote:
Hello,
I was wondering if there is a way to manipulate headers with Servant.
I have:

newtype Token = Token Text
type API = Header "Authorization" Token :> Get '[JSON] MyData

However, with this method I get "Bearer XXXXX". However, a Token should be only the XXX part...
Any way to extract it automatically?

Thanks
Corentin

--


--
Erik Aker

--


--
Alp Mestanogullari

--

--
Reply | Threaded
Open this post in threaded view
|

Fwd: MQTT

Arian van Putten
In reply to this post by Corentin Dupont
And to the group. I forgot to hit reply all :)

---------- Forwarded message ---------
From: Arian van Putten <[hidden email]>
Date: Thu, Nov 8, 2018 at 5:24 PM
Subject: Re: MQTT
To: Corentin Dupont <[hidden email]>


Hey,
Brining more types to IoT seems like a cool idea :)

Here are just some quick thoughts by me googling about MQTT:

A quick glance at the standard, MQTT seems to be not an extension to HTTP (http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html) . It runs on top of TCP/IP,  just like HTTP, it has topics, similar to URLs in HTTP,
but the similarities seem to stop there.. For example, there doesn't seem to be any notion of content types of requests and responses, whilst content negotiation is one of the core features of Servant (in my opinion).
Because MQTT doesn't have this notion, I'm not sure how one would implement type-directed routing ala Servant. You would have to implement some notion of content types on top of MQTT, but that would perhaps make
it hard to interact with existing MQTT implementations.   Do you know if there are standard extensions that add this kind of functionality?

Secondly, from a glance, MQTT seems to be a very bi-directional protocol that supports many to many communication, whilst HTTP (and servant) is very client-server oriented.

I'm sure there is merit in doing a typed (servant-like) approach to MQTT topics, but I'm not sure if putting it in servant is the right approach. Given the very different natures of these protocols.
However, I do think the type-level DSL could be re-used to write
type-level MQTT topics, and I also think looking at the servant routing implementation (or this blog post https://www.well-typed.com/blog/2015/11/implementing-a-minimal-version-of-haskell-servant/) could
give a good idea how to implement a MQTT-based router for such a DSL.


If you have any questions for  implementing such a router yourself, feel free to ask them here, or poke me on IRC (#servant, arianvp).

On Thu, Nov 8, 2018, 16:43 Corentin Dupont <[hidden email] wrote:
Hello again,
is there any plan to support MQTT in Servant?
It's a rather simple protocol that defines PUBLISH and SUBSCRIBE (instead of POST/GET...).

--


--
Groetjes,

Arian

--
Reply | Threaded
Open this post in threaded view
|

Re: Extract token from Headers

Nickolay Kudasov
In reply to this post by Corentin Dupont
Alp,

Ah, I see, that makes sense now :)

Corentin, glad I was able to help anyway!

Kind regards,
Nick

On Thu, 8 Nov 2018 at 18:43 Corentin Dupont <[hidden email]> wrote:
Thanks a lot for your example, it is working now.

On Thu, Nov 8, 2018 at 12:57 PM Nickolay Kudasov <[hidden email]> wrote:
Corentin,

I think it is a bit strange that you're asked to provide MimeRender instance. What is your exact API?

Here's a minimal working Servant server with Token used both in request and response headers:

Kind regards,
Nick

On Thu, 8 Nov 2018 at 13:06 Alp Mestanogullari <[hidden email]> wrote:
It should be (Proxy :: Proxy PlainText), not PlainText, in the RHS of the mimeRender definition.

Otherwise, I think you can make Token a newtype of Text and use GeneralizedNewtypeDeriving to get your MimeRender PlainText instance:

newtype Token = Token Text deriving (Eq, Show, MimeRender PlainText)


On Thu, Nov 8, 2018 at 10:56 AM Corentin Dupont <[hidden email]> wrote:
Cool!
So I tried:

data Token = Token Text deriving (Eq, Show)

instance FromHttpApiData KC.Token where
  parseHeader ((stripPrefix "Bearer ") . decodeUtf8 -> Just tok) = Right $ Token tok


However It seems I also need MimeRender (otherwise the function 'serve' complains that there is no instance:

instance MimeRender PlainText KC.Token where
  mimeRender _ (Token tok) = mimeRender PlainText tok


Is this correct?
It complains:
Data constructor not in scope: PlainText :: Proxy ctype0

Where can I find PlainText? I imported Servant.API.ContentTypes to no avail.


On Wed, Nov 7, 2018 at 6:43 PM erik <[hidden email]> wrote:

On Wed, Nov 7, 2018 at 9:20 AM Corentin Dupont <[hidden email]> wrote:
Hello,
I was wondering if there is a way to manipulate headers with Servant.
I have:

newtype Token = Token Text
type API = Header "Authorization" Token :> Get '[JSON] MyData

However, with this method I get "Bearer XXXXX". However, a Token should be only the XXX part...
Any way to extract it automatically?

Thanks
Corentin

--


--
Erik Aker

--


--
Alp Mestanogullari

--

--

--