<commit_before>﻿namespace bgTeam.SSO.Client
{
    using System;
    using System.IdentityModel.Tokens.Jwt;
    using System.Net.Http;
    using System.Threading.Tasks;
    using Newtonsoft.Json;

    // TODO: cache responses: public key -> response
    // TODO: http client issues review: http://www.nimaara.com/2016/11/01/beware-of-the-net-httpclient/
    // TODO: add some client-side check. Don't call the remote service unless the client-side checks were passed
    // TODO: get rid of using issuer or make cipher asymmetric and validate it via public key https://piotrgankiewicz.com/2017/07/24/jwt-rsa-hmac-asp-net-core/
    public class SsoClient
    {
        public async Task<bool> ValidateToken(string token)
        {
            const string validateUrl = "api/auth/validatetoken";

            var address = $"https://{GetIssuerAddressFromToken(token)}/{validateUrl}";
            SsoReplyResult reply = null;
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
                var res = await httpClient.GetStringAsync(new Uri(address));
                reply = JsonConvert.DeserializeObject<SsoReplyResult>(res);
            }

            return reply.Succeeded;
        }

        private string GetIssuerAddressFromToken(string token)
        {
            var jwtHandler = new JwtSecurityTokenHandler();
            var jwtToken = jwtHandler.ReadJwtToken(token);

            return jwtToken.Issuer;<commit_msg>remove "https://" prefix from sso validate token address<commit_after>﻿namespace bgTeam.SSO.Client
{
    using System;
    using System.IdentityModel.Tokens.Jwt;
    using System.Net.Http;
    using System.Threading.Tasks;
    using Newtonsoft.Json;

    // TODO: cache responses: public key -> response
    // TODO: http client issues review: http://www.nimaara.com/2016/11/01/beware-of-the-net-httpclient/
    // TODO: add some client-side check. Don't call the remote service unless the client-side checks were passed
    // TODO: get rid of using issuer or make cipher asymmetric and validate it via public key https://piotrgankiewicz.com/2017/07/24/jwt-rsa-hmac-asp-net-core/
    public class SsoClient
    {
        public async Task<bool> ValidateToken(string token)
        {
            const string validateUrl = "api/auth/validatetoken";

            var address = $"{GetIssuerAddressFromToken(token)}/{validateUrl}";
            SsoReplyResult reply = null;
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
                var res = await httpClient.GetStringAsync(new Uri(address));
                reply = JsonConvert.DeserializeObject<SsoReplyResult>(res);
            }

            return reply.Succeeded;
        }

        private string GetIssuerAddressFromToken(string token)
        {
            var jwtHandler = new JwtSecurityTokenHandler();
            var jwtToken = jwtHandler.ReadJwtToken(token);

            return jwtToken.Issuer;
{'fraction_non_alphanumeric': 0.09591245574509173, 'fraction_numerical': 0.010299324106855488, 'mean_word_length': 2.492134831460674, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7372133', 'n_tokens_mistral': 888, 'n_tokens_neox': 848, 'n_words': 216}