示例#1
0
 internal static async Task GetTokenIfEmptyAsync(AuthTokenType authTokenType)
 {
     if (authTokenType == AuthTokenType.SignIn && signInToken == null)
     {
         signInToken = Utility.ReadFromLocalStorage<OAuthToken>(Utility.SignInTokenKey);
         if (signInToken != null)
             await signInToken.GetActiveTokenAsync();
     }
     else if (authTokenType == AuthTokenType.SignUp && signUpToken == null)
     {
         signUpToken = Utility.ReadFromLocalStorage<OAuthToken>(Utility.SignUpTokenKey);
         if (signUpToken != null)
             await signUpToken.GetActiveTokenAsync();
     }
     else if (authTokenType == AuthTokenType.Simple && simpleToken == null)
     {
         simpleToken = Utility.ReadFromLocalStorage<OAuthToken>(Utility.SimpleTokenKey);
         if (simpleToken != null)
             await simpleToken.GetActiveTokenAsync();
     }
     else if (authTokenType == AuthTokenType.PrepaidSignIn && signInToken == null)
     {
         signInToken = Utility.ReadFromLocalStorage<OAuthToken>(Utility.PrepaidSignInTokenKey);
         if (signInToken != null)
             await signInToken.GetActiveTokenAsync();
     }
 }
示例#2
0
        public async Task <HttpResponseMessage> Delete(string relativeServicePath, AuthTokenType authTokenType)
        {
            var client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Session.GetAuthTokenAsync(authTokenType));
            return(await client.DeleteAsync(Session.Config.Environment.GetEnumDescription() + relativeServicePath));
        }
示例#3
0
        public async Task <IEntity> Put <T>(string relativeServicePath, AuthTokenType authTokenType, IEntity objectToPost = null, bool isRaw = false)
        {
            var client = new HttpClient();
            HttpResponseMessage response;

            if (authTokenType != AuthTokenType.None)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Session.GetAuthTokenAsync(authTokenType));
            }

            var serializer    = new JsonSerializer();
            var stringContent = new StringWriter();

            serializer.Serialize(stringContent, objectToPost);
            var content = new StringContent(stringContent.ToString(), Encoding.UTF8, "application/json");

            response = await client.PutAsync(Session.Config.Environment.GetEnumDescription() + relativeServicePath, content);

            if (response.IsSuccessStatusCode)
            {
                return
                    ((IEntity)
                     serializer.Deserialize <T>(
                         new JsonTextReader(new StringReader(await response.Content.ReadAsStringAsync()))));
            }

            return(await this.ReturnError(response));
        }
 /// <summary>
 /// Validate a token with the specific type
 /// </summary>
 /// <param name="type"> The token type </param>
 /// <param name="token"> The token </param>
 /// <returns> If the token is valid </returns>
 public static bool Validate(AuthTokenType type, string token)
 {
     try
     {
         validator[type](token);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
示例#5
0
        public UserTokenProvider(string clientId, string clientSecret, AuthTokenType tokenType, string token,
                                 bool useProd = false)
            : base(clientId, clientSecret, useProd)
        {
            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentNullException(nameof(token));
            }

            _tokenType = tokenType;
            _token     = token;
        }
示例#6
0
 internal static async Task<string> GetAuthTokenAsync(AuthTokenType authTokenType)
 {
     switch (authTokenType)
     {
         case AuthTokenType.SignIn:
             return signInToken != null ? await signInToken.GetActiveTokenAsync() : string.Empty;
         case AuthTokenType.SignUp:
             return signUpToken != null ? await signUpToken.GetActiveTokenAsync() : string.Empty;
         case AuthTokenType.Simple:
             return simpleToken != null ? await simpleToken.GetActiveTokenAsync() : string.Empty;
         case AuthTokenType.PrepaidSignIn:
             return signInToken != null ? await signInToken.GetActiveTokenAsync() : string.Empty;
         default:
             return string.Empty;
     }
 }
示例#7
0
        private void ProcessToken(AuthTokenType authTokenType = AuthTokenType.LiveAuthToken, string IdToken = "")
        {
            ExtractEnvelopeInfo();
            //Validate the envelope Info

            ExtractClaimsInfo();

            if (authTokenType == AuthTokenType.LiveAuthToken)
            {
                VerifySignatureInfo();
                LoadBaseResults();
            }
            else if (authTokenType == AuthTokenType.GoogleToken)
            {
                VerifyGoogleToken(IdToken);
                VerifyAndLoadGoogleBaseResults();
            }
        }
    public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
    {
        HttpRequestMessage incommingRequest = context.Request;
        string             authHeader       = incommingRequest.Headers["Authorization"];
        AuthTokenType      authTokenType    = DetecteAuthTokenType(authHeader);

        if (authTokenType == AuthTokenType.OAuth2Bearer)
        {
            // Validate auth token using the JwtSecurityTokenHandler class
        }
        else if (authTokenType == AuthTokenType.Custom)
        {
            // Validate auth token using whatever is necessary
        }
        else
        {
            // auth token doesn't correspond to a recognized type or hasn't been part of the client request - reject request
        }
    }
示例#9
0
        /// <summary>
        /// Get object from Service
        /// </summary>
        /// <param name="relativeServicePath">
        /// Relative REST method path
        /// </param>
        /// <typeparam name="T">
        /// Return object type
        /// </typeparam>
        /// <returns>
        /// Result Object
        /// </returns>
        public async Task <IEntity> Get <T>(string relativeServicePath, AuthTokenType authTokenType)
        {
            var client = new HttpClient();
            HttpResponseMessage response;

            if (authTokenType != AuthTokenType.None)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Session.GetAuthTokenAsync(authTokenType));
            }

            response = await client.GetAsync(Session.Config.Environment.GetEnumDescription() + relativeServicePath);

            if (response.IsSuccessStatusCode)
            {
                var serializer = new JsonSerializer();
                return
                    ((IEntity)
                     serializer.Deserialize <T>(
                         new JsonTextReader(new StringReader(await response.Content.ReadAsStringAsync()))));
            }

            return(await this.ReturnError(response));
        }
