Skip to content

Commit

Permalink
ImplicitFlowVkAuthorization: рефакторинг
Browse files Browse the repository at this point in the history
* Обновить сообщения об ошибках

* Не выбрасывать исключения на опциональных отсутствующих параметрах

* Упростить алгоритм парсинга query
  • Loading branch information
itsokto committed May 23, 2020
1 parent 1e687bf commit b7dd567
Showing 1 changed file with 9 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using JetBrains.Annotations;
using VkNet.Enums;
using VkNet.Exception;
using VkNet.Model;
using VkNet.Utils;

namespace VkNet.Infrastructure.Authorization.ImplicitFlow
{
/// <inheritdoc />
[UsedImplicitly]
[SuppressMessage("ReSharper", "SuggestBaseTypeForParameter")]
public class ImplicitFlowVkAuthorization : IVkAuthorization<ImplicitFlowPageType>
{
private const string AccessToken = "access_token";
Expand All @@ -23,15 +22,14 @@ public class ImplicitFlowVkAuthorization : IVkAuthorization<ImplicitFlowPageType
private const string UserId = "user_id";

/// <inheritdoc />
[JetBrains.Annotations.NotNull]
[UsedImplicitly]
[NotNull]
public AuthorizationResult GetAuthorizationResult(Uri url)
{
var pageType = GetPageType(url);

if (pageType != ImplicitFlowPageType.Result)
{
throw new VkAuthorizationException("URL должен содержать токен доступа.");
throw new VkAuthorizationException($"{ImplicitFlowPageType.Result} page expected, but was {pageType}.");
}

var parameters = GetFragmentParameters(url);
Expand Down Expand Up @@ -86,37 +84,29 @@ private static string GetToken(Dictionary<string, string> parameters)
{
if (!parameters.TryGetValue(AccessToken, out var token))
{
throw new VkApiException($"Параметр {AccessToken} не найден");
throw new VkAuthorizationException($"Missing URL parameter: '{AccessToken}'");
}

return token;
}

private static string GetState(Dictionary<string, string> parameters)
{
if (!parameters.TryGetValue(State, out var state))
{
throw new VkApiException($"Параметр {State} не найден");
}
parameters.TryGetValue(State, out var state);

return state;
}

private static int GetExpiresIn(Dictionary<string, string> parameters)
{
if (!parameters.TryGetValue(ExpiresIn, out var expiresIn))
{
throw new VkApiException($"Параметр {ExpiresIn} не найден");
}

return int.Parse(expiresIn);
return parameters.TryGetValue(ExpiresIn, out var expiresIn) ? int.Parse(expiresIn) : 0;
}

private static long GetUserId(Dictionary<string, string> parameters)
{
if (!parameters.TryGetValue(UserId, out var userId))
{
throw new VkApiException($"Параметр {UserId} не найден");
throw new VkAuthorizationException($"Missing URL parameter: '{UserId}'");
}

return long.Parse(userId);
Expand All @@ -139,15 +129,8 @@ private static Dictionary<string, string> GetQueryParameters(Uri url)
private static Dictionary<string, string> GetParams(string query)
{
return query.Split(new[] { "&" }, StringSplitOptions.RemoveEmptyEntries)
.ToDictionary(x => x.Split(new[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[0],
ParseDictionaryValue);
}

private static string ParseDictionaryValue(string x)
{
var parametersPair = x.Split(new[] { "=" }, StringSplitOptions.RemoveEmptyEntries);

return parametersPair.Length > 1 ? parametersPair[1] : string.Empty;
.Select(q => q.Split('='))
.ToDictionary(x => x.First(), x => x.Last());
}
}
}

0 comments on commit b7dd567

Please sign in to comment.