monban-core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.envrc +5 -0
  3. data/.git_release_request.rc.sh +7 -0
  4. data/.gitignore +9 -0
  5. data/.gitlab-ci.yml +12 -0
  6. data/.travis.yml +12 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.lock +36 -0
  9. data/LICENSE +21 -0
  10. data/README.md +51 -0
  11. data/Rakefile +10 -0
  12. data/lib/monban/core/version.rb +5 -0
  13. data/lib/monban/domain/auth.rb +273 -0
  14. data/lib/monban/domain/password.rb +51 -0
  15. data/lib/monban/use_case/account/admin.rb +49 -0
  16. data/lib/monban/use_case/account/change/email.rb +60 -0
  17. data/lib/monban/use_case/account/change/login_id.rb +57 -0
  18. data/lib/monban/use_case/account/change/password.rb +45 -0
  19. data/lib/monban/use_case/account/change/roles.rb +51 -0
  20. data/lib/monban/use_case/account/fetch.rb +36 -0
  21. data/lib/monban/use_case/account/register.rb +50 -0
  22. data/lib/monban/use_case/account/search.rb +48 -0
  23. data/lib/monban/use_case/account/unregister.rb +39 -0
  24. data/lib/monban/use_case/auth/account.rb +43 -0
  25. data/lib/monban/use_case/auth/change/authy.rb +50 -0
  26. data/lib/monban/use_case/auth/change/password.rb +49 -0
  27. data/lib/monban/use_case/auth/token/authy.rb +51 -0
  28. data/lib/monban/use_case/auth/token/full.rb +51 -0
  29. data/lib/monban/use_case/auth/token/general.rb +33 -0
  30. data/lib/monban/use_case/auth/token/renew.rb +70 -0
  31. data/lib/monban/use_case/auth/token/reset.rb +99 -0
  32. data/lib/monban/use_case/auth/token.rb +39 -0
  33. data/lib/monban/use_case/auth/verify/authy.rb +43 -0
  34. data/lib/monban/use_case/auth/verify/password.rb +65 -0
  35. data/lib/monban/use_case/auth/verify/reset_token.rb +41 -0
  36. data/lib/monban/use_case/base.rb +15 -0
  37. data/monban-core.gemspec +39 -0
  38. metadata +184 -0