示例#10
0
 public AuthTokenValidator(string AuthToken, AuthTokenType authTokenType = AuthTokenType.LiveAuthToken)
 {
     ValidateAndSplitToken(AuthToken);
     ProcessToken(authTokenType, AuthToken);
 }
示例#11
0
 public AuthToken(AuthTokenType type, string value)
 {
     Type  = type;
     Value = value;
 }
示例#12
0
        /// <summary>
        /// Put object to Service
        /// </summary>
        /// <param name="relativeServicePath">
        /// Relative REST method path
        /// </param>
        /// <param name="urlParams">
        /// Parameters to be posted
        /// </param>
        /// <returns>
        /// Success or Failure
        /// </returns>
        public async Task <object> Put(string relativeServicePath, IEnumerable <KeyValuePair <string, string> > urlParams, AuthTokenType authTokenType)
        {
            var client = new HttpClient();
            HttpResponseMessage response;

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Session.GetAuthTokenAsync(authTokenType));
            var content = new FormUrlEncodedContent(urlParams);

            response = await client.PutAsync(Session.Config.Environment.GetEnumDescription() + relativeServicePath, content);

            if (response.IsSuccessStatusCode && response.StatusCode == HttpStatusCode.NoContent)
            {
                return(true);
            }

            return(new Error(await response.Content.ReadAsStringAsync()));
        }
示例#13
0
        public async Task <IEntity> Post <T>(string relativeServicePath, IEnumerable <KeyValuePair <string, string> > urlParams, AuthTokenType authTokenType, bool flag)
        {
            HttpResponseMessage response;
            HttpClientHandler   httpClientHandler = new HttpClientHandler();

            httpClientHandler.AllowAutoRedirect = false;
            using (HttpClient client = new HttpClient(httpClientHandler))
            {
                if (authTokenType != AuthTokenType.None)
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Session.GetAuthTokenAsync(authTokenType));
                }
                var content = new FormUrlEncodedContent(urlParams);
                response = await client.PostAsync(Session.Config.Environment.GetEnumDescription() + relativeServicePath, content);
            }

            if (response.IsSuccessStatusCode)
            {
                var serializer     = new JsonSerializer();
                var responseString = new StringReader(await response.Content.ReadAsStringAsync());
                var resp           =
                    (IEntity)
                    serializer.Deserialize <T>(
                        new JsonTextReader(responseString));
                return(resp);
            }

            return(await this.ReturnError(response));
        }
