forked from DuendeArchive/IdentityModel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushedAuthorizationTests.cs
219 lines (175 loc) · 7.96 KB
/
PushedAuthorizationTests.cs
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
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using FluentAssertions;
using IdentityModel.Client;
using Microsoft.AspNetCore.WebUtilities;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace IdentityModel.UnitTests
{
public class PushedAuthorizationTests
{
private const string Endpoint = "http://server/par";
private PushedAuthorizationRequest Request = new PushedAuthorizationRequest
{
ClientId = "client",
ResponseType = "code",
Address = Endpoint
};
[Fact]
public async Task Http_request_should_have_correct_format()
{
var handler = new NetworkHandler(HttpStatusCode.NotFound, "not found");
var client = new HttpClient(handler);
var request = new PushedAuthorizationRequest
{
ClientId = "client",
ResponseType = "code",
Address = Endpoint,
RedirectUri = "https://example.com/signin-oidc",
Scope = "openid profile",
Nonce = "1234",
State = "5678"
};
request.Headers.Add("custom", "custom");
request.Properties.Add("custom", "custom");
var response = await client.PushAuthorizationAsync(request);
var httpRequest = handler.Request;
httpRequest.Method.Should().Be(HttpMethod.Post);
httpRequest.RequestUri.Should().Be(new Uri(Endpoint));
httpRequest.Content.Should().NotBeNull();
var headers = httpRequest.Headers;
headers.Count().Should().Be(3);
headers.Should().Contain(h => h.Key == "custom" && h.Value.First() == "custom");
var properties = httpRequest.Properties;
properties.Count.Should().Be(1);
var prop = properties.First();
prop.Key.Should().Be("custom");
((string)prop.Value).Should().Be("custom");
}
[Fact]
public async Task Request_with_request_object_should_succeed()
{
var document = File.ReadAllText(FileName.Create("success_par_response.json"));
var handler = new NetworkHandler(document, HttpStatusCode.OK);
var client = new HttpClient(handler);
var request = new PushedAuthorizationRequest
{
ClientId = "client",
Request = "request object value",
Address = Endpoint,
};
await client.PushAuthorizationAsync(request);
var fields = QueryHelpers.ParseQuery(handler.Body);
fields.Count.Should().Be(2);
fields["client_id"].First().Should().Be("client");
fields["request"].First().Should().Be("request object value");
}
[Fact]
public async Task Success_protocol_response_should_be_handled_correctly()
{
var document = File.ReadAllText(FileName.Create("success_par_response.json"));
var handler = new NetworkHandler(document, HttpStatusCode.OK);
var client = new HttpClient(handler)
{
BaseAddress = new Uri(Endpoint)
};
var response = await client.PushAuthorizationAsync(Request);
response.IsError.Should().BeFalse();
response.ErrorType.Should().Be(ResponseErrorType.None);
response.HttpStatusCode.Should().Be(HttpStatusCode.OK);
response.RequestUri.Should().Be("urn:ietf:params:oauth:request_uri:123456");
response.ExpiresIn.Should().Be(600);
}
[Fact]
public async Task Malformed_response_document_should_be_handled_correctly()
{
var document = "invalid";
var handler = new NetworkHandler(document, HttpStatusCode.OK);
var client = new HttpClient(handler);
var response = await client.PushAuthorizationAsync(Request);
response.IsError.Should().BeTrue();
response.ErrorType.Should().Be(ResponseErrorType.Exception);
response.Raw.Should().Be("invalid");
response.Exception.Should().NotBeNull();
}
[Fact]
public async Task Exception_should_be_handled_correctly()
{
var handler = new NetworkHandler(new Exception("exception"));
var client = new HttpClient(handler);
var response = await client.PushAuthorizationAsync(Request);
response.IsError.Should().BeTrue();
response.ErrorType.Should().Be(ResponseErrorType.Exception);
response.Error.Should().Be("exception");
response.Exception.Should().NotBeNull();
}
[Fact]
public async Task Http_error_should_be_handled_correctly()
{
var handler = new NetworkHandler(HttpStatusCode.NotFound, "not found");
var client = new HttpClient(handler);
var response = await client.PushAuthorizationAsync(Request);
response.IsError.Should().BeTrue();
response.Error.Should().Be("not found");
response.ErrorType.Should().Be(ResponseErrorType.Http);
response.HttpStatusCode.Should().Be(HttpStatusCode.NotFound);
}
[Fact]
public async Task Additional_request_parameters_should_be_handled_correctly()
{
var document = File.ReadAllText(FileName.Create("success_par_response.json"));
var handler = new NetworkHandler(document, HttpStatusCode.OK);
var client = new HttpClient(handler);
var response = await client.PushAuthorizationAsync(new PushedAuthorizationRequest
{
ClientId = "client",
ResponseType = "code",
Address = Endpoint,
AcrValues = "idp:example",
Scope = "scope1 scope2",
Parameters =
{
{ "foo", "bar" }
}
});
// check request
var fields = QueryHelpers.ParseQuery(handler.Body);
fields.Count.Should().Be(5);
fields["client_id"].First().Should().Be("client");
fields["response_type"].First().Should().Be("code");
fields["acr_values"].First().Should().Be("idp:example");
fields["scope"].First().Should().Be("scope1 scope2");
fields["foo"].First().Should().Be("bar");
// check response
response.IsError.Should().BeFalse();
response.ErrorType.Should().Be(ResponseErrorType.None);
response.HttpStatusCode.Should().Be(HttpStatusCode.OK);
}
[Fact]
public async Task Pushed_authorization_without_response_type_should_fail()
{
var document = File.ReadAllText(FileName.Create("success_par_response.json"));
var handler = new NetworkHandler(document, HttpStatusCode.OK);
var client = new HttpClient(handler);
Request.ResponseType = null;
Func<Task> act = async () => await client.PushAuthorizationAsync(Request);
(await act.Should().ThrowAsync<ArgumentException>()).WithParameterName("response_type");
}
[Fact]
public async Task Pushed_authorization_with_request_uri_should_fail()
{
var document = File.ReadAllText(FileName.Create("success_par_response.json"));
var handler = new NetworkHandler(document, HttpStatusCode.OK);
var client = new HttpClient(handler);
Request.Parameters.Add(OidcConstants.AuthorizeRequest.RequestUri, "not allowed");
Func<Task> act = async () => await client.PushAuthorizationAsync(Request);
(await act.Should().ThrowAsync<ArgumentException>()).WithParameterName("request_uri");
}
}
}