/// <summary>Invoke /// </summary> public async Task InvokeAsync(RestExecuteContext context) { if (!_option.RestOption.EnableJwt) { await _next.Invoke(context); return; } try { //开启Jwt认证,需要在发起了AssignFileKey请求之后,将数据保存下来 if (context.Response.GetType() == typeof(AssignFileKeyResponse)) { var fid = ((AssignFileKeyResponse)context.Response).Fid; var authentication = context.Response.Headers.FirstOrDefault(x => x.Name.Equals("Authorization", StringComparison.OrdinalIgnoreCase)); //添加到token管理器 _jwtManager.AddAssignJwt(fid, authentication.Value.ToString()); } } catch (Exception ex) { SetPipelineError(context, new AssignJwtError($"AssignJwt出错,{ex.Message}")); return; } await _next.Invoke(context); }
/// <summary>Invoke /// </summary> public async Task InvokeAsync(RestExecuteContext context) { if (!_option.RestOption.EnableJwt) { await _next.Invoke(context); return; } try { //上传,删除文件之前获取Jwt if (IsPreAuthRequestAuth(context.Request.GetType())) { //Assign Jwt var jwt = _jwtManager.GetAssignJwt(context.Request.Fid); //添加认证头部 context.Builder.AddParameter("Authorization", jwt, ParameterType.HttpHeader); } } catch (Exception ex) { SetPipelineError(context, new AssignJwtError($"UploadAuthentication出错,{ex.Message}")); return; } await _next.Invoke(context); }
/// <summary>Invoke /// </summary> public async Task InvokeAsync(RestExecuteContext context) { if (!_option.RestOption.EnableJwt) { await _next.Invoke(context); return; } try { if (context.Response.GetType() == typeof(LookupResponse)) { var lookupRequest = (LookupRequest)context.Request; var authentication = context.Response.Headers.FirstOrDefault(x => x.Name.Equals("Authorization", StringComparison.OrdinalIgnoreCase)); if (lookupRequest.IsRead.HasValue && lookupRequest.IsRead.Value) { if (_option.RestOption.EnableReadJwt) { //添加到Read Jwt管理器 _jwtManager.AddReadJwt(context.Request.Fid, authentication.Value.ToString()); } } else { //添加到token管理器 _jwtManager.AddLookupJwt(context.Request.Fid, authentication.Value.ToString()); } } } catch (Exception ex) { SetPipelineError(context, new LookupJwtError($"Lookup Jwt出错,{ex.Message}")); return; } await _next.Invoke(context); }
/// <summary>Invoke /// </summary> public async Task InvokeAsync(RestExecuteContext context) { try { var httpBuilder = context.Request.CreateBuilder(); context.Builder = httpBuilder; } catch (Exception ex) { SetPipelineError(context, new BuildHttpError($"构建HttpBuilder出错,{ex.Message}")); return; } await _next.Invoke(context); }
/// <summary>Invoke /// </summary> public async Task InvokeAsync(RestExecuteContext context) { try { //获取连接 var connection = GetConnection(context); if (connection == null) { Logger.LogError("无法根据请求的相关数据获取连接Connection!"); SetPipelineError(context, new ExecuteError("无法根据请求的相关数据获取连接Connection!")); return; } IRestRequest restRequest = context.Builder.BuildRequest(); var restResponse = await connection.ExecuteAsync(restRequest); Logger.LogDebug("Execute执行返回结果为:{0}", restResponse.Content); //设置response返回的状态,头部Header等 if (!restResponse.IsSuccessful) { SetPipelineError(context, new ExecuteError("RestResponse返回数据不正确!")); return; } context.Response = _jsonSerializer.Deserialize(restResponse.Content, context.Response.GetType()) as SeaweedfsResponse; context.Response.IsSuccessful = restResponse.IsSuccessful; context.Response.ErrorException = restResponse.ErrorException; context.Response.ErrorMessage = restResponse.ErrorMessage; foreach (var header in restResponse.Headers) { context.Response.Headers.Add(header.TransferToParameter()); } } catch (Exception ex) { SetPipelineError(context, new ExecuteError($"调用Execute出错,{ex.Message}")); return; } await _next.Invoke(context); }