Fixed Authentication

Also added Geni to the list of supported providers in the readme file
This commit is contained in:
papinto
2016-05-15 17:11:31 -07:00
parent 842c250113
commit fa61e28d09
3 changed files with 10 additions and 13 deletions

View File

@@ -17,6 +17,7 @@ Provides a set of extra authentication providers for OWIN ([Project Katana](http
- Fitbit - Fitbit
- Flickr - Flickr
- Foursquare - Foursquare
- Geni
- GitHub - GitHub
- Gitter - Gitter
- Google+ - Google+

View File

@@ -71,28 +71,24 @@ namespace Owin.Security.Providers.Geni
new KeyValuePair<string, string>("code", code), new KeyValuePair<string, string>("code", code),
new KeyValuePair<string, string>("grant_type", "authorization_code"), new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("client_id", Options.AppKey), new KeyValuePair<string, string>("client_id", Options.AppKey),
new KeyValuePair<string, string>("client_secret", Options.AppSecret),
new KeyValuePair<string, string>("redirect_uri", redirectUri) new KeyValuePair<string, string>("redirect_uri", redirectUri)
}; };
// Request the token // Request the token
var requestMessage = new HttpRequestMessage(HttpMethod.Post, Options.Endpoints.TokenEndpoint); var tokenResponse =
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", new Base64TextEncoder().Encode(Encoding.ASCII.GetBytes(Options.AppKey + ":" + Options.AppSecret))); await _httpClient.PostAsync(Options.Endpoints.TokenEndpoint, new FormUrlEncodedContent(body));
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
requestMessage.Content = new FormUrlEncodedContent(body);
var tokenResponse = await _httpClient.SendAsync(requestMessage);
tokenResponse.EnsureSuccessStatusCode(); tokenResponse.EnsureSuccessStatusCode();
var text = await tokenResponse.Content.ReadAsStringAsync(); var text = await tokenResponse.Content.ReadAsStringAsync();
// Deserializes the token response // Deserializes the token response
dynamic response = JsonConvert.DeserializeObject<dynamic>(text); dynamic response = JsonConvert.DeserializeObject<dynamic>(text);
var accessToken = (string)response.access_token; var accessToken = (string)response.access_token;
var refreshToken = (string) response.refresh_token; var refreshToken = (string)response.refresh_token;
// Get the user info // Get the Geni user
var userInfoRequest = new HttpRequestMessage(HttpMethod.Get, Options.Endpoints.UserEndpoint); var userInfoResponse = await _httpClient.GetAsync(
userInfoRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); Options.Endpoints.UserEndpoint + "?access_token=" + Uri.EscapeDataString(accessToken), Request.CallCancelled);
userInfoRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var userInfoResponse = await _httpClient.SendAsync(userInfoRequest);
userInfoResponse.EnsureSuccessStatusCode(); userInfoResponse.EnsureSuccessStatusCode();
text = await userInfoResponse.Content.ReadAsStringAsync(); text = await userInfoResponse.Content.ReadAsStringAsync();
var user = JObject.Parse(text); var user = JObject.Parse(text);

View File

@@ -26,8 +26,8 @@ namespace Owin.Security.Providers.Geni.Provider
AccessToken = accessToken; AccessToken = accessToken;
RefreshToken = refreshToken; RefreshToken = refreshToken;
User = user; User = user;
Name = user.SelectToken("user.name").ToString(); Name = user.SelectToken("name").ToString();
Id = user.SelectToken("user.translator_id").ToString(); Id = user.SelectToken("translator_id").ToString();
} }
/// <summary> /// <summary>