示例#14
0
        public async Task <HttpResponseMessage> PostNonRedirect(string relativeServicePath, AuthTokenType authTokenType, IEntity objectToPost = null)
        {
            Get_Cookie = string.Empty;
            HttpResponseMessage response = null;
            var serializer = new JsonSerializer();

            var handler = new HttpClientHandler()
            {
                AllowAutoRedirect = false
            };

            HttpClient client = new HttpClient(handler);

            if (authTokenType != AuthTokenType.None)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Session.GetAuthTokenAsync(authTokenType));
            }

            var content = new FormUrlEncodedContent(objectToPost.ToKeyValuePair());

            response =
                await client.PostAsync(Session.Config.Environment.GetEnumDescription() + relativeServicePath, content);

            return(response);

            //if (!response.IsSuccessStatusCode)
            //{
            //    string Set_Cookie = ((string[])response.Headers.GetValues("Set-Cookie"))[0];
            //    Set_Cookie = Set_Cookie.Substring(Set_Cookie.IndexOf("prepaiduser-payauth=") + 20);
            //    Set_Cookie = Set_Cookie.Substring(0, Set_Cookie.IndexOf("; Expires="));

            //    //Set_Cookie = Set_Cookie.Substring(Set_Cookie.IndexOf("prepaiduser-payauth=") + 20).Substring(0, Set_Cookie.Substring(Set_Cookie.IndexOf("prepaiduser-payauth=") + 20).IndexOf("; Expires="));
            //    return Set_Cookie;
            //}

            //return null;
        }
示例#15
0
        public async Task <IEntity> PostNonRedirect <T>(string relativeServicePath, AuthTokenType authTokenType, IEntity objectToPost = null, bool isRaw = false)
        {
            Get_Cookie = string.Empty;
            HttpResponseMessage response = null;
            var serializer = new JsonSerializer();

            var handler = new HttpClientHandler()
            {
                AllowAutoRedirect = false
            };

            HttpClient client = new HttpClient(handler);

            if (authTokenType != AuthTokenType.None)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Session.GetAuthTokenAsync(authTokenType));
            }

            if (!isRaw)
            {
                if (objectToPost != null)
                {
                    var content = new FormUrlEncodedContent(objectToPost.ToKeyValuePair());
                    response =
                        await client.PostAsync(Session.Config.Environment.GetEnumDescription() + relativeServicePath, content);
                }
                else
                {
                    response =
                        await
                        client.PostAsync(
                            Session.Config.Environment.GetEnumDescription() + relativeServicePath,
                            new StringContent(string.Empty));
                }
            }
            else
            {
                var stringContent = new StringWriter();
                serializer.Serialize(stringContent, objectToPost);
                var content = new StringContent(stringContent.ToString(), Encoding.UTF8, "application/json");
                response = await client.PostAsync(Session.Config.Environment.GetEnumDescription() + relativeServicePath, content);
            }
            if (response.IsSuccessStatusCode)
            {
                return
                    ((IEntity)
                     serializer.Deserialize <T>(
                         new JsonTextReader(new StringReader(await response.Content.ReadAsStringAsync()))));
            }
            else
            {
                string Set_Cookie = ((string[])response.Headers.GetValues("Set-Cookie"))[0];
                Set_Cookie = Set_Cookie.Substring(Set_Cookie.IndexOf("prepaiduser-payauth=") + 20);
                Set_Cookie = Set_Cookie.Substring(0, Set_Cookie.IndexOf("; Expires="));

                //Set_Cookie = Set_Cookie.Substring(Set_Cookie.IndexOf("prepaiduser-payauth=") + 20).Substring(0, Set_Cookie.Substring(Set_Cookie.IndexOf("prepaiduser-payauth=") + 20).IndexOf("; Expires="));
                Get_Cookie = Set_Cookie;
            }

            return(await this.ReturnError(response));
        }
示例#16
0
        public async Task <HttpResponseMessage> Put(string relativeServicePath, string jsonToPost, AuthTokenType authTokenType)
        {
            var client = new HttpClient();

            if (authTokenType != AuthTokenType.None)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Session.GetAuthTokenAsync(authTokenType));
            }
            var content = new StringContent(jsonToPost, Encoding.UTF8, "application/json");

            return(await client.PutAsync(Session.Config.Environment.GetEnumDescription() + relativeServicePath, content));
        }