Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 6542153

Browse files
first spike for introspection client
1 parent 5b4fbd8 commit 6542153

4 files changed

Lines changed: 197 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2014, 2015 Dominick Baier, Brock Allen
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Net;
20+
using System.Net.Http;
21+
using System.Threading.Tasks;
22+
23+
namespace IdentityModel.Client
24+
{
25+
public class IntrospectionClient
26+
{
27+
private readonly HttpClient _client;
28+
29+
public IntrospectionClient(string endpoint, string clientId = "", string clientSecret = "", HttpMessageHandler innerHttpMessageHandler = null)
30+
{
31+
if (string.IsNullOrWhiteSpace(endpoint)) throw new ArgumentNullException("endpoint");
32+
if (innerHttpMessageHandler == null) innerHttpMessageHandler = new HttpClientHandler();
33+
34+
_client = new HttpClient(innerHttpMessageHandler)
35+
{
36+
BaseAddress = new Uri(endpoint)
37+
};
38+
39+
if (!string.IsNullOrWhiteSpace(clientId))
40+
{
41+
_client.SetBasicAuthentication(clientId, clientSecret);
42+
}
43+
}
44+
45+
public TimeSpan Timeout
46+
{
47+
set
48+
{
49+
_client.Timeout = value;
50+
}
51+
}
52+
53+
public async Task<IntrospectionResponse> SendAsync(IntrospectionRequest request)
54+
{
55+
var form = new Dictionary<string, string>();
56+
form.Add("token", request.Token);
57+
58+
if (string.IsNullOrWhiteSpace(request.ClientId))
59+
{
60+
form.Add("client_id", request.ClientId);
61+
form.Add("client_secret", request.ClientSecret);
62+
}
63+
64+
try
65+
{
66+
var response = await _client.PostAsync("", new FormUrlEncodedContent(form));
67+
if (response.StatusCode != HttpStatusCode.OK)
68+
{
69+
return new IntrospectionResponse
70+
{
71+
IsError = true,
72+
Error = response.ReasonPhrase
73+
};
74+
}
75+
76+
return new IntrospectionResponse(await response.Content.ReadAsStringAsync());
77+
}
78+
catch (Exception ex)
79+
{
80+
return new IntrospectionResponse
81+
{
82+
IsError = true,
83+
Error = ex.Message
84+
};
85+
}
86+
}
87+
}
88+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2014, 2015 Dominick Baier, Brock Allen
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
namespace IdentityModel.Client
18+
{
19+
public class IntrospectionRequest
20+
{
21+
public string Token { get; set; }
22+
23+
public string ClientId { get; set; }
24+
public string ClientSecret { get; set; }
25+
26+
public IntrospectionRequest()
27+
{
28+
Token = "";
29+
ClientId = "";
30+
ClientSecret = "";
31+
}
32+
}
33+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2014, 2015 Dominick Baier, Brock Allen
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using Newtonsoft.Json.Linq;
18+
using System;
19+
using System.Collections.Generic;
20+
21+
namespace IdentityModel.Client
22+
{
23+
public class IntrospectionResponse
24+
{
25+
public string Raw { get; set; }
26+
27+
public bool IsError { get; set; }
28+
public string Error { get; set; }
29+
30+
public bool IsActive { get; set; }
31+
public IEnumerable<Tuple<string, string>> Claims { get; set; }
32+
33+
public IntrospectionResponse()
34+
{ }
35+
36+
public IntrospectionResponse(string raw)
37+
{
38+
Raw = raw;
39+
40+
try
41+
{
42+
var json = JObject.Parse(raw);
43+
IsActive = bool.Parse(json["active"].ToString());
44+
45+
var claims = new List<Tuple<string, string>>();
46+
47+
foreach (var x in json)
48+
{
49+
var array = x.Value as JArray;
50+
51+
if (array != null)
52+
{
53+
foreach (var item in array)
54+
{
55+
claims.Add(Tuple.Create(x.Key, item.ToString()));
56+
}
57+
}
58+
else
59+
{
60+
claims.Add(Tuple.Create(x.Key, x.Value.ToString()));
61+
}
62+
}
63+
64+
Claims = claims;
65+
}
66+
catch (Exception ex)
67+
{
68+
IsError = true;
69+
Error = ex.Message;
70+
}
71+
}
72+
}
73+
}

source/IdentityModel.Shared/IdentityModel.Shared.projitems

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<Compile Include="$(MSBuildThisFileDirectory)Client\AuthorizeResponse.cs" />
1717
<Compile Include="$(MSBuildThisFileDirectory)Client\BasicAuthenticationHeaderValue.cs" />
1818
<Compile Include="$(MSBuildThisFileDirectory)Client\HttpClientExtensions.cs" />
19+
<Compile Include="$(MSBuildThisFileDirectory)Client\IntrospectionClient.cs" />
20+
<Compile Include="$(MSBuildThisFileDirectory)Client\IntrospectionRequest.cs" />
21+
<Compile Include="$(MSBuildThisFileDirectory)Client\IntrospectionResponse.cs" />
1922
<Compile Include="$(MSBuildThisFileDirectory)Client\TokenClient.cs" />
2023
<Compile Include="$(MSBuildThisFileDirectory)Client\TokenClientExtensions.cs" />
2124
<Compile Include="$(MSBuildThisFileDirectory)Client\OAuth2Constants.cs" />

0 commit comments

Comments
 (0)