Skip to content

Commit

Permalink
added OAuthClientException
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikos Anestos committed Oct 5, 2017
1 parent 0b45523 commit c0b08dc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ public class CallbackController : Controller

public ActionResult Index()
{
string responseString = _client.GetToken(Request.QueryString["code"]);

if (!String.IsNullOrEmpty(responseString))
try
{
try
{
SaveDataToSession(responseString);
}
catch (JwtValidationException e)
{
Session["error"] = e.Message;
}
string responseString = _client.GetToken(Request.QueryString["code"]);
SaveDataToSession(responseString);
}
catch (JwtValidationException e)
{
Session["error"] = e.Message;
}
catch (OAuthClientException e)
{
Session["error"] = e.Message;
}

return Redirect("/");
Expand Down Expand Up @@ -109,7 +109,8 @@ public bool IsJwtValid(String jwt)
}
}

if (!JObject.Parse(decodedPayload).GetValue("iss").ToString().Equals(issuer)){
if (!JObject.Parse(decodedPayload).GetValue("iss").ToString().Equals(issuer))
{
throw new JwtValidationException("Issuer not validated");
}

Expand Down
26 changes: 14 additions & 12 deletions example-dotnet-openid-connect-client/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,33 @@ public ActionResult Index()

public ActionResult Refresh()
{
String responseString = _client.Refresh(Session["refresh_token"].ToString());
if (String.IsNullOrEmpty(responseString))
try
{
Session["error"] = "Could not refresh Access Token";
}
else
{
JObject jsonObj = JObject.Parse(responseString);
Session["access_token"] = jsonObj.GetValue("access_token");
Session["refresh_token"] = jsonObj.GetValue("refresh_token");
String responseString = _client.Refresh(Session["refresh_token"].ToString());
JObject jsonObj = JObject.Parse(responseString);
Session["access_token"] = jsonObj.GetValue("access_token");
Session["refresh_token"] = jsonObj.GetValue("refresh_token");
}
catch (OAuthClientException e)
{
Session["error"] = e.Message;
}

return Redirect("/");
}

public ActionResult Revoke()
{
if (_client.Revoke(Session["refresh_token"].ToString()))
try
{
_client.Revoke(Session["refresh_token"].ToString());
Session["refresh_token"] = null;
}
else
catch (OAuthClientException e)
{
Session["error"] = "Could not revoke Access Token";
Session["error"] = e.Message;
}


return Redirect("/");
}
Expand Down
10 changes: 5 additions & 5 deletions example-dotnet-openid-connect-client/Helpers/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private Client()
scope = _config.GetScope();
}

public bool Revoke(String refresh_token)
public void Revoke(String refresh_token)
{
var values = new Dictionary<string, string>
{
Expand All @@ -66,10 +66,10 @@ public bool Revoke(String refresh_token)
var responseContent = response.Content;
string responseString = responseContent.ReadAsStringAsync().Result;

return true;
return;
}

return false;
throw new OAuthClientException("Could not revoke the refresh token");
}

public String Refresh(String refresh_token)
Expand All @@ -93,7 +93,7 @@ public String Refresh(String refresh_token)
return responseContent.ReadAsStringAsync().Result;
}

return null;
throw new OAuthClientException("Could not refresh the tokens");
}

public String GetAuthnReqUrl()
Expand Down Expand Up @@ -126,7 +126,7 @@ public String GetToken(String code)
return responseContent.ReadAsStringAsync().Result;
}

return null;
throw new OAuthClientException("Token request failed with status code: " + response.StatusCode);
}

public static Client Instance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2016 Curity AB.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;

public class OAuthClientException : Exception
{
public OAuthClientException()
{
}

public OAuthClientException(string message)
: base(message)
{
}

public OAuthClientException(string message, Exception inner)
: base(message, inner)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<Compile Include="App_Start\AppConfig.cs" />
<Compile Include="Helpers\Client.cs" />
<Compile Include="Helpers\JwtValidationException.cs" />
<Compile Include="Helpers\OAuthClientException.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Web.config" />
Expand Down

0 comments on commit c0b08dc

Please sign in to comment.