matrix-client-haskell icon indicating copy to clipboard operation
matrix-client-haskell copied to clipboard

expose auth token in ClientSession?

Open solomon-b opened this issue 3 years ago • 6 comments
trafficstars

I want to use the login function to generate auth tokens for later use in a bot. I wrote login to return a ClientSession so that passwords can be used as an alternative to an auth token when starting a session (such as if the lib were to be used for build a user facing app rather then a bot).

I think both use cases are totally valid. How would you feel about adding a function to extract just the auth token from a ClientSession?

solomon-b avatar Jan 15 '22 18:01 solomon-b

How about adding a loginToken :: LoginCredentials -> MatrixIO (ClientSession, MatrixToken) instead, and make the current login be fst <$> loginToken cred ? Or perhaps we could simply change the current function. Also I noticed login is failing on error, and perhaps it would be better to make it return a MatrixIO with the error in Left?

If that does not work, then sure, I don't mind adding an accessor for the token.

TristanCacqueray avatar Jan 16 '22 17:01 TristanCacqueray

We might also want to add a loginCacheToken helper that would copy the token in ~/.cache/haskell-matrix-client/token and re-use it on subsequent call to avoid leaking access tokens. That might be a better solution, and perhaps that should be the default behavior of the login function.

TristanCacqueray avatar Jan 16 '22 17:01 TristanCacqueray

Oh these are all great suggestions. I'll put together a PR this week. I'm not sure why I didn't put login in MatrixIO to begin with..

so we want:

  1. loginToken :: LoginCredentials -> MatrixIO (ClientSession, MatrixToken) for generating access tokens,
  2. login should return MatrixIO
  3. Possibly, change login to use a token stored in the xdg cache directory?

For 3 i would prefer login to take a path to the token on disk so that I can specify a cache folder for whatever application i am building with matrix-client.

solomon-b avatar Jan 19 '22 02:01 solomon-b

Thank you for the feedback and the contributions, the cofree-bot looks great!

Thinking about it some more, caching the token can be tricky, how the caller would know if the login will work without credentials? Thus I would simply replace the current login with loginToken, and let the caller deal with the token.

TristanCacqueray avatar Jan 19 '22 03:01 TristanCacqueray

For example, a cache aware interface could take a IO LoginCredentials action as a parameter, and it would run it if the cached token is missing or invalid. But I think that's a bit out of scope for this library...

TristanCacqueray avatar Jan 19 '22 03:01 TristanCacqueray

Thank you for the feedback and the contributions, the cofree-bot looks great!

Thank you!

For example, a cache aware interface could take a IO LoginCredentials action as a parameter, and it would run it if the cached token is missing or invalid. But I think that's a bit out of scope for this library...

I agree that caching is likely out of scope for the library and should be handled by downstream users.

I'll write a PR without the caching bit then we can sort it out in the PR.

solomon-b avatar Jan 19 '22 06:01 solomon-b

Fixed by #19

TristanCacqueray avatar Mar 14 '23 13:03 TristanCacqueray