magic-admin 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,187 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe MagicAdmin::Resource::Token do
6
+ describe "instance methods" do
7
+ describe "public methods" do
8
+ it "#validate" do
9
+ claim = { "ext" => 1000, "nbf" => "nbf" }
10
+ rec_address = double("rec_address")
11
+ proof = double("proof")
12
+ time_now = 1_202_020
13
+ allow(Time).to receive(:now).and_return(time_now)
14
+
15
+ expect(subject).to receive(:decode).and_return([proof, claim])
16
+ expect(subject).to receive(:validate_claim_fields!).with(claim)
17
+
18
+ expect(subject).to receive(:rec_pub_address)
19
+ .with(claim, proof)
20
+ .and_return(rec_address)
21
+
22
+ expect(subject).to receive(:validate_public_address!)
23
+ .with(rec_address, spec_did_token)
24
+
25
+ expect(subject).to receive(:validate_claim_ext!)
26
+ .with(time_now, claim["ext"])
27
+
28
+ expect(subject).to receive(:validate_claim_nbf!)
29
+ .with(time_now, claim["nbf"])
30
+
31
+ subject.validate(spec_did_token)
32
+ end
33
+
34
+ context "#decode" do
35
+ it "with valid token" do
36
+ clam_hash = {
37
+ "iat" => 1_234_512_345,
38
+ "ext" => 1_234_512_345,
39
+ "iss" => "did:ethr:0xtest0000test0000test0000test0000test0000",
40
+ "sub" => "00abcdefghijklmnopqrstuvwxyz123456789000000=",
41
+ "aud" => "did:magic:testtest-test-test-test-testtesttest",
42
+ "nbf" => 1_234_512_345,
43
+ "tid" => "testtest-test-test-test-testtesttest",
44
+ "add" => "0x00test0000test0000test0000test0000test0000"\
45
+ "test0000test0000test0000test0000test0000test"\
46
+ "0000test0000test0000test0000test0000test0000"
47
+ }
48
+
49
+ proof_str = "0xtest0000test0000test0000test0000test0000"\
50
+ "test0000test0000test0000test0000test0000test"\
51
+ "0000test0000test0000test0000test0000test000000"
52
+
53
+ decode_val = [proof_str, clam_hash]
54
+
55
+ expect(subject.decode(spec_did_token)).to eq(decode_val)
56
+ end
57
+
58
+ it "with invalid token" do
59
+ allow(subject).to receive(:base64_decode)
60
+ .with(spec_did_token)
61
+ .and_return("")
62
+
63
+ expect do
64
+ subject.decode(spec_did_token)
65
+ end .to raise_error(MagicAdmin::DIDTokenError,
66
+ "DID Token is malformed")
67
+ end
68
+ end
69
+
70
+ context "#construct_issuer_with_public_address" do
71
+ it "return format" do
72
+ public_address = subject.construct_issuer_with_public_address("test_address")
73
+ expected = "did:ethr:test_address"
74
+ expect(public_address).to eq(expected)
75
+ end
76
+ end
77
+
78
+ context "#get_issuer" do
79
+ it "return format" do
80
+ issuer = subject.get_issuer(spec_did_token)
81
+ expected = "did:ethr:0xtest0000test0000test0000test0000test0000"
82
+ expect(issuer).to eq(expected)
83
+ end
84
+ end
85
+
86
+ context "#get_public_address" do
87
+ it "return format" do
88
+ public_address = subject.get_public_address(spec_did_token)
89
+ expected = "0xtest0000test0000test0000test0000test0000"
90
+ expect(public_address).to eq(expected)
91
+ end
92
+ end
93
+ end
94
+
95
+ describe "private methods" do
96
+ it "#claim_fields" do
97
+ fields = subject.send(:claim_fields)
98
+ expect(fields).to eq(%w[iat ext iss sub aud nbf tid])
99
+ end
100
+
101
+ context "#validate_claim_fields!" do
102
+ it "return true when claim keys match with claim_fields " do
103
+ allow(subject).to receive(:claim_fields).and_return([])
104
+
105
+ expect(subject.send(:validate_claim_fields!, {})).to be_truthy
106
+ end
107
+
108
+ it "raise error when claim keys does not match with claim_fields " do
109
+ fields = %w[iat ext iss sub aud nbf tid]
110
+ msg = "DID Token missing required fields: "
111
+ msg += fields.join(", ")
112
+ expect do
113
+ subject.send(:validate_claim_fields!, { invalid: nil })
114
+ end .to raise_error(MagicAdmin::DIDTokenError, msg)
115
+ end
116
+ end
117
+
118
+ context "#validate_public_address!" do
119
+ it "return true when rec_address eq did_token public_address" do
120
+ allow(subject).to receive(:get_public_address)
121
+ .with(spec_did_token)
122
+ .and_return("test_123")
123
+
124
+ expect(subject.send(:validate_public_address!,
125
+ "test_123",
126
+ spec_did_token)).to be_truthy
127
+ end
128
+
129
+ it "raise error when rec_address not eq did_token public_address" do
130
+ msg = "Signature mismatch between 'proof' and 'claim'."
131
+ expect do
132
+ subject.send(:validate_public_address!,
133
+ "test_123",
134
+ spec_did_token)
135
+ end .to raise_error(MagicAdmin::DIDTokenError, msg)
136
+ end
137
+ end
138
+
139
+ context "#validate_claim_ext!" do
140
+ it "return true when time is not grater claim_ext" do
141
+ time = Time.now.to_i
142
+ claim_ext = time + 100
143
+
144
+ expect(subject.send(:validate_claim_ext!,
145
+ time,
146
+ claim_ext)).to be_truthy
147
+ end
148
+
149
+ it "raise error when time is grater claim_ext" do
150
+ time = Time.now.to_i
151
+ claim_ext = time - 100
152
+ msg = "Given DID token has expired. Please generate a new one."
153
+ expect do
154
+ subject.send(:validate_claim_ext!, time, claim_ext)
155
+ end .to raise_error(MagicAdmin::DIDTokenError, msg)
156
+ end
157
+ end
158
+
159
+ context "#validate_claim_nbf using apply_nbf_grace_period claim_nbf" do
160
+ it "when time is not less then return true" do
161
+ time = Time.now.to_i
162
+ claim_nbf = double("claim_nbf")
163
+ allow(subject).to receive(:apply_nbf_grace_period)
164
+ .with(claim_nbf)
165
+ .and_return(time - 100)
166
+
167
+ expect(subject.send(:validate_claim_nbf!,
168
+ time,
169
+ claim_nbf)).to be_truthy
170
+ end
171
+
172
+ it "when time is less then raise error" do
173
+ time = Time.now.to_i
174
+ claim_nbf = double("claim_nbf")
175
+ msg = "Given DID token cannot be used at this time."
176
+ allow(subject).to receive(:apply_nbf_grace_period)
177
+ .with(claim_nbf)
178
+ .and_return(time + 100)
179
+
180
+ expect do
181
+ subject.send(:validate_claim_nbf!, time, claim_nbf)
182
+ end .to raise_error(MagicAdmin::DIDTokenError, msg)
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe MagicAdmin::Resource::User do
6
+ let(:magic) { Magic.new(api_secret_key: spec_api_secret_key) }
7
+ let(:public_address) do
8
+ MagicAdmin::Resource::Token.new.get_public_address(spec_did_token)
9
+ end
10
+
11
+ let(:issuer) do
12
+ MagicAdmin::Resource::Token.new.get_issuer(spec_did_token)
13
+ end
14
+
15
+ let(:construct_issuer_with_public_address) do
16
+ MagicAdmin::Resource::Token.new.construct_issuer_with_public_address(public_address)
17
+ end
18
+
19
+ let(:stub_response_body) do
20
+ { "data" => {}, "error_code" => "", "message" => "", "status" => "ok" }
21
+ end
22
+
23
+ subject { described_class.new(magic) }
24
+
25
+ it "present attr readers" do
26
+ expect(subject).to respond_to(:magic)
27
+ end
28
+
29
+ context "#get_metadata_by_issuer" do
30
+ it "send request with options" do
31
+ allow(MagicAdmin::Util).to receive(:headers)
32
+ .with(magic.secret_key)
33
+ .and_return({})
34
+ expect(magic.http_client).to receive(:call)
35
+ .with(:get,
36
+ "/v1/admin/auth/user/get",
37
+ {
38
+ params: { issuer: issuer }, headers: {}
39
+ })
40
+
41
+ subject.get_metadata_by_issuer(issuer)
42
+ end
43
+ end
44
+
45
+ context "#get_metadata_by_public_address" do
46
+ it "return response" do
47
+ url = "https://api.magic.link/v1/admin/auth/user/get?issuer="
48
+ url += construct_issuer_with_public_address
49
+ stub_request(:get, url)
50
+ .to_return(status: 200, body: stub_response_body.to_json, headers: {})
51
+ reps = subject.get_metadata_by_public_address(public_address)
52
+ expect(reps.status_code).to eq(200)
53
+ end
54
+ end
55
+
56
+ context "#get_metadata_by_token" do
57
+ it "return response" do
58
+ url = "https://api.magic.link/v1/admin/auth/user/get?issuer="
59
+ url += issuer
60
+ stub_request(:get, url)
61
+ .to_return(status: 200, body: stub_response_body.to_json, headers: {})
62
+ reps = subject.get_metadata_by_token(spec_did_token)
63
+
64
+ expect(reps.status_code).to eq(200)
65
+ end
66
+ end
67
+
68
+ context "#logout_by_issuer" do
69
+ it "return response" do
70
+ url = "https://api.magic.link/v2/admin/auth/user/logout"
71
+ stub_request(:post, url)
72
+ .to_return(status: 200, body: stub_response_body.to_json, headers: {})
73
+ reps = subject.logout_by_issuer(issuer)
74
+
75
+ expect(reps.status_code).to eq(200)
76
+ end
77
+ end
78
+
79
+ context "#logout_by_public_address" do
80
+ it "return response" do
81
+ url = "https://api.magic.link/v2/admin/auth/user/logout"
82
+ stub_request(:post, url)
83
+ .to_return(status: 200, body: stub_response_body.to_json, headers: {})
84
+ reps = subject.logout_by_public_address(public_address)
85
+ expect(reps.status_code).to eq(200)
86
+ end
87
+ end
88
+
89
+ context "#logout_by_token" do
90
+ it "return response" do
91
+ url = "https://api.magic.link/v2/admin/auth/user/logout"
92
+ stub_request(:post, url)
93
+ .to_return(status: 200, body: stub_response_body.to_json, headers: {})
94
+ reps = subject.logout_by_token(spec_did_token)
95
+ expect(reps.status_code).to eq(200)
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/setup"
4
+ Bundler.setup
5
+
6
+ require 'simplecov'
7
+ SimpleCov.start
8
+
9
+ require "magic-admin"
10
+ require "webmock/rspec"
11
+
12
+ RSpec.configure do |config|
13
+ # Add custom RSpec configuration here
14
+ end
15
+
16
+ # Description:
17
+ # Method provide sample did_token for configure specs
18
+ #
19
+ # Returns:
20
+ # did_token
21
+ #
22
+
23
+ def spec_did_token
24
+ "WyIweHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDA"\
25
+ "wMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMH"\
26
+ "Rlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwMDAiLCJ7XCJpYXRcIjoxMjM0N"\
27
+ "TEyMzQ1LFwiZXh0XCI6MTIzNDUxMjM0NSxcImlzc1wiOlwiZGlkOmV0aHI6MHh0ZXN0"\
28
+ "MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwXCIsXCJzdWJcIjpcIjA"\
29
+ "wYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwMDAwMDA9XCIsXCJhdW"\
30
+ "RcIjpcImRpZDptYWdpYzp0ZXN0dGVzdC10ZXN0LXRlc3QtdGVzdC10ZXN0dGVzdHRlc"\
31
+ "3RcIixcIm5iZlwiOjEyMzQ1MTIzNDUsXCJ0aWRcIjpcInRlc3R0ZXN0LXRlc3QtdGVz"\
32
+ "dC10ZXN0LXRlc3R0ZXN0dGVzdFwiLFwiYWRkXCI6XCIweDAwdGVzdDAwMDB0ZXN0MDA"\
33
+ "wMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMH"\
34
+ "Rlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc"\
35
+ "3QwMDAwdGVzdDAwMDBcIn0iXQ=="
36
+ end
37
+
38
+ # Description:
39
+ # Method provide sample api_secret_key for configure specs
40
+ #
41
+ # Returns:
42
+ # api_secret_key
43
+ #
44
+
45
+ def spec_api_secret_key
46
+ "sk_test_TESTTESTTESTTEST"
47
+ end
48
+
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe MagicAdmin::Util, type: :module do
6
+ context ".platform_info" do
7
+ it "return respond" do
8
+ expect(described_class.platform_info).to be_kind_of(Hash)
9
+ end
10
+
11
+ it "include keys" do
12
+ expect(described_class.platform_info).to include(:platform,
13
+ :language,
14
+ :language_version,
15
+ :user_name)
16
+ end
17
+ end
18
+
19
+ context ".user_agent" do
20
+ it "return respond" do
21
+ expect(described_class.user_agent).to be_kind_of(Hash)
22
+ end
23
+
24
+ it "include keys" do
25
+ expect(described_class.user_agent).to include(:sdk_version,
26
+ :publisher,
27
+ :platform)
28
+ end
29
+ end
30
+
31
+ context ".headers" do
32
+ it "return respond" do
33
+ expect(described_class.headers(spec_api_secret_key)).to be_kind_of(Hash)
34
+ end
35
+
36
+ it "include keys" do
37
+ expect(described_class.headers(spec_api_secret_key)).to include(
38
+ :"content-type", :"X-Magic-Secret-Key", :"User-Agent"
39
+ )
40
+ end
41
+ end
42
+ end
metadata CHANGED
@@ -1,27 +1,144 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magic-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Troll Goat
8
- autorequire:
7
+ - Magic Labs Inc.
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-30 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Magic
14
- email: arthur@fortmatic.com
15
- executables: []
11
+ date: 2020-10-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '11.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '11.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.80'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.80'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.19'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.19'
97
+ description: The Magic Admin Ruby provides convenient ways for developers to interact
98
+ with Magic API endpointsand an array of utilities to handle DID Token.
99
+ email: support@magic.link
100
+ executables:
101
+ - magic-console
16
102
  extensions: []
