public static ITokenContext GrantAccessToken(this ITokenContext context) { ITokenProvider provider = GetProvider(); provider.GrantAccessToken(context); return(context); }
public void GrantAccessToken(ITokenContext context) { try { InspectContext(context); bool handled = false; var processors = ServiceLocator.Current.GetAllInstances<ContextProcessor<ITokenContext>>(); foreach (ContextProcessor<ITokenContext> processor in processors) { if (!processor.IsSatisfiedBy(context)) continue; handled = true; processor.Process(context); break; } if (!handled) throw Errors.UnsupportedGrantType(context); } catch (OAuthErrorResponseException<ITokenContext> x) { context.Error = new ErrorResponse { Error = x.Error, ErrorDescription = x.Message, ErrorUri = x.ErrorUri }; } }
public TokenResponse CreateResponse(ITokenContext context) { if (context.Error != null) { TokenResponse errorResponse = new TokenResponse(); errorResponse.HttpStatusCode = 400; IDictionary <string, object> errorResults = context.Error.ToResponseValues(); if (context.Error.Error == Parameters.ErrorParameters.ErrorValues.InvalidClient) { errorResponse.HttpStatusCode = 401; } errorResponse.Body = SerializeResponse(errorResults); return(errorResponse); } if (context.Token == null) { throw new OAuthFatalException(TokenEndpointResources.ContextDoesNotContainToken); } TokenResponse response = new TokenResponse { HttpStatusCode = 200 }; response.Body = SerializeResponse(((ITokenizer)context.Token).ToResponseValues()); return(response); }
internal static OAuthErrorResponseException <ITokenContext> InvalidGrant(ITokenContext context) { return(new OAuthErrorResponseException <ITokenContext>(context, Parameters.ErrorParameters.ErrorValues.InvalidGrant, description: string.Format(CultureInfo.CurrentUICulture, TokenEndpointResources.InvalidGrant, context.AuthorizationCode))); }
public NumAttributeRange(string attrName, bool isFloat, double from, double?to, ITokenContext context) { AttrName = attrName; IsFloat = isFloat; From = from; To = to; Context = context; }
private void InspectContext(ITokenContext context) { new SpecificationInspector().Inspect(context); new AuthorizationCodeInspector().Inspect(context); new RefreshAccessTokenInspector().Inspect(context); new ResourceOwnerPasswordCredentialInspector().Inspect(context); IEnumerable<IContextInspector<ITokenContext>> inspectors = ServiceLocator.Current.GetAllInstances<IContextInspector<ITokenContext>>(); foreach (IContextInspector<ITokenContext> inspector in inspectors) inspector.Inspect(context); }
public IToken IssueAccessTokenForResourceOwner(ITokenContext context) { AccessToken token = new AccessToken { ExpiresIn = 120, Token = Guid.NewGuid().ToString(), RefreshToken = Guid.NewGuid().ToString(), Scope = new string[] { "create", "delete", "view" }, }; TokenRepo.AddAccessToken(token); return(token); }
public IToken IssueAccessTokenForResourceOwner(ITokenContext context) { AccessToken token = new AccessToken { ExpiresIn = 120, Token = Guid.NewGuid().ToString(), RefreshToken = Guid.NewGuid().ToString(), Scope = new string[] { "create", "delete", "view" }, }; TokenRepo.AddAccessToken(token); return token; }
private void InspectContext(ITokenContext context) { new SpecificationInspector().Inspect(context); new AuthorizationCodeInspector().Inspect(context); new RefreshAccessTokenInspector().Inspect(context); new ResourceOwnerPasswordCredentialInspector().Inspect(context); IEnumerable <IContextInspector <ITokenContext> > inspectors = ServiceLocator.Current.GetAllInstances <IContextInspector <ITokenContext> >(); foreach (IContextInspector <ITokenContext> inspector in inspectors) { inspector.Inspect(context); } }
public IToken IssueAccessTokenForResourceOwner(ITokenContext context) { var authorizationGrant = this.TokenRepo.FindAuthorizationGrant(context.AuthorizationCode); AccessToken token = new AccessToken { ExpiresIn = 120, Token = Guid.NewGuid().ToString(), RefreshToken = Guid.NewGuid().ToString(), //Scope = authorizationGrant.Scope.Split(' '), }; TokenRepo.AddAccessToken(token); return token; }
public void CommonAssertInspector(IContextInspector<ITokenContext> inspector, ITokenContext ctx) { try { inspector.Inspect(ctx); Assert.Fail("No Exception was thrown"); } catch (OAuthErrorResponseException<ITokenContext> x) { Assert.AreEqual(Parameters.ErrorParameters.ErrorValues.InvalidRequest, x.Error); } catch (Exception x) { Assert.Fail(x.Message); } }
public TokenResponse CreateResponse(ITokenContext context) { if (context.Error != null) { TokenResponse errorResponse = new TokenResponse(); errorResponse.HttpStatusCode = 400; IDictionary<string, object> errorResults = context.Error.ToResponseValues(); if (context.Error.Error == Parameters.ErrorParameters.ErrorValues.InvalidClient) errorResponse.HttpStatusCode = 401; errorResponse.Body = SerializeResponse(errorResults); return errorResponse; } if (context.Token == null) throw new OAuthFatalException(TokenEndpointResources.ContextDoesNotContainToken); TokenResponse response = new TokenResponse { HttpStatusCode = 200 }; response.Body = SerializeResponse(((ITokenizer)context.Token).ToResponseValues()); return response; }
public void CreateContextFromPostRequest() { Mock <HttpRequestBase> mckRequest = new Mock <HttpRequestBase>(); mckRequest.SetupGet(x => x.HttpMethod).Returns("POST"); mckRequest.SetupGet(x => x.Form).Returns(MakeRequestValues("http://www.mysite.com/callback")); IContextBuilder <ITokenContext> builder = new TokenContextBuilder(); ITokenContext context = builder.FromHttpRequest(mckRequest.Object); Assert.AreEqual("123", context.Client.ClientId); Assert.AreEqual("client-secret", context.Client.ClientSecret); Assert.AreEqual("owner-secret", context.ResourceOwnerPassword); Assert.AreEqual("456", context.ResourceOwnerUsername); Assert.AreEqual("auth-code", context.AuthorizationCode); Assert.AreEqual(Parameters.GrantTypeValues.AuthorizationCode, context.GrantType); Assert.AreEqual("refresh-token", context.RefreshToken); Assert.AreEqual(new Uri("http://www.mysite.com/callback"), context.RedirectUri); }
protected void CommonProcessorErrorAssert(AuthenticationCodeProcessor processor, ITokenContext context, string errorParamter) { try { processor.Process(context); Assert.Fail("Did not produce an exception"); } catch (OAuthErrorResponseException<IOAuthContext> x) { Assert.AreEqual(errorParamter, x.Error); } catch (OAuthErrorResponseException<ITokenContext> x) { Assert.AreEqual(errorParamter, x.Error); } catch (Exception x) { Assert.Fail("Unexpected exception was thrown: " + x.Message); } }
public void GrantAccessToken(ITokenContext context) { try { InspectContext(context); bool handled = false; var processors = ServiceLocator.Current.GetAllInstances <ContextProcessor <ITokenContext> >(); foreach (ContextProcessor <ITokenContext> processor in processors) { if (!processor.IsSatisfiedBy(context)) { continue; } handled = true; processor.Process(context); break; } if (!handled) { throw Errors.UnsupportedGrantType(context); } } catch (OAuthErrorResponseException <ITokenContext> x) { context.Error = new ErrorResponse { Error = x.Error, ErrorDescription = x.Message, ErrorUri = x.ErrorUri }; } }
public TokenWriteOperations() { _tokenContext = DbContext.TokenContext; }
public static TermOccur GetTermOccur(this ITokenContext context) { const TermOccur DEFAULT_TERM_OCCUR = TermOccur.Must; return(context[WhitespaceToken.TERM_OCCUR].Value as TermOccur? ?? DEFAULT_TERM_OCCUR); }
public bool CredentialsAreValid(ITokenContext context) { return(context.ResourceOwnerUsername.ToUpperInvariant() == "GEOFF" && context.ResourceOwnerPassword == "password"); }
protected void CommonProcessorErrorAssert(AuthenticationCodeProcessor processor, ITokenContext context, string errorParamter) { try { processor.Process(context); Assert.Fail("Did not produce an exception"); } catch (OAuthErrorResponseException <IOAuthContext> x) { Assert.AreEqual(errorParamter, x.Error); } catch (OAuthErrorResponseException <ITokenContext> x) { Assert.AreEqual(errorParamter, x.Error); } catch (Exception x) { Assert.Fail("Unexpected exception was thrown: " + x.Message); } }
internal static OAuthErrorResponseException <ITokenContext> UnsupportedGrantType(ITokenContext context) { return(new OAuthErrorResponseException <ITokenContext>(context, Parameters.ErrorParameters.ErrorValues.UnsupportedGrantType, description: string.Format(CultureInfo.CurrentUICulture, TokenEndpointResources.UnsupportedGrantType, context.GrantType))); }
public static TokenResponse CreateTokenResponse(this ITokenContext context) { ITokenResponseBuilder builder = GetResponseBuilder(); return(builder.CreateResponse(context)); }
public TokenGetOperations() { _tokenContext = DbContext.TokenContext; }
public void CommonAssertInspector(IContextInspector <ITokenContext> inspector, ITokenContext ctx) { try { inspector.Inspect(ctx); Assert.Fail("No Exception was thrown"); } catch (OAuthErrorResponseException <ITokenContext> x) { Assert.AreEqual(Parameters.ErrorParameters.ErrorValues.InvalidRequest, x.Error); } catch (Exception x) { Assert.Fail(x.Message); } }
public bool CredentialsAreValid(ITokenContext context) { return (context.ResourceOwnerUsername.ToUpperInvariant() == "GEOFF" && context.ResourceOwnerPassword == "password"); }