New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MemoryStore #39

Merged
merged 1 commit into from Nov 8, 2018

Conversation

Projects
None yet
4 participants
@DmitryTsepelev
Contributor

DmitryTsepelev commented Oct 20, 2018

Here is a very basic implementation of abstract adapter for storages.

The idea is that we keep all the code we had in the Redis adapter and just use the appropriate store object for the each specific implementation. This object should be able to respond to all the methods we're using (get, expire at etc), for the in-memory adapter I've used a mock_redis gem (if we don't want to have this dependency - I can replace it with my own implementation). If we once decide to add for instance a postgres support - we'd just need to write the class implementing all the methods we use.

Note: I've tried to keep it as much backward compatible as possible but I'm not sure if we really need to configure things like token_prefix on the top level but if we want to have it - I'd bring it back.

@DmitryTsepelev DmitryTsepelev force-pushed the DmitryTsepelev:memory_store branch from 6841e26 to fc10cdf Oct 20, 2018

@tuwukee

Hi @DmitryTsepelev!

Thank you, that's a nice work!

But at the moment the main goal is to get rid of extra dependency (redis), and not to introduce a new one (mock_redis). So far as we're using a pretty limited subset of redis functionality it should be possible to build a simple memory store from scratch.

@DmitryTsepelev DmitryTsepelev force-pushed the DmitryTsepelev:memory_store branch 14 times, most recently from b6e3684 to b4cbffc Oct 20, 2018

@DmitryTsepelev

This comment has been minimized.

Contributor

DmitryTsepelev commented Oct 21, 2018

Hi @tuwukee, thanks for the review!

I've added a MemoryStore (+ tests) implementing all the Redis methods which are in use. Also, I've removed ||= from adapters and added it to the JWTSessions#token_store method to make testing easier.

@tuwukee

Hi @DmitryTsepelev,

thanks, the changes look great!
Added a few comments, please take a look.


DEFAULT_REDIS_HOST = '127.0.0.1'
DEFAULT_REDIS_PORT = '6379'
DEFAULT_REDIS_DB_NAME = '0'
DEFAULT_TOKEN_PREFIX = 'jwt_'

This comment has been minimized.

@tuwukee

tuwukee Oct 22, 2018

Owner

Removal of token prefix breaks backward compatibility, please bring it back.

This comment has been minimized.

@DmitryTsepelev

module JWTSessions
module CamelizedString
refine String do

This comment has been minimized.

@tuwukee

tuwukee Oct 22, 2018

Owner

I'd prefer to not use refinements, it's rather a questionable feature.

This comment has been minimized.

@DmitryTsepelev
Show resolved Hide resolved lib/jwt_sessions.rb Outdated
Show resolved Hide resolved test/units/jwt_sessions/test_memory_store.rb Outdated
@@ -7,6 +7,7 @@ class TestRefreshToken < Minitest::Test
attr_reader :csrf, :token, :access_uid

def setup
JWTSessions.token_store = :redis

This comment has been minimized.

@tuwukee

tuwukee Oct 22, 2018

Owner

As a side note: there're kind of integration tests, defined within sinatra/rails dummy apps.
It'd be great to run them with memory store (and generic unit tests also).
Can we extend the Rakefile with rake tasks to run the same test suites, but using memory store as default?

This comment has been minimized.

@DmitryTsepelev

DmitryTsepelev Oct 22, 2018

Contributor

working on it

@DmitryTsepelev DmitryTsepelev force-pushed the DmitryTsepelev:memory_store branch 6 times, most recently from f15b754 to a367f43 Oct 23, 2018

Show resolved Hide resolved lib/jwt_sessions.rb Outdated

@DmitryTsepelev DmitryTsepelev force-pushed the DmitryTsepelev:memory_store branch 5 times, most recently from a4b9269 to b253aa5 Oct 23, 2018

@DmitryTsepelev DmitryTsepelev force-pushed the DmitryTsepelev:memory_store branch 3 times, most recently from 4636de7 to 7499ccf Oct 29, 2018

@DmitryTsepelev

This comment has been minimized.

Contributor

DmitryTsepelev commented Oct 29, 2018

As a followup for #39 (comment), I've redesigned API for adapters.

Now each descendant has to implement the following public methods:

  • fetch_access
  • persist_access
  • fetch_refresh
  • persist_refresh
  • update_refresh
  • all_in_namespace
  • destroy_refresh
  • destroy_access

Also, the following protected methods should be implemented too:

  • configure_store (I can't imagine an adapter which does not need a post-initialization hook configure_store)
  • wildcard_refresh_key (used in refresh_key)
@palkan

This comment has been minimized.

Contributor

palkan commented Oct 29, 2018

I can't imagine an adapter which does not need a post-initialization hook configure_store

Why can't we use #initialize? (and let adapter decide whether it needs prefix or not, for example, in-memory adapter doesn't need it, does it?)

@DmitryTsepelev

This comment has been minimized.

Contributor

DmitryTsepelev commented Oct 30, 2018

Okay, prefix is left read-only at the top level and concrete adapters can define it if needed, all the configuration happens inside the initialize. Also, I've removed the part that made new private - it's not a public API anymore, and the user has to use symbols anyway.

@tuwukee

looks good to me

@DmitryTsepelev DmitryTsepelev force-pushed the DmitryTsepelev:memory_store branch 13 times, most recently from 4d40771 to cf032be Oct 30, 2018

store.persist_refresh('uid', expiration, 'access_uid', 'csrf', expiration, 'ns')
store.persist_refresh('uid', expiration, 'access_uid', 'csrf', expiration, 'ns2')
refresh_tokens = store.all('ns')
assert_equal refresh_tokens.count, 1

This comment has been minimized.

@batizhevsky

batizhevsky Oct 31, 2018

I suppose arguments should be in the different order:
assert_equal(exp, act, msg = nil)

This comment has been minimized.

@DmitryTsepelev

DmitryTsepelev Oct 31, 2018

Contributor

agreed, let me check all other specs

@DmitryTsepelev DmitryTsepelev force-pushed the DmitryTsepelev:memory_store branch from c104850 to 02505f3 Oct 31, 2018

@DmitryTsepelev

This comment has been minimized.

Contributor

DmitryTsepelev commented Oct 31, 2018

Rebased once again, I guess it's ready to be merged unless there are any other issues you see :)

@palkan

palkan approved these changes Oct 31, 2018

@tuwukee tuwukee changed the base branch from master to pre-release Nov 8, 2018

@tuwukee tuwukee merged commit 8d63dc2 into tuwukee:pre-release Nov 8, 2018

2 checks passed

Codacy/PR Quality Review Up to standards. A positive pull request.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment