-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAppController.cs
174 lines (131 loc) · 6.33 KB
/
AppController.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
using Intuit.Ipp.OAuth2PlatformClient;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.Net;
using System.Net.Http.Headers;
namespace MvcCodeFlowClientManual.Controllers
{
[Authorize]
public class AppController : Controller
{
public static string mod;
public static string expo;
public static string clientid = ConfigurationManager.AppSettings["clientid"];
public static string clientsecret = ConfigurationManager.AppSettings["clientsecret"];
public static string redirectUrl = ConfigurationManager.AppSettings["redirectUrl"];
public static string stateCSRFToken = "";
public static string authorizeUrl = "";
public static string tokenEndpoint = "";
public static string revocationEndpoint = "";
public static string userinfoEndpoint = "";
public static string issuerEndpoint = "";
public static string code = "";
public static string access_token = "";
public static string refresh_token = "";
public static string identity_token = "";
public static IList<JsonWebKey> keys;
public ActionResult Index()
{
return View();
}
public async Task<ActionResult> CallService()
{
var principal = User as ClaimsPrincipal;
string query = "select * from CompanyInfo";
// build the request
string encodedQuery = WebUtility.UrlEncode(query);
if (Session["realmId"] != null)
{
string realmId = Session["realmId"].ToString();
string qboBaseUrl = ConfigurationManager.AppSettings["QBOBaseUrl"];
//add qbobase url and query
string uri = string.Format("{0}/v3/company/{1}/query?query={2}", qboBaseUrl, realmId, encodedQuery);
string result="";
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + principal.FindFirst("access_token").Value);
result = await client.GetStringAsync(uri);
return View("CallService",(object)( "QBO API call success! " + result));
}
catch (Exception ex)
{
return View("CallService",(object)"QBO API call Failed!");
}
}
else
return View("CallService",(object)"QBO API call Failed!");
}
public async Task<ActionResult> RefreshToken()
{
//Refresh Token call
var tokenClient = new TokenClient(AppController.tokenEndpoint, AppController.clientid, AppController.clientsecret);
var principal = User as ClaimsPrincipal;
var refreshToken = principal.FindFirst("refresh_token").Value;
TokenResponse response = await tokenClient.RequestRefreshTokenAsync(refreshToken);
UpdateCookie(response);
return RedirectToAction("Index");
}
public async Task<ActionResult> RevokeAccessToken()
{
var accessToken = (User as ClaimsPrincipal).FindFirst("access_token").Value;
//Revoke Access token call
var revokeClient = new TokenRevocationClient(AppController.revocationEndpoint, clientid, clientsecret);
//Revoke access token
TokenRevocationResponse revokeAccessTokenResponse = await revokeClient.RevokeAccessTokenAsync(accessToken);
if (revokeAccessTokenResponse.HttpStatusCode == HttpStatusCode.OK)
{
Session.Abandon();
Request.GetOwinContext().Authentication.SignOut();
}//delete claims and cookies
return RedirectToAction("Index");
}
public async Task<ActionResult> RevokeRefreshToken()
{
var refreshToken = (User as ClaimsPrincipal).FindFirst("refresh_token").Value;
//Revoke Refresh token call
var revokeClient = new TokenRevocationClient(AppController.revocationEndpoint, clientid, clientsecret);
//Revoke refresh token
TokenRevocationResponse revokeAccessTokenResponse = await revokeClient.RevokeAccessTokenAsync(refreshToken);
if (revokeAccessTokenResponse.HttpStatusCode == HttpStatusCode.OK)
{
Session.Abandon();
Request.GetOwinContext().Authentication.SignOut();
}
//return RedirectToAction("Index");
return RedirectToAction("Index");
}
private void UpdateCookie(TokenResponse response)
{
if (response.IsError)
{
throw new Exception(response.Error);
}
var identity = (User as ClaimsPrincipal).Identities.First();
var result = from c in identity.Claims
where c.Type != "access_token" &&
c.Type != "refresh_token" &&
c.Type != "access_token_expires_at" &&
c.Type != "access_token_expires_at"
select c;
var claims = result.ToList();
claims.Add(new Claim("access_token", response.AccessToken));
claims.Add(new Claim("access_token_expires_at", (DateTime.Now.AddSeconds(response.AccessTokenExpiresIn)).ToString()));
claims.Add(new Claim("refresh_token", response.RefreshToken));
claims.Add(new Claim("refresh_token_expires_at", (DateTime.UtcNow.ToEpochTime() + response.RefreshTokenExpiresIn).ToDateTimeFromEpoch().ToString()));
var newId = new ClaimsIdentity(claims, "Cookies");
Request.GetOwinContext().Authentication.SignIn(newId);
}
}
}