/
WRAPv09IdentityProvider.cs
88 lines (74 loc) · 2.82 KB
/
WRAPv09IdentityProvider.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
using System;
using System.Net;
using System.Windows;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
namespace KidoZen.authentication
{
public class WRAPv09IdentityProvider : IIdentityProvider
{
private string wrapName;
private string wrapPassword;
public WRAPv09IdentityProvider()
{
}
public void Configure(IdentityProviderConfig configuration)
{
}
public void Initialize(String username, String password)
{
this.wrapName = username;
this.wrapPassword = password;
}
public void BeforeRequestToken(Object[] args)
{
}
public void AfterRequestToken(Object[] args)
{
}
public async Task<RequestTokenResult> RequestToken(Uri identityProviderUrl, String scope)
{
try
{
var sb = new StringBuilder(512);
sb.Append("wrap_name=");
sb.Append(WebUtility.UrlEncode(wrapName));
sb.Append("&wrap_password=");
sb.Append(WebUtility.UrlEncode(wrapPassword));
sb.Append("&wrap_scope=");
sb.Append(WebUtility.UrlEncode(scope));
var headers = new Dictionary<string, string>();
headers["Accept"] = "text/xml";
var res = await identityProviderUrl.ExecuteAsync<object>(null, sb.ToString(), "application/x-www-form-urlencoded", "POST", headers: headers, cache:true);
var content = res.Data.ToString();
var contentType = default(string);
res.Headers.TryGetValue("Content-Type", out contentType);
if (contentType==null && !contentType.ToLower().Contains("xml"))
throw new Exception(content);
var doc = XDocument.Parse(content);
var fault = doc
.Descendants(XName.Get("Fault", "http://www.w3.org/2003/05/soap-envelope"))
.FirstOrDefault();
if (fault != null)
{
var text = fault
.Descendants(XName.Get("Text", "http://www.w3.org/2003/05/soap-envelope"))
.FirstOrDefault();
throw new Exception( text != null ? text.Value : fault.ToString() );
}
var indexBegin = content.IndexOf("<Assertion ");
var indexEnd = content.IndexOf("</Assertion>") + 12;
var token = content.Substring(indexBegin, indexEnd - indexBegin);
return new RequestTokenResult { Token = token };
}
catch (Exception )
{
throw;
}
}
}
}