17
103
  extra_rdoc_files: []
18
104
  files:
105
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
106
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
107
+ - ".github/ISSUE_TEMPLATE/question.md"
108
+ - ".github/PULL_REQUEST_TEMPLATE.md"
109
+ - ".gitignore"
110
+ - ".rspec"
111
+ - ".rubocop.yml"
112
+ - CHANGELOG.md
113
+ - CONTRIBUTING.md
114
+ - Gemfile
115
+ - LICENSE.txt
116
+ - README.md
117
+ - bin/magic-console
19
118
  - lib/magic-admin.rb
20
- homepage:
119
+ - lib/magic-admin/config.rb
120
+ - lib/magic-admin/errors.rb
121
+ - lib/magic-admin/http/client.rb
122
+ - lib/magic-admin/http/request.rb
123
+ - lib/magic-admin/http/response.rb
124
+ - lib/magic-admin/resource/token.rb
125
+ - lib/magic-admin/resource/user.rb
126
+ - lib/magic-admin/util.rb
127
+ - lib/magic-admin/version.rb
128
+ - magic-admin.gemspec
129
+ - test/http/client_test.rb
130
+ - test/http/request_test.rb
131
+ - test/http/response_test.rb
132
+ - test/magic_test.rb
133
+ - test/resource/token_test.rb
134
+ - test/resource/user_test.rb
135
+ - test/spec_helper.rb
136
+ - test/util_test.rb
137
+ homepage: https://docs.magic.link/admin-sdk/ruby
21
138
  licenses:
22
139
  - MIT
23
140
  metadata: {}
24
- post_install_message:
141
+ post_install_message:
25
142
  rdoc_options: []
26
143
  require_paths:
27
144
  - lib
@@ -29,15 +146,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
29
146
  requirements:
30
147
  - - ">="
31
148
  - !ruby/object:Gem::Version
32
- version: '0'
149
+ version: 2.5.0
33
150
  required_rubygems_version: !ruby/object:Gem::Requirement
34
151
  requirements:
35
152
  - - ">="
36
153
  - !ruby/object:Gem::Version
37
154
  version: '0'
38
155
  requirements: []
39
- rubygems_version: 3.0.6
40
- signing_key:
156
+ rubygems_version: 3.1.2
157
+ signing_key:
41
158
  specification_version: 4
42
- summary: Magic
43
- test_files: []
159
+ summary: Ruby bindings for the Magic Admin API
160
+ test_files:
161
+ - test/http/client_test.rb
162
+ - test/http/request_test.rb
163
+ - test/http/response_test.rb
164
+ - test/magic_test.rb
165
+ - test/resource/token_test.rb
166
+ - test/resource/user_test.rb
167
+ - test/spec_helper.rb
168
+ - test/util_test.rb