adding a configurable hostname when creating the onshape owin middleware

This commit is contained in:
Tathagata Chakraborty
2015-09-15 11:09:57 +05:30
parent 4f56f6387a
commit e73197a619
4 changed files with 29 additions and 13 deletions

View File

@@ -28,5 +28,17 @@ namespace Owin.Security.Providers.Onshape
CallbackPath = new PathString(callbackPath)
});
}
public static IAppBuilder UseOnshapeAuthentication(this IAppBuilder app, string appKey,
string appSecret, string callbackPath, string hostname)
{
return app.UseOnshapeAuthentication(new OnshapeAuthenticationOptions
{
AppKey = appKey,
AppSecret = appSecret,
CallbackPath = new PathString(callbackPath),
Hostname = hostname
});
}
}
}

View File

@@ -20,8 +20,9 @@ namespace Owin.Security.Providers.Onshape
{
private const string StateCookie = "_OnshapeState";
private const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";
private const string TokenEndpoint = "https://partner.dev.onshape.com/oauth/token";
private const string UserInfoEndpoint = "https://partner.dev.onshape.com/api/users/current";
private const string AuthorizationEndpoint = "/oauth/authorize";
private const string TokenEndpoint = "/oauth/token";
private const string UserInfoEndpoint = "/api/users/current";
private readonly ILogger logger;
private readonly HttpClient httpClient;
@@ -77,7 +78,7 @@ namespace Owin.Security.Providers.Onshape
body.Add(new KeyValuePair<string, string>("client_secret", Options.AppSecret));
// Get token
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, TokenEndpoint);
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, "https://" + Options.Hostname + TokenEndpoint);
tokenRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
tokenRequest.Content = new FormUrlEncodedContent(body);
@@ -92,7 +93,7 @@ namespace Owin.Security.Providers.Onshape
string tokenType = (string)response.token_type;
var userRequest = new HttpRequestMessage(HttpMethod.Get, UserInfoEndpoint);
var userRequest = new HttpRequestMessage(HttpMethod.Get, "https://" + Options.Hostname + UserInfoEndpoint);
userRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
userRequest.Headers.Authorization = new AuthenticationHeaderValue(tokenType, accessToken);
HttpResponseMessage graphResponse = await httpClient.SendAsync(userRequest, Request.CallCancelled);
@@ -163,7 +164,7 @@ namespace Owin.Security.Providers.Onshape
GenerateCorrelationId(properties);
string authorizationEndpoint =
"https://partner.dev.onshape.com/oauth/authorize" +
"https://" + Options.Hostname + AuthorizationEndpoint +
"?response_type=code" +
"&client_id=" + Uri.EscapeDataString(Options.AppKey) +
"&redirect_uri=" + Uri.EscapeDataString(redirectUri);

View File

@@ -62,14 +62,9 @@ namespace Owin.Security.Providers.Onshape
public string AppSecret { get; set; }
/// <summary>
/// Endpoint which is used to redirect users to request Onshape access
/// Hostname to use for the endpoints. Override this for connecting to the partner stack.
/// </summary>
public string AuthorizationEndpoint { get; set; }
/// <summary>
/// Endpoint which is used to exchange code for access token
/// </summary>
public string TokenEndpoint { get; set; }
public string Hostname { get; set; }
/// <summary>
/// Gets or sets the <see cref="IOnshapeAuthenticationProvider" /> used in the authentication events
@@ -96,6 +91,7 @@ namespace Owin.Security.Providers.Onshape
Caption = Constants.DefaultAuthenticationType;
AuthenticationMode = AuthenticationMode.Passive;
BackchannelTimeout = TimeSpan.FromSeconds(60);
Hostname = "cad.onshape.com";
}
}
}

View File

@@ -298,7 +298,14 @@ namespace OwinOAuthProvidersDemo
//app.UseOnshapeAuthentication(
// appKey: "",
// appSecret: "");
// appSecret: "",
// callbackPath: "/oauthRedirect");
//
//app.UseOnshapeAuthentication(
// appKey: "",
// appSecret: "",
// callbackPath: "/oauthRedirect",
// hostname: "partner.dev.onshape.com");
}
}
}