Skip to content

Commit a972691

Browse files
committed
Makig state mandatory for OIDC URI
1 parent 1589ff3 commit a972691

File tree

4 files changed

+66
-41
lines changed

4 files changed

+66
-41
lines changed

Diff for: lib/vonage/network_authentication/client_authentication.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ def token(oidc_auth_code:, redirect_uri:, **params)
2323
).access_token
2424
end
2525

26-
def generate_oidc_uri(purpose:, api_scope:, login_hint:, redirect_uri:, state: nil)
26+
def generate_oidc_uri(purpose:, api_scope:, login_hint:, redirect_uri:, state:)
2727
scope = "openid%20dpv:#{purpose}%23#{api_scope}"
2828
uri = "https://oidc.idp.vonage.com/oauth2/auth?" +
2929
"client_id=#{@config.application_id}" +
3030
"&response_type=code" +
3131
"&scope=#{scope}" +
3232
"&login_hint=#{login_hint}" +
33-
"&redirect_uri=#{redirect_uri}"
33+
"&redirect_uri=#{redirect_uri}" +
34+
"&state=#{state}"
3435

35-
uri += "&state=#{state}" if state
3636
uri
3737
end
3838
end

Diff for: lib/vonage/network_number_verification.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,21 @@ def verify(phone_number:, auth_data:, hashed: false)
7474
# @param [required, String] :redirect_uri The URI that will receive the callback containing the OIDC auth code.
7575
#
7676
# @param [required, String] :state A string that you can use for tracking.
77-
# This field is optional, but it is recommended to set a unique identifier for each access token you generate.
77+
# Used to set a unique identifier for each access token you generate.
7878
#
7979
# @return [String]
8080
#
8181
# @see https://developer.vonage.com/en/getting-started-network/authentication#1-make-an-oidc-request
82-
sig { params(phone_number: String, redirect_uri: String, state: T.nilable(String)).returns(String) }
83-
def generate_oidc_uri(phone_number:, redirect_uri:, state: nil)
82+
sig { params(phone_number: String, redirect_uri: String, state: String).returns(String) }
83+
def generate_oidc_uri(phone_number:, redirect_uri:, state:)
8484
params = {
8585
purpose: 'FraudPreventionAndDetection',
8686
api_scope: 'number-verification-verify-read',
8787
login_hint: phone_number,
88-
redirect_uri: redirect_uri
88+
redirect_uri: redirect_uri,
89+
state: state
8990
}
9091

91-
params[:state] = state if state
92-
9392
Vonage::NetworkAuthentication::ClientAuthentication.new(@config).generate_oidc_uri(**params)
9493
end
9594
end

Diff for: test/vonage/network_authentication/client_authentication_test.rb

+43-15
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,6 @@ def test_token_method_without_redirect_uri
5151
end
5252

