public static ITokenContext GrantAccessToken(this ITokenContext context)
        {
            ITokenProvider provider = GetProvider();

            provider.GrantAccessToken(context);
            return(context);
        }
Пример #2
0
        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);
        }
Пример #4
0
 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)));
 }
Пример #5
0
 public NumAttributeRange(string attrName, bool isFloat, double from, double?to, ITokenContext context)
 {
     AttrName = attrName;
     IsFloat  = isFloat;
     From     = from;
     To       = to;
     Context  = context;
 }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
		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;

		}
Пример #9
0
        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);
            }
        }
Пример #10
0
		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;

		}
Пример #11
0
        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);
            }

        }
Пример #12
0
        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;
        }
Пример #13
0
        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);
        }
Пример #14
0
 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);
     }
 }
Пример #15
0
        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
                };
            }
        }
Пример #16
0
 public TokenWriteOperations()
 {
     _tokenContext = DbContext.TokenContext;
 }
Пример #17
0
        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");
 }
Пример #19
0
 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);
     }
 }
Пример #20
0
 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)));
 }
Пример #21
0
        public static TokenResponse CreateTokenResponse(this ITokenContext context)
        {
            ITokenResponseBuilder builder = GetResponseBuilder();

            return(builder.CreateResponse(context));
        }
Пример #22
0
 public TokenGetOperations()
 {
     _tokenContext = DbContext.TokenContext;
 }
Пример #23
0
 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);
     }
 }
Пример #24
0
 public bool CredentialsAreValid(ITokenContext context)
 {
     return (context.ResourceOwnerUsername.ToUpperInvariant() == "GEOFF" && context.ResourceOwnerPassword == "password");
 }