public async Task <JwtAuthResult> RefreshToken(string refreshToken, string accessToken) { DateTime now = DateTime.UtcNow; (ClaimsPrincipal principal, JwtSecurityToken jwtToken) = DecodeJwtToken(accessToken); if (jwtToken == null || !jwtToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256Signature)) { throw new SecurityTokenException("Invalid params"); } string userName = principal.Identity?.Name; RefreshToken token = await m_tokenStorage.GetToken(refreshToken); if (!token.UserName.Equals(userName) || token.ExpiresAt < now) { throw new SecurityTokenException("Invalid params"); } return(await GenerateToken(userName, principal.Claims.ToArray())); }
public Signature(ITokenStorage storage) { _storage = storage; _random = new Random(); _app = _storage.GetToken("bluepill"); }
public async Task <HttpResponseMessage> SendRequest(string fqdn, Endpoint endpoint, Dictionary <string, string> parameters = null, string auth = null) { // Sets parameters as an empty dictionary if null parameters ??= new Dictionary <string, string>(); // Creates request HttpRequestMessage request = new HttpRequestMessage { Method = endpoint.Method }; // Creates a list of parameters in the url path Dictionary <string, string> pathParameters = parameters .Where(x => Regex.IsMatch(endpoint.Path, $@":{x.Key}(?!\w)")) .ToDictionary(x => x.Key, x => x.Value); // Creates a list of remaining parameters Dictionary <string, string> remainingParams = parameters .Except(pathParameters) .ToDictionary(x => x.Key, x => x.Value); // Creates url string string url = fqdn + pathParameters .Aggregate(endpoint.Path, (acc, next) => Regex.Replace(acc, $@":{next.Key}(?!\w)", next.Value)); // Checks if uri is properly formatted if (!Uri.IsWellFormedUriString(url, UriKind.Absolute)) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest); string json = JsonSerializer.Serialize(new Error(400, "Improper URL")); response.Content = new StringContent(json); return(response); } // adds parameters to request if (request.Method == HttpMethod.Get) { UriBuilder address = new UriBuilder(url); address.Query = await new FormUrlEncodedContent(remainingParams).ReadAsStringAsync().ConfigureAwait(false); request.RequestUri = new Uri(address.ToString()); } else { request.RequestUri = new Uri(url); string jsonBody = JsonSerializer.Serialize(remainingParams); request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json"); } // Adds authentication if (endpoint.AuthRequired) { request.Headers.Add("Authorization", $"Bearer {auth ?? _tokenStorage.GetToken()}"); } // Returns response try { return(await _client.SendAsync(request).ConfigureAwait(false)); } catch (Exception ex) { string json = JsonSerializer.Serialize(new Error(500, ex.Message)); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.InternalServerError); result.Content = new StringContent(json); return(result); } }