Skip to content
This repository has been archived by the owner on Mar 7, 2019. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Dec 28, 2017
2 parents 155b956 + 82056e4 commit 3de1ddf
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 120 deletions.
2 changes: 1 addition & 1 deletion UCWASDK/UCWASDK/Models/PhoneDialInInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class PhoneDialInInformation : UCWAModelBaseLink
internal InternalEmbedded Embedded { get; set; }

[JsonIgnore]
public DialInRegion[] DialInRegion { get { return Embedded.dialInRegion; } }
public DialInRegion[] DialInRegion { get { return Embedded?.dialInRegion; } }

internal class InternalEmbedded
{
Expand Down
6 changes: 3 additions & 3 deletions UCWASDK/UCWASDK/Models/Root.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ internal class InternalLinks

[JsonProperty("redirect")]
internal UCWAHref redirect { get; set; }

[JsonProperty("user")]
internal UCWAHref user { get; set; }

[JsonProperty("xframe")]
internal UCWAHref xframe { get; set; }
}

public async Task<Redirect> GetRedirect()
{
return Links.redirect == null ? null : await HttpService.Get<Redirect>(Links.redirect);
return Links.redirect == null ? null : await HttpService.Get<Redirect>(Links.redirect, anonymous: true);
}

public async Task<User> GetUser()
Expand Down
17 changes: 16 additions & 1 deletion UCWASDK/UCWASDK/Models/UCWAErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,20 @@ public VersionNotSupportedException(string message, Exception innerException) :
private bool _isTransient;
public bool IsTransient { get => _isTransient; set => _isTransient = value; }
}

public class AuthenticationExpiredException : Exception, IUCWAException
{
public override string Message { get { return $"Authentication information : {nameof(TrustedIssuers)} {TrustedIssuers}, {nameof(ClientId)} {ClientId}, {nameof(GrantType)} {GrantType}, {nameof(TokenUri)} {TokenUri}, {nameof(AuthorizationUri)} {AuthorizationUri}, {base.Message}"; } }
public AuthenticationExpiredException() : base() { }
public AuthenticationExpiredException(string message) : base(message) { }
public AuthenticationExpiredException(string message, Exception innerException) : base(message, innerException) { }
private Reason _reason;
public Reason Reason { get => _reason; set => _reason = value; }
private bool _isTransient;
public bool IsTransient { get => _isTransient; set => _isTransient = value; }
public string TrustedIssuers { get; internal set; }
public string ClientId { get; internal set; }
public string TokenUri { get; internal set; }
public string GrantType { get; internal set; }
public string AuthorizationUri { get; internal set; }
}
}
45 changes: 45 additions & 0 deletions UCWASDK/UCWASDK/Services/ExceptionMappingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,41 @@
using Microsoft.Skype.UCWA.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;

namespace Microsoft.Skype.UCWA.Services
{
internal class ExceptionMappingService
{
private const string matchKeyKey = "key";
private const string matchValueKey = "value";
private const string trustedIssuersKey = "trusted_issuers";
private const string clientIdKey = "client_id";
private const string grantTypeKey = "grant_type";
private const string tokenKey = "href";
private const string authorizationKey = "authorization_uri";
private static Regex authenticationMatchingRegex = new Regex($@"(?<{matchKeyKey}>[\w]+)=""(?<{matchValueKey}>[\w -@\.:/,\*]+)""?");
private Dictionary<string, string> getAuthenticationHeaderValues(HttpResponseHeaders headers)
{
var value = new Dictionary<string, string>();
var matches = headers.WwwAuthenticate.SelectMany( x => x.Parameter.Split(new string[] { "\"," }, StringSplitOptions.RemoveEmptyEntries)).Select(x => authenticationMatchingRegex.Match(x)).Where(x => x.Success);
foreach (var match in matches)
{
var mtch = match;
do
{
value.Add(match.Groups[matchKeyKey].Value, match.Groups[matchValueKey].Value);
mtch = mtch.NextMatch();
}
while (mtch.Success);
}
return value;
}
internal Exception GetExceptionFromHttpStatusCode(HttpResponseMessage response, string error)
{// we can start by guessing what kind of error it is by relying on the protocol first https://ucwa.skype.com/documentation/ProgrammingConcepts-Errors
var reason = TryDeserializeReason(error);
Expand Down Expand Up @@ -41,6 +69,23 @@ internal Exception GetExceptionFromHttpStatusCode(HttpResponseMessage response,
return new PreconditionFailedException(reason?.Message ?? error) { Reason = reason, IsTransient = false };
#endregion
#region nontransient
case HttpStatusCode.Unauthorized:
if (response.Headers.WwwAuthenticate.Any())
{
var authenticationHeaderValues = getAuthenticationHeaderValues(response.Headers);
return new AuthenticationExpiredException(reason?.Message ?? error)
{
Reason = reason,
IsTransient = true,
ClientId = authenticationHeaderValues[clientIdKey],
GrantType = authenticationHeaderValues[grantTypeKey],
TokenUri = authenticationHeaderValues[tokenKey],
TrustedIssuers = authenticationHeaderValues[trustedIssuersKey],
AuthorizationUri = authenticationHeaderValues[authorizationKey],
};
}
else
return new UnauthorizedAccessException(error);
case HttpStatusCode.Forbidden:
return new UnauthorizedAccessException(error);
case HttpStatusCode.RequestEntityTooLarge:
Expand Down
Loading

0 comments on commit 3de1ddf

Please sign in to comment.