File: client_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (273 lines) | stat: -rw-r--r-- 13,397 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
require "spec"
require "oauth2"
require "http/server"
require "../http/spec_helper"

# TODO: Windows networking in the interpreter requires #12495
{% if flag?(:interpreted) && flag?(:win32) %}
  pending OAuth2::Client
  {% skip_file %}
{% end %}

describe OAuth2::Client do
  describe "authorization uri" do
    it "gets with default endpoint" do
      client = OAuth2::Client.new "localhost", "client_id", "client_secret", redirect_uri: "uri"
      uri = client.get_authorize_uri(scope: "foo bar")
      uri.should eq("https://localhost/oauth2/authorize?client_id=client_id&redirect_uri=uri&response_type=code&scope=foo+bar")
    end

    it "gets with custom endpoint" do
      client = OAuth2::Client.new "localhost", "client_id", "client_secret", redirect_uri: "uri", authorize_uri: "/baz"
      uri = client.get_authorize_uri(scope: "foo bar")
      uri.should eq("https://localhost/baz?client_id=client_id&redirect_uri=uri&response_type=code&scope=foo+bar")
    end

    it "gets with state" do
      client = OAuth2::Client.new "localhost", "client_id", "client_secret", redirect_uri: "uri"
      uri = client.get_authorize_uri(scope: "foo bar", state: "xyz")
      uri.should eq("https://localhost/oauth2/authorize?client_id=client_id&redirect_uri=uri&response_type=code&scope=foo+bar&state=xyz")
    end

    it "gets with block" do
      client = OAuth2::Client.new "localhost", "client_id", "client_secret", redirect_uri: "uri"
      uri = client.get_authorize_uri(scope: "foo bar") do |form|
        form.add "baz", "qux"
      end
      uri.should eq("https://localhost/oauth2/authorize?client_id=client_id&redirect_uri=uri&response_type=code&scope=foo+bar&baz=qux")
    end

    it "gets with absolute uri" do
      client = OAuth2::Client.new "localhost", "client_id", "client_secret",
        redirect_uri: "uri",
        authorize_uri: "https://example2.com:1234/foo?bar=baz"
      uri = client.get_authorize_uri(scope: "foo bar")
      uri.should eq("https://example2.com:1234/foo?client_id=client_id&redirect_uri=uri&response_type=code&scope=foo+bar&bar=baz")
    end
  end

  describe "get_access_token_using_*" do
    describe "using HTTP Basic authentication to pass credentials" do
      it "#get_access_token_using_authorization_code" do
        handler = HTTP::Handler::HandlerProc.new do |context|
          body = context.request.body.not_nil!.gets_to_end
          response = {access_token: "access_token", body: body}
          context.response.print response.to_json
        end

        run_handler(handler) do |http_client|
          client = OAuth2::Client.new "127.0.0.1", "client_id", "client_secret", scheme: "http"
          client.http_client = http_client

          token = client.get_access_token_using_authorization_code(authorization_code: "SDFhw39fwfg23flSfpawbef")
          token.extra.not_nil!["body"].should eq %("redirect_uri=&grant_type=authorization_code&code=SDFhw39fwfg23flSfpawbef")
          token.access_token.should eq "access_token"
        end
      end

      it "configures HTTP::Client" do
        server = HTTP::Server.new do |context|
          body = context.request.body.not_nil!.gets_to_end
          response = {access_token: "access_token", body: body}
          context.response.print response.to_json
        end
        address = server.bind_tcp 0

        run_server(server) do
          client = OAuth2::Client.new "127.0.0.1", "client_id", "client_secret", port: address.port, scheme: "http"
          client.http_client.port.should eq address.port
          client.http_client.host.should eq "127.0.0.1"

          token = client.get_access_token_using_authorization_code(authorization_code: "SDFhw39fwfg23flSfpawbef")
          token.extra.not_nil!["body"].should eq %("redirect_uri=&grant_type=authorization_code&code=SDFhw39fwfg23flSfpawbef")
          token.access_token.should eq "access_token"
        end
      end

      it "#get_access_token_using_resource_owner_credentials" do
        handler = HTTP::Handler::HandlerProc.new do |context|
          body = context.request.body.not_nil!.gets_to_end
          response = {access_token: "access_token", body: body}
          context.response.print response.to_json
        end

        run_handler(handler) do |http_client|
          client = OAuth2::Client.new "127.0.0.1", "client_id", "client_secret", scheme: "http"
          client.http_client = http_client

          token = client.get_access_token_using_resource_owner_credentials(username: "user123", password: "monkey", scope: "read_posts")
          token.extra.not_nil!["body"].should eq %("grant_type=password&username=user123&password=monkey&scope=read_posts")
          token.access_token.should eq "access_token"
        end
      end

      it "#get_access_token_using_client_credentials" do
        handler = HTTP::Handler::HandlerProc.new do |context|
          body = context.request.body.not_nil!.gets_to_end
          response = {access_token: "access_token", body: body}
          context.response.print response.to_json
        end

        run_handler(handler) do |http_client|
          client = OAuth2::Client.new "127.0.0.1", "client_id", "client_secret", scheme: "http"
          client.http_client = http_client

          token = client.get_access_token_using_client_credentials(scope: "read_posts")
          token.extra.not_nil!["body"].should eq %("grant_type=client_credentials&scope=read_posts")
          token.access_token.should eq "access_token"
        end
      end

      it "#get_access_token_using_refresh_token" do
        handler = HTTP::Handler::HandlerProc.new do |context|
          body = context.request.body.not_nil!.gets_to_end
          response = {access_token: "access_token", body: body}
          context.response.print response.to_json
        end

        run_handler(handler) do |http_client|
          client = OAuth2::Client.new "127.0.0.1", "client_id", "client_secret", scheme: "http"
          client.http_client = http_client

          token = client.get_access_token_using_refresh_token(scope: "read_posts", refresh_token: "some_refresh_token")
          token.extra.not_nil!["body"].should eq %("grant_type=refresh_token&refresh_token=some_refresh_token&scope=read_posts")
          token.access_token.should eq "access_token"
        end
      end

      it "#make_token_request" do
        handler = HTTP::Handler::HandlerProc.new do |context|
          body = context.request.body.not_nil!.gets_to_end
          dpop = context.request.headers.get?("DPoP")
          response = {access_token: "access_token", body: body, dpop: dpop}
          context.response.print response.to_json
        end

        run_handler(handler) do |http_client|
          client = OAuth2::Client.new "127.0.0.1", "client_id", "client_secret", scheme: "http"
          client.http_client = http_client

          token_response = client.make_token_request do |form, headers|
            form.add("redirect_uri", client.redirect_uri)
            form.add("grant_type", "authorization_code")
            form.add("code", "some_authorization_code")
            form.add("code_verifier", "a_code_verifier")
            form.add("nonce", "a_nonce")
            headers.add("DPoP", "a_DPoP_jwt_token")
          end
          token_response.status_code.should eq(200)
          token = OAuth2::AccessToken.from_json(token_response.body)
          token.extra.not_nil!["body"].should eq %("redirect_uri=&grant_type=authorization_code&code=some_authorization_code&code_verifier=a_code_verifier&nonce=a_nonce")
          token.extra.not_nil!["dpop"].should eq %(["a_DPoP_jwt_token"])
          token.access_token.should eq "access_token"
        end
      end
    end
    describe "using Request Body to pass credentials" do
      it "#get_access_token_using_authorization_code" do
        handler = HTTP::Handler::HandlerProc.new do |context|
          body = context.request.body.not_nil!.gets_to_end
          response = {access_token: "access_token", body: body}
          context.response.print response.to_json
        end

        run_handler(handler) do |http_client|
          client = OAuth2::Client.new "127.0.0.1", "client_id", "client_secret", scheme: "http", auth_scheme: OAuth2::AuthScheme::RequestBody
          client.http_client = http_client

          token = client.get_access_token_using_authorization_code(authorization_code: "SDFhw39fwfg23flSfpawbef")
          token.extra.not_nil!["body"].should eq %("client_id=client_id&client_secret=client_secret&redirect_uri=&grant_type=authorization_code&code=SDFhw39fwfg23flSfpawbef")
          token.access_token.should eq "access_token"
        end
      end

      it "#get_access_token_using_resource_owner_credentials" do
        handler = HTTP::Handler::HandlerProc.new do |context|
          body = context.request.body.not_nil!.gets_to_end
          response = {access_token: "access_token", body: body}
          context.response.print response.to_json
        end

        run_handler(handler) do |http_client|
          client = OAuth2::Client.new "127.0.0.1", "client_id", "client_secret", scheme: "http", auth_scheme: OAuth2::AuthScheme::RequestBody
          client.http_client = http_client

          token = client.get_access_token_using_resource_owner_credentials(username: "user123", password: "monkey", scope: "read_posts")
          token.extra.not_nil!["body"].should eq %("client_id=client_id&client_secret=client_secret&grant_type=password&username=user123&password=monkey&scope=read_posts")
          token.access_token.should eq "access_token"
        end
      end

      it "#get_access_token_using_client_credentials" do
        handler = HTTP::Handler::HandlerProc.new do |context|
          body = context.request.body.not_nil!.gets_to_end
          response = {access_token: "access_token", body: body}
          context.response.print response.to_json
        end

        run_handler(handler) do |http_client|
          client = OAuth2::Client.new "127.0.0.1", "client_id", "client_secret", scheme: "http", auth_scheme: OAuth2::AuthScheme::RequestBody
          client.http_client = http_client

          token = client.get_access_token_using_client_credentials(scope: "read_posts")
          token.extra.not_nil!["body"].should eq %("client_id=client_id&client_secret=client_secret&grant_type=client_credentials&scope=read_posts")
          token.access_token.should eq "access_token"
        end
      end

      it "#get_access_token_using_refresh_token" do
        handler = HTTP::Handler::HandlerProc.new do |context|
          body = context.request.body.not_nil!.gets_to_end
          response = {access_token: "access_token", body: body}
          context.response.print response.to_json
        end

        run_handler(handler) do |http_client|
          client = OAuth2::Client.new "127.0.0.1", "client_id", "client_secret", scheme: "http", auth_scheme: OAuth2::AuthScheme::RequestBody
          client.http_client = http_client

          token = client.get_access_token_using_refresh_token(scope: "read_posts", refresh_token: "some_refresh_token")
          token.extra.not_nil!["body"].should eq %("client_id=client_id&client_secret=client_secret&grant_type=refresh_token&refresh_token=some_refresh_token&scope=read_posts")
          token.access_token.should eq "access_token"
        end
      end

      it "#make_token_request" do
        handler = HTTP::Handler::HandlerProc.new do |context|
          body = context.request.body.not_nil!.gets_to_end
          dpop = context.request.headers.get?("DPoP")
          response = {access_token: "access_token", body: body, dpop: dpop}
          context.response.print response.to_json
        end

        run_handler(handler) do |http_client|
          client = OAuth2::Client.new "127.0.0.1", "client_id", "client_secret", scheme: "http", auth_scheme: OAuth2::AuthScheme::RequestBody
          client.http_client = http_client

          token_response = client.make_token_request do |form, headers|
            form.add("grant_type", "refresh_token")
            form.add("refresh_token", "some_refresh_token")
            form.add("scope", "read_posts")
            form.add("nonce", "a_nonce")
            headers.add("DPoP", "a_DPoP_jwt_token")
          end
          token_response.status_code.should eq(200)
          token = OAuth2::AccessToken.from_json(token_response.body)
          token.extra.not_nil!["body"].should eq %("client_id=client_id&client_secret=client_secret&grant_type=refresh_token&refresh_token=some_refresh_token&scope=read_posts&nonce=a_nonce")
          token.extra.not_nil!["dpop"].should eq %(["a_DPoP_jwt_token"])
          token.access_token.should eq "access_token"
        end
      end
    end
  end

  typeof(begin
    client = OAuth2::Client.new "localhost", "client_id", "client_secret", redirect_uri: "uri", authorize_uri: "/baz"
    client.get_access_token_using_authorization_code("some_code")
    client.get_access_token_using_refresh_token("some_refresh_token")
    client.get_access_token_using_refresh_token("some_refresh_token", scope: "some scope")
    client.get_access_token_using_client_credentials(scope: "some scope")
    client.get_access_token_using_resource_owner_credentials(username: "user123", password: "monkey")
    client.get_access_token_using_resource_owner_credentials(username: "user123", password: "monkey", scope: "foo")
  end)
end