ClientElement GetClientInfo(NameValueCollection body, BaseValidatingContext <OAuthAuthorizationServerOptions> context) { var oauth2Config = ConfigurationManager.GetSection("oauth2") as Oauth2Section; var clientId = body["client_id"]; if (clientId == null) { context.SetError("'client_id' not found"); context.Rejected(); return(null); } var client = oauth2Config.clients.GetSource(clientId); if (client == null) { context.SetError("'client_id' not found"); context.Rejected(); return(null); } if (!String.IsNullOrEmpty(client.allowIp) && client.allowIp != "*") { // TODO: check Ip } if (!String.IsNullOrEmpty(client.allowOrigin) && client.allowOrigin != "*") { // TODO: checkOrigin } return(client); }
public virtual JwtToken Create(BaseValidatingContext baseValidatingContext, JwtServerOptions jwtServerOptions) { if (baseValidatingContext == null) { throw new ArgumentNullException(nameof(baseValidatingContext)); } if (jwtServerOptions == null) { throw new ArgumentNullException(nameof(jwtServerOptions)); } var tokenHadler = new TokenHandler(jwtServerOptions); var startingDate = DateTime.UtcNow; var expiresDate = DateTime.UtcNow.Add(jwtServerOptions.AccessTokenExpireTimeSpan); var token = tokenHadler.GenerateToken( claims: baseValidatingContext.Claims, notBefore: startingDate, expires: expiresDate); var result = new JwtToken { AccessToken = token, ExpiresIn = this.GetTokenExpiral(startingDate, expiresDate), TokenType = JwtBearerDefaults.AuthenticationScheme }; return(result); }
public static async Task CallAndHandleError <T>(Func <Task> action, BaseValidatingContext <T> context, IOwinExceptionHandler exceptionHandler) { Exception exception = null; try { await action(); if (context.HasError) { Trace.TraceWarning( "Non-exceptional error during authorization. Error: '{0}', ErrorDescription: '{1}', ErrorUri: '{2}'", context.Error, context.ErrorDescription, context.ErrorUri); } } catch (Exception t) { exception = t; } if (exception != null) { await exceptionHandler.ReportExceptionAndCreateResponseAsync(context, exception); } }
public static async Task ReportExceptionAndCreateResponseAsync <T>( BaseValidatingContext <T> context, Exception exception, string developerName) { string identifier = string.Empty; try { // I don't want to use Autofac here as it may be the dependency resolution // causing the error. var developer = await HardwiredDependencies.NewDefaultDeveloperRepository().TryGetByGitNameAsync(developerName); identifier = exception.GetExceptionIdentifier(); await HardwiredDependencies.NewErrorReportingService().ReportErrorAsync(exception, identifier, developer); context.SetError("internal_error", "Something went wrong: " + identifier); } catch (Exception t) { System.Diagnostics.Trace.TraceError("Failed to report errors: " + t); context.SetError("internal_error", "An error occurred and could not be reported: " + identifier); } }
public Task ReportExceptionAndCreateResponseAsync <T>( BaseValidatingContext <T> context, Exception exception) { var developerName = ExceptionHandlerUtilities.GetDeveloperName(context.Request); return(ExceptionHandlerUtilities.ReportExceptionAndCreateResponseAsync(context, exception, developerName)); }
public virtual async Task <JwtToken> CreateAsync(BaseValidatingContext baseValidatingContext, JwtServerOptions jwtServerOptions) { Validate(); var tokenHadler = new TokenHandler(jwtServerOptions); var startingDate = DateTime.UtcNow; var expiresDate = DateTime.UtcNow.Add(jwtServerOptions.AccessTokenExpireTimeSpan); var token = tokenHadler.GenerateToken( claims: baseValidatingContext.Claims, notBefore: startingDate, expires: expiresDate); var result = new JwtToken { AccessToken = token, ExpiresIn = this.GetTokenExpiral(startingDate, expiresDate), TokenType = JwtBearerDefaults.AuthenticationScheme }; if (jwtServerOptions.RefreshTokenProvider != null) { var refreshToken = await jwtServerOptions.RefreshTokenProvider.GenerateAsync(baseValidatingContext.Claims); if (!string.IsNullOrWhiteSpace(refreshToken)) { result.RefreshToken = refreshToken; } } void Validate() { if (baseValidatingContext == null) { throw new ArgumentNullException(nameof(baseValidatingContext)); } if (jwtServerOptions == null) { throw new ArgumentNullException(nameof(jwtServerOptions)); } } return(result); }
public static AuthRequest GetAuthRequest(this BaseValidatingContext <OAuthAuthorizationServerOptions> context) { if (context == null || context.Request == null) { return(null); } Stream req = context.Request.Body; req.Seek(0, System.IO.SeekOrigin.Begin); string json = new StreamReader(req).ReadToEnd(); AuthRequest authRequest = null; try { authRequest = JsonConvert.DeserializeObject <AuthRequest>(json); return(authRequest); } catch { return(null); } }
} // ValidateClientAuthentication private Task clientIdError <T>(BaseValidatingContext <T> context, string errorMsg) { context.SetError(invalidClientId, errorMsg); return(Task.FromResult <object>(null)); }