Skip to content

Commit 1fe5863

Browse files
authored
New challenge/session methods (#51)
* fix gem release process * add lint step * use correct ruby version * fix indentation * add challenge methods * add methods * add sessions methods * bump version * fix claim_challenge params * remove trailing comma
1 parent 6bad1eb commit 1fe5863

File tree

7 files changed

+235
-12
lines changed

7 files changed

+235
-12
lines changed

.github/workflows/main.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@ on:
88
pull_request:
99

1010
jobs:
11-
build:
11+
lint:
1212
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: Set up Ruby
16+
uses: ruby/setup-ruby@v1
17+
with:
18+
ruby-version: '2.6'
19+
bundler-cache: true
20+
- name: Run RuboCop
21+
run: bundle exec rubocop
1322

23+
test:
24+
runs-on: ubuntu-latest
1425
strategy:
1526
matrix:
1627
ruby:
@@ -19,11 +30,11 @@ jobs:
1930
AUTHSIGNAL_API_URL: ${{ secrets.AUTHSIGNAL_API_URL }}
2031
AUTHSIGNAL_API_SECRET_KEY: ${{ secrets.AUTHSIGNAL_API_SECRET_KEY }}
2132
steps:
22-
- uses: actions/checkout@v2
23-
- name: Set up Ruby
24-
uses: ruby/setup-ruby@v1
25-
with:
26-
ruby-version: ${{ matrix.ruby }}
27-
bundler-cache: true
28-
- name: Run the default task
29-
run: bundle exec rake
33+
- uses: actions/checkout@v4
34+
- name: Set up Ruby
35+
uses: ruby/setup-ruby@v1
36+
with:
37+
ruby-version: ${{ matrix.ruby }}
38+
bundler-cache: true
39+
- name: Run tests
40+
run: bundle exec rake

.rubocop.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ Metrics/BlockLength:
2222
Metrics/ParameterLists:
2323
Max: 15
2424

25-
# SDK classes can be longer
25+
# SDK classes/modules can be longer
2626
Metrics/ClassLength:
27+
Max: 250
28+
29+
Metrics/ModuleLength:
2730
Max: 150
2831

2932
Metrics/MethodLength:

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.6.10

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
authsignal-ruby (5.2.2)
4+
authsignal-ruby (5.3.0)
55
base64
66
faraday (>= 2.0.1)
77
faraday-retry (~> 2.2)

lib/authsignal.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def get_user(user_id:)
3939
handle_response(response)
4040
end
4141

42+
def query_users(**options)
43+
response = Client.new.query_users(**options)
44+
45+
handle_response(response)
46+
end
47+
4248
def update_user(user_id:, attributes:)
4349
response = Client.new.update_user(user_id: user_id, attributes: attributes)
4450

@@ -86,12 +92,72 @@ def get_action(user_id:, action:, idempotency_key:)
8692
handle_response(response)
8793
end
8894

95+
def query_user_actions(user_id:, **options)
96+
response = Client.new.query_user_actions(user_id: user_id, **options)
97+
98+
handle_response(response)
99+
end
100+
89101
def update_action(user_id:, action:, idempotency_key:, attributes:)
90102
response = Client.new.update_action(user_id: user_id, action: action, idempotency_key: idempotency_key, attributes: attributes)
91103

92104
handle_response(response)
93105
end
94106

107+
def challenge(verification_method:, action:, **options)
108+
response = Client.new.challenge(verification_method: verification_method, action: action, **options)
109+
110+
handle_response(response)
111+
end
112+
113+
def verify(challenge_id:, verification_code:)
114+
response = Client.new.verify(challenge_id: challenge_id, verification_code: verification_code)
115+
116+
handle_response(response)
117+
end
118+
119+
def claim_challenge(challenge_id:, user_id:, **options)
120+
response = Client.new.claim_challenge(challenge_id: challenge_id, user_id: user_id, **options)
121+
122+
handle_response(response)
123+
end
124+
125+
def get_challenge(**options)
126+
response = Client.new.get_challenge(**options)
127+
128+
handle_response(response)
129+
end
130+
131+
def create_session(client_id:, token:, action: nil)
132+
response = Client.new.create_session(client_id: client_id, token: token, action: action)
133+
134+
handle_response(response)
135+
end
136+
137+
def validate_session(access_token:, client_ids: nil)
138+
response = Client.new.validate_session(access_token: access_token, client_ids: client_ids)
139+
140+
handle_response(response)
141+
end
142+
143+
def refresh_session(refresh_token:)
144+
response = Client.new.refresh_session(refresh_token: refresh_token)
145+
146+
handle_response(response)
147+
end
148+
149+
def revoke_session(access_token:)
150+
response = Client.new.revoke_session(access_token: access_token)
151+
152+
handle_response(response)
153+
end
154+
155+
def revoke_user_sessions(user_id:)
156+
response = Client.new.revoke_user_sessions(user_id: user_id)
157+
158+
handle_response(response)
159+
end
160+
95161
private
96162

97163
def handle_response(response)

lib/authsignal/client.rb

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ def delete_user(user_id:)
5252
make_request(:delete, "users/#{url_encode(user_id)}")
5353
end
5454

55+
def query_users(
56+
username: nil,
57+
email: nil,
58+
phone_number: nil,
59+
token: nil,
60+
limit: nil,
61+
last_evaluated_user_id: nil
62+
)
63+
params = {
64+
username: username,
65+
email: email,
66+
phoneNumber: phone_number,
67+
token: token,
68+
limit: limit&.to_s,
69+
lastEvaluatedUserId: last_evaluated_user_id
70+
}.compact
71+
72+
path = params.empty? ? 'users' : "users?#{URI.encode_www_form(params)}"
73+
make_request(:get, path)
74+
end
75+
5576
def get_authenticators(user_id:)
5677
make_request(:get, "users/#{url_encode(user_id)}/authenticators")
5778
end
@@ -81,10 +102,131 @@ def get_action(user_id:, action:, idempotency_key:)
81102
make_request(:get, "users/#{url_encode(user_id)}/actions/#{action}/#{url_encode(idempotency_key)}")
82103
end
83104

105+
def query_user_actions(
106+
user_id:,
107+
from_date: nil,
108+
action_codes: [],
109+
state: nil
110+
)
111+
params = {
112+
fromDate: from_date,
113+
codes: action_codes.empty? ? nil : action_codes.join(','),
114+
state: state
115+
}.compact
116+
117+
base_path = "users/#{url_encode(user_id)}/actions"
118+
path = params.empty? ? base_path : "#{base_path}?#{URI.encode_www_form(params)}"
119+
make_request(:get, path)
120+
end
121+
84122
def update_action(user_id:, action:, idempotency_key:, attributes:)
85123
make_request(:patch, "users/#{url_encode(user_id)}/actions/#{action}/#{url_encode(idempotency_key)}", body: attributes)
86124
end
87125

126+
def challenge(
127+
verification_method:,
128+
action:,
129+
idempotency_key: nil,
130+
user_id: nil,
131+
email: nil,
132+
phone_number: nil,
133+
sms_channel: nil,
134+
locale: nil,
135+
device_id: nil,
136+
ip_address: nil,
137+
user_agent: nil,
138+
custom: nil,
139+
scope: nil
140+
)
141+
body = {
142+
verification_method: verification_method,
143+
action: action,
144+
idempotency_key: idempotency_key,
145+
user_id: user_id,
146+
email: email,
147+
phone_number: phone_number,
148+
sms_channel: sms_channel,
149+
locale: locale,
150+
device_id: device_id,
151+
ip_address: ip_address,
152+
user_agent: user_agent,
153+
custom: custom,
154+
scope: scope
155+
}
156+
make_request(:post, 'challenge', body: body)
157+
end
158+
159+
def verify(challenge_id:, verification_code:)
160+
body = {
161+
challenge_id: challenge_id,
162+
verification_code: verification_code
163+
}
164+
make_request(:post, 'verify', body: body)
165+
end
166+
167+
def claim_challenge(
168+
challenge_id:,
169+
user_id:,
170+
skip_verification_check: nil
171+
)
172+
body = {
173+
challenge_id: challenge_id,
174+
user_id: user_id,
175+
skip_verification_check: skip_verification_check
176+
}
177+
make_request(:post, 'claim', body: body)
178+
end
179+
180+
def get_challenge(
181+
challenge_id: nil,
182+
user_id: nil,
183+
action: nil,
184+
verification_method: nil
185+
)
186+
params = {}
187+
params[:challengeId] = challenge_id if challenge_id
188+
params[:userId] = user_id if user_id
189+
params[:action] = action if action
190+
params[:verificationMethod] = verification_method if verification_method
191+
192+
query_string = URI.encode_www_form(params) unless params.empty?
193+
path = query_string ? "challenges?#{query_string}" : 'challenges'
194+
195+
make_request(:get, path)
196+
end
197+
198+
def create_session(client_id:, token:, action: nil)
199+
body = {
200+
client_id: client_id,
201+
token: token,
202+
action: action
203+
}.compact
204+
make_request(:post, 'sessions', body: body)
205+
end
206+
207+
def validate_session(access_token:, client_ids: nil)
208+
body = {
209+
access_token: access_token,
210+
client_ids: client_ids
211+
}.compact
212+
make_request(:post, 'sessions/validate', body: body)
213+
end
214+
215+
def refresh_session(refresh_token:)
216+
body = { refresh_token: refresh_token }
217+
make_request(:post, 'sessions/refresh', body: body)
218+
end
219+
220+
def revoke_session(access_token:)
221+
body = { access_token: access_token }
222+
make_request(:post, 'sessions/revoke', body: body)
223+
end
224+
225+
def revoke_user_sessions(user_id:)
226+
body = { user_id: user_id }
227+
make_request(:post, 'sessions/user/revoke', body: body)
228+
end
229+
88230
##
89231
# TODO: delete identify?
90232
def identify(user_id, user_payload)

lib/authsignal/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Authsignal
4-
VERSION = '5.2.2'
4+
VERSION = '5.3.0'
55
end

0 commit comments

Comments
 (0)