5353
def test_generate_oidc_uri_method
54-
uri = client_authentication.generate_oidc_uri(
55-
purpose: example_purpose,
56-
api_scope: example_api_scope,
57-
login_hint: example_login_hint,
58-
redirect_uri: example_redirect_uri
59-
)
60-
61-
assert_equal "https://oidc.idp.vonage.com/oauth2/auth?client_id=#{application_id}&response_type=code&scope=openid%20dpv:#{example_purpose}%23#{example_api_scope}&login_hint=#{example_login_hint}&redirect_uri=#{example_redirect_uri}", uri
62-
end
63-
64-
def test_generate_oidc_uri_method_with_optional_params
6554
uri = client_authentication.generate_oidc_uri(
6655
purpose: example_purpose,
6756
api_scope: example_api_scope,
@@ -74,18 +63,57 @@ def test_generate_oidc_uri_method_with_optional_params
7463
end
7564

7665
def test_generate_oidc_uri_method_without_purpose
77-
assert_raises(ArgumentError) { client_authentication.generate_oidc_uri(api_scope: example_api_scope, login_hint: example_login_hint, redirect_uri: example_redirect_uri) }
66+
assert_raises(ArgumentError) do
67+
client_authentication.generate_oidc_uri(
68+
api_scope: example_api_scope,
69+
login_hint: example_login_hint,
70+
redirect_uri: example_redirect_uri,
71+
state: '12345'
72+
)
73+
end
7874
end
7975

8076
def test_generate_oidc_uri_method_without_api_scope
81-
assert_raises(ArgumentError) { client_authentication.generate_oidc_uri(purpose: example_purpose, login_hint: example_login_hint, redirect_uri: example_redirect_uri) }
77+
assert_raises(ArgumentError) do
78+
client_authentication.generate_oidc_uri(
79+
purpose: example_purpose,
80+
login_hint: example_login_hint,
81+
redirect_uri: example_redirect_uri,
82+
state: '12345'
83+
)
84+
end
8285
end
8386

8487
def test_generate_oidc_uri_method_without_login_hint
85-
assert_raises(ArgumentError) { client_authentication.generate_oidc_uri(purpose: example_purpose, api_scope: example_api_scope, redirect_uri: example_redirect_uri) }
88+
assert_raises(ArgumentError) do
89+
client_authentication.generate_oidc_uri(
90+
purpose: example_purpose,
91+
api_scope: example_api_scope,
92+
redirect_uri: example_redirect_uri,
93+
state: '12345'
94+
)
95+
end
8696
end
8797

8898
def test_generate_oidc_uri_method_without_redirect_uri
89-
assert_raises(ArgumentError) { client_authentication.generate_oidc_uri(purpose: example_purpose, api_scope: example_api_scope, login_hint: example_login_hint) }
99+
assert_raises(ArgumentError) do
100+
client_authentication.generate_oidc_uri(
101+
purpose: example_purpose,
102+
api_scope: example_api_scope,
103+
login_hint: example_login_hint,
104+
state: '12345'
105+
)
106+
end
107+
end
108+
109+
def test_generate_oidc_uri_method_without_state
110+
assert_raises(ArgumentError) do
111+
client_authentication.generate_oidc_uri(
112+
purpose: example_purpose,
113+
api_scope: example_api_scope,
114+
login_hint: example_login_hint,
115+
redirect_uri: example_redirect_uri
116+
)
117+
end
90118
end
91119
end

Diff for: test/vonage/network_number_verification_test.rb

+15-17
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_verify_method_with_hashed_phone_number
7373
assert_kind_of Vonage::Response, response
7474
end
7575

76-
def test_check_method_without_phone_number
76+
def test_verify_method_without_phone_number
7777
assert_raises(ArgumentError) do
7878
network_number_verification.verify(
7979
auth_data: {
@@ -84,7 +84,7 @@ def test_check_method_without_phone_number
8484
end
8585
end
8686

87-
def test_check_method_with_invalid_phone_number
87+
def test_verify_method_with_invalid_phone_number
8888
auth_data = {
8989
oidc_auth_code: example_oidc_auth_code,
9090
redirect_uri: example_redirect_uri
@@ -95,7 +95,7 @@ def test_check_method_with_invalid_phone_number
9595
assert_raises(ArgumentError) { network_number_verification.verify(phone_number: '447904603505', auth_data: auth_data) }
9696
end
9797

98-
def test_check_method_with_invalid_hashed
98+
def test_verify_method_with_invalid_hashed
9999
auth_data = {
100100
oidc_auth_code: example_oidc_auth_code,
101101
redirect_uri: example_redirect_uri
@@ -104,11 +104,11 @@ def test_check_method_with_invalid_hashed
104104
assert_raises(TypeError) { network_number_verification.verify(phone_number: hashed_phone_number, auth_data: auth_data, hashed: 'true') }
105105
end
106106

107-
def test_check_method_without_auth_data
107+
def test_verify_method_without_auth_data
108108
assert_raises(ArgumentError) { network_number_verification.verify(phone_number: phone_number) }
109109
end
110110

111-
def test_check_method_with_invalid_auth_data
111+
def test_verify_method_with_invalid_auth_data
112112
auth_data = [
113113
example_oidc_auth_code,
114114
example_redirect_uri
@@ -117,15 +117,15 @@ def test_check_method_with_invalid_auth_data
117117
assert_raises(TypeError) { network_number_verification.verify(phone_number: phone_number, auth_data: auth_data) }
118118
end
119119

120-
def test_check_method_without_oidc_auth_code
120+
def test_verify_method_without_oidc_auth_code
121121
auth_data = {
122122
redirect_uri: example_redirect_uri
123123
}
124124

125125
assert_raises(ArgumentError) { network_number_verification.verify(phone_number: phone_number, auth_data: auth_data) }
126126
end
127127

128-
def test_check_method_with_invalid_oidc_auth_code
128+
def test_verify_method_with_invalid_oidc_auth_code
129129
auth_data = {
130130
oidc_auth_code: 12345,
131131
redirect_uri: example_redirect_uri
@@ -134,15 +134,15 @@ def test_check_method_with_invalid_oidc_auth_code
134134
assert_raises(ArgumentError) { network_number_verification.verify(phone_number: phone_number, auth_data: auth_data) }
135135
end
136136

137-
def test_check_method_without_redirect_uri
137+
def test_verify_method_without_redirect_uri
138138
auth_data = {
139139
oidc_auth_code: example_oidc_auth_code
140140
}
141141

142142
assert_raises(ArgumentError) { network_number_verification.verify(phone_number: phone_number, auth_data: auth_data) }
143143
end
144144

145-
def test_check_method_with_invalid_redirect_uri
145+
def test_verify_method_with_invalid_redirect_uri
146146
auth_data = {
147147
oidc_auth_code: example_oidc_auth_code,
148148
redirect_uri: 12345
@@ -152,22 +152,20 @@ def test_check_method_with_invalid_redirect_uri
152152
end
153153

154154
def test_generate_client_uri_method
155-
expected_uri = "https://oidc.idp.vonage.com/oauth2/auth?client_id=#{application_id}&response_type=code&scope=openid%20dpv:FraudPreventionAndDetection%23number-verification-verify-read&login_hint=#{phone_number}&redirect_uri=#{example_redirect_uri}"
156-
157-
assert_equal expected_uri, network_number_verification.generate_oidc_uri(phone_number: phone_number, redirect_uri: example_redirect_uri)
158-
end
159-
160-
def test_generate_client_uri_method_with_optional_params
161155
expected_uri = "https://oidc.idp.vonage.com/oauth2/auth?client_id=#{application_id}&response_type=code&scope=openid%20dpv:FraudPreventionAndDetection%23number-verification-verify-read&login_hint=#{phone_number}&redirect_uri=#{example_redirect_uri}&state=12345"
162156

163157
assert_equal expected_uri, network_number_verification.generate_oidc_uri(phone_number: phone_number, redirect_uri: example_redirect_uri, state: '12345')
164158
end
165159

166160
def test_generate_client_uri_method_without_phone_number
167-
assert_raises(ArgumentError) { network_number_verification.generate_oidc_uri(redirect_uri: example_redirect_uri) }
161+
assert_raises(ArgumentError) { network_number_verification.generate_oidc_uri(redirect_uri: example_redirect_uri, state: '12345') }
168162
end
169163

170164
def test_generate_client_uri_method_without_redirect_uri
171-
assert_raises(ArgumentError) { network_number_verification.generate_oidc_uri(phone_number: phone_number) }
165+
assert_raises(ArgumentError) { network_number_verification.generate_oidc_uri(phone_number: phone_number, state: '12345') }
166+
end
167+
168+
def test_generate_client_uri_method_without_state
169+
assert_raises(ArgumentError) { network_number_verification.generate_oidc_uri(phone_number: phone_number, redirect_uri: example_redirect_uri) }
172170
end
173171
end

0 commit comments

Comments
 (0)