@@ -0,0 +1,45 @@
1
+ require "monban/use_case/base"
2
+
3
+ require "getto/params"
4
+
5
+ module Monban
6
+ module UseCase
7
+ module Account
8
+ module Change
9
+ class Password < Base
10
+
11
+ initialize_with(
12
+ :error,
13
+ repository: [
14
+ :account_exists?,
15
+ ],
16
+ password: [:change],
17
+ )
18
+
19
+ def change(params)
20
+ Getto::Params.new.validate(params) do |v|
21
+ v.hash(
22
+ account_id: v.integer{|val| param_error!(account_id: val) },
23
+ password: v.combine([v.string, v.not_empty]), # DO NOT LOGGING PASSWORD!!
24
+ )
25
+ end or param_error!(params: "FILTERED")
26
+
27
+ repository.transaction do
28
+ unless repository.account_exists?(account_id: params[:account_id])
29
+ error.not_found! "account_id: #{params[:account_id]}"
30
+ end
31
+
32
+ self.password.change(
33
+ account_id: params[:account_id],
34
+ password: params[:password],
35
+ )
36
+ end
37
+
38
+ nil
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,51 @@
1
+ require "monban/use_case/base"
2
+
3
+ require "getto/params"
4
+
5
+ module Monban
6
+ module UseCase
7
+ module Account
8
+ module Change
9
+ class Roles < Base
10
+
11
+ initialize_with(
12
+ error: [:invalid_params!, :not_found!],
13
+ time: [:now],
14
+ repository: [
15
+ :transaction,
16
+ :account_exists?,
17
+ :update_roles,
18
+ :roles,
19
+ ],
20
+
21
+ accept_roles: Array,
22
+ )
23
+
24
+ def change(params)
25
+ Getto::Params.new.validate(params) do |v|
26
+ v.hash(
27
+ account_id: v.integer {|val| param_error!(account_id: val) },
28
+ roles: v.array_include(accept_roles.map(&:to_s)){|val| param_error!(roles: val) },
29
+ )
30
+ end or param_error!(params: params)
31
+
32
+ repository.transaction do
33
+ unless repository.account_exists?(account_id: params[:account_id])
34
+ error.not_found! "account_id: #{params[:account_id]}"
35
+ end
36
+
37
+ repository.update_roles(
38
+ account_id: params[:account_id],
39
+ roles: params[:roles],
40
+ now: time.now,
41
+ )
42
+
43
+ repository.roles(account_id: params[:account_id])
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+ require "monban/use_case/base"
2
+
3
+ require "getto/params"
4
+
5
+ module Monban
6
+ module UseCase
7
+ module Account
8
+ class Fetch < Base
9
+
10
+ initialize_with(
11
+ error: [:invalid_params!],
12
+ repository: [
13
+ :login_id,
14
+ :reset_password_email,
15
+ :roles,
16
+ ],
17
+ )
18
+
19
+ def fetch(params)
20
+ Getto::Params.new.validate(params) do |v|
21
+ v.hash(
22
+ account_id: v.integer{|val| param_error!(account_id: val) },
23
+ )
24
+ end or param_error!(params: params)
25
+
26
+ {
27
+ login_id: repository.login_id(account_id: params[:account_id]),
28
+ email: repository.reset_password_email(account_id: params[:account_id]),
29
+ roles: repository.roles(account_id: params[:account_id]),
30
+ }
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,50 @@
1
+ require "monban/use_case/base"
2
+
3
+ require "getto/params"
4
+
5
+ module Monban
6
+ module UseCase
7
+ module Account
8
+ class Register < Base
9
+
10
+ initialize_with(
11
+ error: [:invalid_params!, :conflict!],
12
+ time: [:now],
13
+ repository: [
14
+ :transaction,
15
+ :login_id_exists?,
16
+ :insert_account,
17
+ :update_login_id,
18
+ ],
19
+ )
20
+
21
+ def create(params)
22
+ Getto::Params.new.validate(params) do |v|
23
+ v.hash(
24
+ login_id: v.combine([v.string, v.not_empty]){|val| param_error!(login_id: val) },
25
+ )
26
+ end or param_error!(params: params)
27
+
28
+ repository.transaction do
29
+ if repository.login_id_exists?(login_id: params[:login_id])
30
+ error.conflict! "login_id already exists"
31
+ end
32
+
33
+ account_id = repository.insert_account(
34
+ now: time.now,
35
+ )
36
+
37
+ repository.update_login_id(
38
+ account_id: account_id,
39
+ login_id: params[:login_id],
40
+ now: time.now,
41
+ )
42
+
43
+ account_id
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,48 @@
1
+ require "monban/use_case/base"
2
+
3
+ require "getto/params"
4
+ require "getto/params/search"
5
+
6
+ module Monban
7
+ module UseCase
8
+ module Account
9
+ class Search < Base
10
+
11
+ initialize_with(
12
+ error: [:invalid_params!],
13
+ repository: [:search],
14
+
15
+ limit: Integer,
16
+ )
17
+
18
+ # :nocov:
19
+ def search(params)
20
+ Getto::Params.new.validate(params) do |v|
21
+ v.hash(
22
+ page: v.string{|val| param_error!(page: val) },
23
+ sort: v.in([
24
+ "login_id.asc",
25
+ "login_id.desc",
26
+ ]){|val| param_error!(sort: val) },
27
+ query: v.hash(
28
+ "login_id.cont" => v.string{|val| param_error!("login_id.cont": val) },
29
+ ),
30
+ )
31
+ end or param_error!(params: params)
32
+
33
+
34
+ repository.search(**(Getto::Params::Search.new(**params, limit: limit).to_h do |search|
35
+ search.sort do |s|
36
+ s.straight :login_id
37
+ end
38
+ search.query do |q|
39
+ q.search "login_id.cont", &q.not_empty
40
+ end
41
+ end.to_h))
42
+ end
43
+ # :nocov:
44
+
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,39 @@
1
+ require "monban/use_case/base"
2
+
3
+ require "getto/params"
4
+
5
+ module Monban
6
+ module UseCase
7
+ module Account
8
+ class Unregister < Base
9
+
10
+ initialize_with(
11
+ error: [:invalid_params!, :not_found!],
12
+ repository: [
13
+ :transaction,
14
+ :account_exists?,
15
+ :delete_account,
16
+ ],
17
+ )
18
+
19
+ def unregister(params)
20
+ Getto::Params.new.validate(params) do |v|
21
+ v.hash(
22
+ account_id: v.integer{|val| param_error!(account_id: val) },
23
+ )
24
+ end or param_error!(params: params)
25
+
26
+ repository.transaction do
27
+ unless repository.account_exists?(account_id: params[:account_id])
28
+ error.not_found! "account_id: #{params[:account_id]}"
29
+ end
30
+ repository.delete_account(account_id: params[:account_id])
31
+ end
32
+
33
+ nil
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,43 @@
1
+ require "monban/use_case/base"
2
+
3
+ require "getto/params"
4
+
5
+ module Monban
6
+ module UseCase
7
+ module Auth
8
+ class Account < Base
9
+
10
+ initialize_with(
11
+ error: [:invalid_params!, :invalid_account!],
12
+ time: [:now],
13
+
14
+ account: ::Hash,
15
+
16
+ repository: [
17
+ :account_id_by_public_id,
18
+ ],
19
+ )
20
+
21
+ def id
22
+ @id ||= begin
23
+ Getto::Params.new.validate(account) do |v|
24
+ v.hash(
25
+ public_id: v.combine([v.string, v.not_empty]){|val| param_error!(public_id: val) },
26
+ )
27
+ end or param_error!(account: account)
28
+
29
+ repository.account_id_by_public_id(
30
+ public_id: account[:public_id],
31
+ now: time.now,
32
+ ) or error.invalid_account! "public_id: #{account[:public_id]}"
33
+ end
34
+ end
35
+
36
+ def [](key)
37
+ account[key]
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,50 @@
1
+ require "monban/use_case/base"
2
+
3
+ require "getto/params"
4
+
5
+ module Monban
6
+ module UseCase
7
+ module Auth
8
+ module Change
9
+ class Authy < Base
10
+
11
+ initialize_with(
12
+ error: [:invalid_params!, :invalid_account!],
13
+ time: [:now],
14
+ authy: [:register_user],
15
+ repository: [
16
+ :transaction,
17
+ :update_authy_id,
18
+ ],
19
+ )
20
+
21
+ def change(params)
22
+ Getto::Params.new.validate(params) do |v|
23
+ v.hash(
24
+ account_id: v.integer {|val| param_error!(account_id: val) },
25
+ country_code: v.combine([v.string, v.not_empty]){|val| param_error!(country_code: val) },
26
+ phone_number: v.combine([v.string, v.not_empty]){|val| param_error!(phone_number: val) },
27
+ )
28
+ end or param_error!(params: params)
29
+
30
+ authy_id = authy.register_user(
31
+ country_code: params[:country_code],
32
+ phone_number: params[:phone_number],
33
+ ) or error.invalid_account! "params: #{params}"
34
+
35
+ repository.transaction do
36
+ repository.update_authy_id(
37
+ account_id: params[:account_id],
38
+ authy_id: authy_id,
39
+ now: time.now,
40
+ )
41
+ end
42
+
43
+ nil
44
+ end
45
+
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,49 @@
1
+ require "monban/use_case/base"
2
+
3
+ require "getto/params"
4
+
5
+ module Monban
6
+ module UseCase
7
+ module Auth
8
+ module Change
9
+ class Password < Base
10
+
11
+ initialize_with(
12
+ repository: [
13
+ :transaction,
14
+ :delete_reset_password_token,
15
+ :update_password_hash,
16
+ ],
17
+ time: [:now],
18
+
19
+ password: [:create]
20
+ )
21
+
22
+ def change(params)
23
+ Getto::Params.new.validate(params) do |v|
24
+ v.hash(
25
+ account_id: v.integer {|val| param_error!(account_id: val) },
26
+ password: v.combine([v.string, v.not_empty]){|val| param_error!(password: val) },
27
+ )
28
+ end or param_error!(params: params)
29
+
30
+ repository.transaction do
31
+ # disable current reset-password token
32
+ # when user change own password
33
+ repository.delete_reset_password_token(account_id: params[:account_id])
34
+
35
+ repository.update_password_hash(
36
+ account_id: params[:account_id],
37
+ password_hash: password.create(password: params[:password]),
38
+ now: time.now,
39
+ )
40
+ end
41
+
42
+ nil
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,51 @@
1
+ require "monban/use_case/base"
2
+ require "monban/use_case/auth/token"
3
+
4
+ require "getto/params"
5
+
6
+ module Monban
7
+ module UseCase
8
+ module Auth
9
+ module Token
10
+ class Authy < Base
11
+ include Token::Helper
12
+
13
+ initialize_with(
14
+ error: [:invalid_params!, :server_error!],
15
+ logger: [:log],
16
+ time: [:now],
17
+ token: [:create],
18
+ auth: [:authy],
19
+
20
+ expire: Integer,
21
+
22
+ repository: [
23
+ :transaction,
24
+ :public_id_exists?,
25
+ :insert_public_id,
26
+ :authy_id,
27
+ :login_id,
28
+ ],
29
+ )
30
+
31
+ def create(params)
32
+ Getto::Params.new.validate(params) do |v|
33
+ v.hash(
34
+ account_id: v.integer{|val| param_error!(account_id: val) },
35
+ )
36
+ end or param_error!(params: params)
37
+
38
+ repository.transaction do
39
+ auth.authy(
40
+ public_id: generate_public_id!(account_id: params[:account_id]),
41
+ authy_id: repository.authy_id(account_id: params[:account_id]),
42
+ expired_at: time.now + expire,
43
+ )
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,51 @@
1
+ require "monban/use_case/base"
2
+ require "monban/use_case/auth/token"
3
+
4
+ require "getto/params"
5
+
6
+ module Monban
7
+ module UseCase
8
+ module Auth
9
+ module Token
10
+ class Full < Base
11
+ include Token::Helper
12
+
13
+ initialize_with(
14
+ error: [:invalid_params!, :server_error!],
15
+ logger: [:log],
16
+ time: [:now],
17
+ token: [:create],
18
+ auth: [:full],
19
+
20
+ expire: Integer,
21
+
22
+ repository: [
23
+ :transaction,
24
+ :public_id_exists?,
25
+ :insert_public_id,
26
+ :roles,
27
+ :login_id,
28
+ ],
29
+ )
30
+
31
+ def create(params)
32
+ Getto::Params.new.validate(params) do |v|
33
+ v.hash(
34
+ account_id: v.integer{|val| param_error!(account_id: val) },
35
+ )
36
+ end or param_error!(params: params)
37
+
38
+ repository.transaction do
39
+ auth.full(
40
+ public_id: generate_public_id!(account_id: params[:account_id]),
41
+ roles: repository.roles(account_id: params[:account_id]),
42
+ expired_at: time.now + expire,
43
+ )
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,33 @@
1
+ require "getto/initialize_with"
2
+
3
+ require "monban/use_case/auth/token/full"
4
+ require "monban/use_case/auth/token/authy"
5
+
6
+ module Monban
7
+ module UseCase
8
+ module Auth
9
+ module Token
10
+ class General
11
+ include Getto::InitializeWith
12
+
13
+ initialize_with(
14
+ error: [:server_error!],
15
+ login: Symbol,
16
+ full: Full,
17
+ authy: Authy,
18
+ )
19
+
20
+ def create(account_id:)
21
+ case login
22
+ when :full then full.create(account_id: account_id)
23
+ when :authy then authy.create(account_id: account_id)
24
+ else
25
+ error.server_error! "invalid login: #{login}"
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,70 @@
1
+ require "monban/use_case/base"
2
+ require "monban/use_case/auth/token"
3
+
4
+ require "getto/params"
5
+
6
+ module Monban
7
+ module UseCase
8
+ module Auth
9
+ module Token
10
+ class Renew < Base
11
+ include Token::Helper
12
+
13
+ initialize_with(
14
+ error: [:invalid_params!, :renew_token_expired!, :server_error!],
15
+ logger: [:log],
16
+ time: [:now],
17
+ token: [:create],
18
+ auth: [:full],
19
+
20
+ expire: Integer,
21
+ renew_expire: Integer,
22
+
23
+ repository: [
24
+ :transaction,
25
+ :public_id_renew_enabled?,
26
+ :public_id_original_created_at,
27
+ :public_id_exists?,
28
+ :insert_public_id,
29
+ :preserve_public_id_original_created_at,
30
+ :roles,
31
+ :login_id,
32
+ ],
33
+ )
34
+
35
+ def create(params)
36
+ Getto::Params.new.validate(params) do |v|
37
+ v.hash(
38
+ account_id: v.integer {|val| param_error!(account_id: val) },
39
+ public_id: v.combine([v.string, v.not_empty]){|val| param_error!(public_id: val) },
40
+ )
41
+ end or param_error!(params: params)
42
+
43
+ repository.transaction do
44
+ repository.public_id_renew_enabled?(
45
+ public_id: params[:public_id],
46
+ original_created_at: time.now - renew_expire,
47
+ ) or error.renew_token_expired!
48
+
49
+ original_created_at = repository.public_id_original_created_at(public_id: params[:public_id])
50
+
51
+ new_public_id = generate_public_id!(account_id: params[:account_id])
52
+
53
+ repository.preserve_public_id_original_created_at(
54
+ public_id: new_public_id,
55
+ original_created_at: original_created_at,
56
+ )
57
+
58
+ auth.full(
59
+ public_id: new_public_id,
60
+ roles: repository.roles(account_id: params[:account_id]),
61
+ expired_at: time.now + expire,
62
+ )
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end