/// <summary> /// Parse OAuthArguments from parameters to the makeRequest servlet. /// </summary> /// /// <param name="auth">authentication type for the request</param> /// <param name="request">servlet request</param> /// @throws GadgetExceptionif any parameters are invalid. public OAuthArguments(AuthType auth, HttpRequest request) { signViewer = false; signOwner = false; bypassSpecCache = false; origClientState = null; requestTokenSecret = null; requestToken = null; tokenName = ""; serviceName = ""; useToken = UseToken.ALWAYS; useToken = ParseUseToken(auth, GetRequestParam(request, USE_TOKEN_PARAM, "")); serviceName = GetRequestParam(request, SERVICE_PARAM, ""); tokenName = GetRequestParam(request, TOKEN_PARAM, ""); requestToken = GetRequestParam(request, REQUEST_TOKEN_PARAM, null); requestTokenSecret = GetRequestParam(request, REQUEST_TOKEN_SECRET_PARAM, null); origClientState = GetRequestParam(request, CLIENT_STATE_PARAM, null); bypassSpecCache = "1".Equals(GetRequestParam(request, BYPASS_SPEC_CACHE_PARAM, null)); signOwner = Boolean.Parse(GetRequestParam(request, SIGN_OWNER_PARAM, "true")); signViewer = Boolean.Parse(GetRequestParam(request, SIGN_VIEWER_PARAM, "true")); }
public OAuthArguments(RequestAuthenticationInfo info) { signViewer = false; signOwner = false; bypassSpecCache = false; origClientState = null; requestTokenSecret = null; requestToken = null; tokenName = ""; serviceName = ""; useToken = UseToken.ALWAYS; Dictionary <string, string> attrs = info.getAttributes(); useToken = ParseUseToken(info.getAuthType(), GetAuthInfoParam(attrs, USE_TOKEN_PARAM, "")); serviceName = GetAuthInfoParam(attrs, SERVICE_PARAM, ""); tokenName = GetAuthInfoParam(attrs, TOKEN_PARAM, ""); requestToken = GetAuthInfoParam(attrs, REQUEST_TOKEN_PARAM, null); requestTokenSecret = GetAuthInfoParam(attrs, REQUEST_TOKEN_SECRET_PARAM, null); origClientState = null; bypassSpecCache = false; signOwner = info.isSignOwner(); signViewer = info.isSignViewer(); foreach (var pair in info.getAttributes()) { requestOptions.Add(pair.Key, pair.Value); } }
public async Task <ActionResult <JwtDto> > Use([FromBody] UseToken command) { command.TokenId = new Guid(); await DispatchAsync(command); var jwt = _cache.GetJwt(command.TokenId); return(Ok(jwt)); }
/// <summary> /// Create an OAuthArguments object with all default values. The details can /// be filled in later using the setters. /// Be careful using this in anything except test code. If you find yourself /// wanting to use this method in real code, consider writing a new /// constructor instead. /// </summary> /// public OAuthArguments() { signViewer = false; signOwner = false; bypassSpecCache = false; origClientState = null; requestTokenSecret = null; requestToken = null; tokenName = ""; serviceName = ""; useToken = UseToken.ALWAYS; }
static private void addAuthenticationHeader(KZApplication app, UseToken useToken, Dictionary <string, string> headers) { Token token = null; switch (useToken) { case UseToken.Application: token = app.Authentication.User.TokenApplication; break; case UseToken.Marketplace: token = app.Authentication.User.TokenMarketplace; break; case UseToken.ServiceBus: token = app.Authentication.User.TokenServiceBus; break; } if (token != null) { headers["Authorization"] = "WRAP access_token=\"" + token.Value + "\""; } }
/// <summary> /// Copy constructor. /// </summary> /// public OAuthArguments(OAuthArguments orig) { signViewer = false; signOwner = false; bypassSpecCache = false; origClientState = null; requestTokenSecret = null; requestToken = null; tokenName = ""; serviceName = ""; useToken = UseToken.ALWAYS; useToken = orig.useToken; serviceName = orig.serviceName; tokenName = orig.tokenName; requestToken = orig.requestToken; requestTokenSecret = orig.requestTokenSecret; origClientState = orig.origClientState; bypassSpecCache = orig.bypassSpecCache; signOwner = orig.signOwner; signViewer = orig.signViewer; }
public void setUseToken(UseToken _useToken) { useToken = _useToken; }
internal static async Task <ServiceEvent <T> > ExecuteStreamAsync <T>(this Uri uri, KZApplication app, Stream content = null, string method = "GET", bool cache = false, TimeSpan?timeout = null, Dictionary <string, string> headers = null, UseToken useToken = UseToken.Application, Action <long[]> onProgress = null, bool cors = false) where T : Stream { Request request = null; Response response = null; if (headers == null) { headers = new Dictionary <string, string>(); } try { // Does the URL require a no cache? if (!cache) { uri = addNoCache(uri); headers.Add("Cache-Control", "no-cache"); headers.Add("Pragma", "no-cache"); } // Adds authentication's header if (useToken != UseToken.None && app != null && app.Authentication != null && app.Authentication.User != null) { addAuthenticationHeader(app, useToken, headers); } request = await Request.Create(uri, method.ToUpper(), content, headers, timeout); response = (method == "POST" || method == "GET" || method == "PUT") ? await request.Send_POST_GET_PUT(onProgress) : await request.Send_OTHERS(onProgress); // Is token expired? if (response.StatusCode == HttpStatusCode.Unauthorized && app.User != null) { // Refresh token if it is expired var authHeader = response.Headers["WWW-Authenticate"]; if (!string.IsNullOrWhiteSpace(authHeader)) { var realm = authHeader .Split(',') .Where(r => r.StartsWith("error=")) .FirstOrDefault(); if (!string.IsNullOrWhiteSpace(realm)) { var message = realm.Split('=')[1].Trim(); if (string.Compare(message, "\"Token is expired\"", StringComparison.CurrentCultureIgnoreCase) == 0) { // Do refresh tokens app.Authentication.RemoveFromCache(app.User.Credential.UserName, app.User.Credential.Password, app.User.Provider); await app.Authentication.Authenticate(app.User.Credential.UserName, app.User.Credential.Password, app.User.Provider); // Set new auth header addAuthenticationHeader(app, useToken, request.Headers); request.Content.Seek(0, SeekOrigin.Begin); // Send request response = (method == "POST" || method == "GET" || method == "PUT") ? await request.Send_POST_GET_PUT(onProgress) : await request.Send_OTHERS(onProgress); } } } } // Process response var evt = new ServiceEvent <T>(); evt.StatusCode = response.StatusCode; evt.Headers = response.Headers; var read = 0L; var total = response.Body == null ? 0 : response.Body.Length; // Download the body as stream an send progress information // Sends initial progress notification if (onProgress != null) { onProgress(new[] { read, total }); } // Creates the stream that will be returned to the client var result = new MemoryStream(); if (total > 0) { // Copies the response body's stream var buffer = new byte[4096]; var bytesRead = await response.Body.ReadAsync(buffer, 0, 4096); while (bytesRead > 0) { result.WriteAsync(buffer, 0, bytesRead); if (onProgress != null) { read += bytesRead; onProgress(new[] { read, total }); } bytesRead = await response.Body.ReadAsync(buffer, 0, 4096); } // Rewinds the stream result.Seek(0, SeekOrigin.Begin); } evt.DataAsStream = (Stream)result; return(evt); } catch (Exception) { throw; } finally { if (request != null) { request.Dispose(); } if (response != null) { response.Dispose(); } } }
internal static async Task <ServiceEvent <T> > ExecuteAsync <T>(this Uri uri, KZApplication app, JToken content, string method = "GET", bool cache = false, TimeSpan?timeout = null, Dictionary <string, string> headers = null, UseToken useToken = UseToken.Application, Action <long[]> onProgress = null, bool cors = false) { MemoryStream stream = null; TextWriter writer = null; if (content != null) { stream = new MemoryStream(); writer = new StreamWriter(stream); content.SerializeJson(writer); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); if (headers == null) { headers = new Dictionary <string, string>(); } headers["Content-Type"] = "application/json"; } var result = await uri.ExecuteAsync <T>(app, stream, method, cache, timeout, headers, useToken, onProgress, cors); if (writer != null) { writer.Dispose(); } if (stream != null) { writer.Dispose(); } return(result); }
internal static async Task <ServiceEvent <T> > ExecuteAsync <T>(this Uri uri, KZApplication app, Stream content = null, string method = "GET", bool cache = false, TimeSpan?timeout = null, Dictionary <string, string> headers = null, UseToken useToken = UseToken.Application, Action <long[]> onProgress = null, bool cors = false) { Request request = null; Response response = null; if (headers == null) { headers = new Dictionary <string, string>(); } try { // Does the URL require a no cache? if (!cache) { uri = addNoCache(uri); headers.Add("Cache-Control", "no-cache"); headers.Add("Accept", "*/*"); headers.Add("Pragma", "no-cache"); } if (timeout != null && timeout.HasValue) { headers.Add("timeout", timeout.Value.TotalSeconds.ToString()); } //**** Passive Auth HotFix **** if (app != null && app.PassiveAuthenticationInformation != null) { headers["Authorization"] = "WRAP access_token=\"" + app.PassiveAuthenticationInformation["access_token"] + "\""; } else { // Adds authentication's header if (useToken != UseToken.None && app != null && app.Authentication != null && app.Authentication.User != null) { addAuthenticationHeader(app, useToken, headers); } } request = await Request.Create(uri, method.ToUpper(), content, headers, timeout); response = (method == "POST" || method == "GET" || method == "PUT")? await request.Send_POST_GET_PUT(onProgress) : await request.Send_OTHERS(onProgress); // Is token expired? if (response.StatusCode == HttpStatusCode.Unauthorized && app.User != null) { // Refresh token if it is expired var authHeader = response.Headers["WWW-Authenticate"]; if (!string.IsNullOrWhiteSpace(authHeader)) { var realm = authHeader .Split(',') .Where(r => r.StartsWith("error=")) .FirstOrDefault(); if (!string.IsNullOrWhiteSpace(realm)) { var message = realm.Split('=')[1].Trim(); if (string.Compare(message, "\"Token is expired\"", StringComparison.CurrentCultureIgnoreCase) == 0) { //**** Passive Auth HotFix **** if (app.PassiveAuthenticationInformation != null) { var newAuthToken = refreshPassiveToken(app.PassiveAuthenticationInformation); request.Headers["Authorization"] = "WRAP access_token=\"" + newAuthToken + "\""; request.Content.Seek(0, SeekOrigin.Begin); // Send request response = (method == "POST" || method == "GET" || method == "PUT") ? await request.Send_POST_GET_PUT(onProgress) : await request.Send_OTHERS(onProgress); } else { // Do refresh tokens app.Authentication.RemoveFromCache(app.User.Credential.UserName, app.User.Credential.Password, app.User.Provider); await app.Authentication.Authenticate(app.User.Credential.UserName, app.User.Credential.Password, app.User.Provider); // Set new auth header addAuthenticationHeader(app, useToken, request.Headers); request.Content.Seek(0, SeekOrigin.Begin); // Send request response = (method == "POST" || method == "GET" || method == "PUT") ? await request.Send_POST_GET_PUT(onProgress) : await request.Send_OTHERS(onProgress); } } } } } // Process response var evt = new ServiceEvent <T>(); evt.StatusCode = response.StatusCode; evt.Headers = response.Headers; if (typeof(T) == typeof(object)) { using (var stream = response.Body) { using (var reader = new StreamReader(stream, UTF8Encoding.UTF8)) { var data = await reader.ReadToEndAsync(); evt.DataAsString = data; } evt.DataAsStream = response.Body; } } else if (response.Headers.ContainsKey("Content-Type") && response.Headers["Content-Type"].Contains("application/json")) { using (var reader = new StreamReader(response.Body, Encoding.UTF8)) { using (var jsonReader = new JsonTextReader(reader)) { evt.Data = serializer.Deserialize <T>(jsonReader); } } } return(evt); } catch (Exception e) { throw; } finally { if (request != null) { request.Dispose(); } if (response != null) { response.Dispose(); } } }
public OAuthArguments(RequestAuthenticationInfo info) { signViewer = false; signOwner = false; bypassSpecCache = false; origClientState = null; requestTokenSecret = null; requestToken = null; tokenName = ""; serviceName = ""; useToken = UseToken.ALWAYS; Dictionary<string, string> attrs = info.getAttributes(); useToken = ParseUseToken(info.getAuthType(), GetAuthInfoParam(attrs, USE_TOKEN_PARAM, "")); serviceName = GetAuthInfoParam(attrs, SERVICE_PARAM, ""); tokenName = GetAuthInfoParam(attrs, TOKEN_PARAM, ""); requestToken = GetAuthInfoParam(attrs, REQUEST_TOKEN_PARAM, null); requestTokenSecret = GetAuthInfoParam(attrs, REQUEST_TOKEN_SECRET_PARAM, null); origClientState = null; bypassSpecCache = false; signOwner = info.isSignOwner(); signViewer = info.isSignViewer(); foreach (var pair in info.getAttributes()) { requestOptions.Add(pair.Key,pair.Value); } }