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(); } }
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)); }
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); } }
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; }
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; } }
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 } }
/// <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)); }
public AuthTokenValidator(string AuthToken, AuthTokenType authTokenType = AuthTokenType.LiveAuthToken) { ValidateAndSplitToken(AuthToken); ProcessToken(authTokenType, AuthToken); }
public AuthToken(AuthTokenType type, string value) { Type = type; Value = value; }
/// <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())); }
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)); }
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; }
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)); }
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)); }